当前位置: 首页>>代码示例>>Java>>正文


Java SSLSession.getPeerCertificateChain方法代码示例

本文整理汇总了Java中javax.net.ssl.SSLSession.getPeerCertificateChain方法的典型用法代码示例。如果您正苦于以下问题:Java SSLSession.getPeerCertificateChain方法的具体用法?Java SSLSession.getPeerCertificateChain怎么用?Java SSLSession.getPeerCertificateChain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.net.ssl.SSLSession的用法示例。


在下文中一共展示了SSLSession.getPeerCertificateChain方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: verify

import javax.net.ssl.SSLSession; //导入方法依赖的package包/类
/**
 * Verifies the certificate chain presented by the server to which
 * a secure Socket has just connected.  Specifically, the provided host
 * name is checked against the Common Name of the server certificate;
 * additional checks may or may not be performed.
 *
 * @param host the requested host name
 * @param session SSLSession used on the connection to host
 * @throws Exception if the certificate chain cannot be verified
 */
protected void verify(String host, SSLSession session) throws Exception {

    X509Certificate[] chain;
    X509Certificate   certificate;
    Principal         principal;
    PublicKey         publicKey;
    String            DN;
    String            CN;
    int               start;
    int               end;
    String            emsg;

    chain       = session.getPeerCertificateChain();
    certificate = chain[0];
    principal   = certificate.getSubjectDN();
    DN          = String.valueOf(principal);
    start       = DN.indexOf("CN=");

    if (start < 0) {
        throw new UnknownHostException(
            Error.getMessage(ErrorCode.M_SERVER_SECURE_VERIFY_1));
    }

    start += 3;
    end   = DN.indexOf(',', start);
    CN    = DN.substring(start, (end > -1) ? end
                                           : DN.length());

    if (CN.length() < 1) {
        throw new UnknownHostException(
            Error.getMessage(ErrorCode.M_SERVER_SECURE_VERIFY_2));
    }

    if (!CN.equalsIgnoreCase(host)) {

        // TLS_HOSTNAME_MISMATCH
        throw new UnknownHostException(
            Error.getMessage(
                ErrorCode.M_SERVER_SECURE_VERIFY_3, 0, new Object[] {
            CN, host
        }));
    }
}
 
开发者ID:tiweGH,项目名称:OpenDiabetes,代码行数:54,代码来源:HsqlSocketFactorySecure.java

示例2: verify

import javax.net.ssl.SSLSession; //导入方法依赖的package包/类
/**
 * Verifyies the certificate chain presented by the server to which
 * a secure Socket has just connected.  Specifically, the provided host
 * name is checked against the Common Name of the server certificate;
 * additional checks may or may not be performed.
 *
 * @param host the requested host name
 * @param session SSLSession used on the connection to host
 * @throws Exception if the certificate chain cannot be verified
 */
protected void verify(String host, SSLSession session) throws Exception {

    X509Certificate[] chain;
    X509Certificate   certificate;
    Principal         principal;
    PublicKey         publicKey;
    String            DN;
    String            CN;
    int               start;
    int               end;
    String            emsg;

    chain       = session.getPeerCertificateChain();
    certificate = chain[0];
    principal   = certificate.getSubjectDN();
    DN          = String.valueOf(principal);
    start       = DN.indexOf("CN=");

    if (start < 0) {
        throw new UnknownHostException(
            Error.getMessage(ErrorCode.M_SERVER_SECURE_VERIFY_1));
    }

    start += 3;
    end   = DN.indexOf(',', start);
    CN    = DN.substring(start, (end > -1) ? end
                                           : DN.length());

    if (CN.length() < 1) {
        throw new UnknownHostException(
            Error.getMessage(ErrorCode.M_SERVER_SECURE_VERIFY_2));
    }

    if (!CN.equalsIgnoreCase(host)) {

        // TLS_HOSTNAME_MISMATCH
        throw new UnknownHostException(
            Error.getMessage(
                ErrorCode.M_SERVER_SECURE_VERIFY_3, 0, new Object[] {
            CN, host
        }));
    }
}
 
开发者ID:Julien35,项目名称:dev-courses,代码行数:54,代码来源:HsqlSocketFactorySecure.java

示例3: verify

import javax.net.ssl.SSLSession; //导入方法依赖的package包/类
/**
 * Verifyies the certificate chain presented by the server to which
 * a secure Socket has just connected.  Specifically, the provided host
 * name is checked against the Common Name of the server certificate;
 * additional checks may or may not be performed.
 *
 * @param host the requested host name
 * @param session SSLSession used on the connection to host
 * @throws Exception if the certificate chain cannot be verified
 */
protected void verify(String host, SSLSession session) throws Exception {

    X509Certificate[] chain;
    X509Certificate   certificate;
    Principal         principal;
    PublicKey         publicKey;
    String            DN;
    String            CN;
    int               start;
    int               end;
    String            emsg;

    chain       = session.getPeerCertificateChain();
    certificate = chain[0];
    principal   = certificate.getSubjectDN();
    DN          = String.valueOf(principal);
    start       = DN.indexOf("CN=");

    if (start < 0) {
        throw new UnknownHostException(
            Trace.getMessage(Trace.HsqlSocketFactorySecure_verify));
    }

    start += 3;
    end   = DN.indexOf(',', start);
    CN    = DN.substring(start, (end > -1) ? end
                                           : DN.length());

    if (CN.length() < 1) {
        throw new UnknownHostException(
            Trace.getMessage(Trace.HsqlSocketFactorySecure_verify2));
    }

    if (!CN.equalsIgnoreCase(host)) {

        // TLS_HOSTNAME_MISMATCH
        throw new UnknownHostException(
            Trace.getMessage(
                Trace.HsqlSocketFactorySecure_verify3, true,
                new Object[] {
            CN, host
        }));
    }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:55,代码来源:HsqlSocketFactorySecure.java

示例4: verifyHostname

import javax.net.ssl.SSLSession; //导入方法依赖的package包/类
/**
 * Describe <code>verifyHostname</code> method here.
 *
 * @param socket a <code>SSLSocket</code> value
 * @exception SSLPeerUnverifiedException  If there are problems obtaining
 * the server certificates from the SSL session, or the server host name 
 * does not match with the "Common Name" in the server certificates 
 * SubjectDN.
 * @exception UnknownHostException  If we are not able to resolve
 * the SSL sessions returned server host name. 
 */
private void verifyHostname(SSLSocket socket) 
    throws SSLPeerUnverifiedException, UnknownHostException {
    if (! verifyHostname) 
        return;

    SSLSession session = socket.getSession();
    String hostname = session.getPeerHost();
    try {
        InetAddress addr = InetAddress.getByName(hostname);
    } catch (UnknownHostException uhe) {
        throw new UnknownHostException("Could not resolve SSL sessions "
                                       + "server hostname: " + hostname);
    }
    
    X509Certificate[] certs = session.getPeerCertificateChain();
    if (certs == null || certs.length == 0) 
        throw new SSLPeerUnverifiedException("No server certificates found!");
    
    //get the servers DN in its string representation
    String dn = certs[0].getSubjectDN().getName();

    //might be useful to print out all certificates we receive from the
    //server, in case one has to debug a problem with the installed certs.
    if (LOG.isDebugEnabled()) {
        LOG.debug("Server certificate chain:");
        for (int i = 0; i < certs.length; i++) {
            LOG.debug("X509Certificate[" + i + "]=" + certs[i]);
        }
    }
    //get the common name from the first cert
    String cn = getCN(dn);
    if (hostname.equalsIgnoreCase(cn)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Target hostname valid: " + cn);
        }
    } else {
        throw new SSLPeerUnverifiedException(
            "HTTPS hostname invalid: expected '" + hostname + "', received '" + cn + "'");
    }
}
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:52,代码来源:StrictSSLProtocolSocketFactory.java


注:本文中的javax.net.ssl.SSLSession.getPeerCertificateChain方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。