javax.security.cert.X509Certificate类的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中javax.security.cert.X509Certificate类的一些代码示例,展示了X509Certificate类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。X509Certificate类的具体详情如下:
包路径:javax.security.cert.X509Certificate
类名称:X509Certificate

X509Certificate介绍

[英]Abstract base class for X.509 certificates.

This represents a standard way for accessing the attributes of X.509 v1 certificates.

Note: This package is provided only for compatibility reasons. It contains a simplified version of the java.security.cert package that was previously used by JSSE (Java SSL package). All applications that do not have to be compatible with older versions of JSSE (that is before Java SDK 1.5) should only use java.security.cert.
[中]

代码示例

代码示例来源:origin: wildfly/wildfly

public static String toPem(final X509Certificate certificate) throws CertificateEncodingException {
  final StringBuilder builder = new StringBuilder();
  builder.append(BEGIN_CERT);
  builder.append('\n');
  builder.append(FlexBase64.encodeString(certificate.getEncoded(), true));
  builder.append('\n');
  builder.append(END_CERT);
  return builder.toString();
}

代码示例来源:origin: robovm/robovm

/**
 * Creates a new {@code X509Certificate} and initializes it from the
 * specified byte array.
 *
 * @param certData
 *            byte array containing data to initialize the certificate.
 * @return the certificate initialized from the specified byte array
 * @throws CertificateException
 *             if the certificate cannot be created or initialized.
 */
public static final X509Certificate getInstance(byte[] certData)
                     throws CertificateException {
  if (certData == null) {
    throw new CertificateException("certData == null");
  }
  ByteArrayInputStream bais = new ByteArrayInputStream(certData);
  return getInstance(bais);
}

代码示例来源:origin: redisson/redisson

@Override
public Principal getSubjectDN() {
  return unwrap().getSubjectDN();
}

代码示例来源:origin: javaee/glassfish

X509Certificate jsseCerts[] = null;
try{
  jsseCerts = session.getPeerCertificateChain();
} catch (Throwable ex){
for (int i = 0; i < x509Certs.length; i++) {
  try {
  byte buffer[] = jsseCerts[i].getEncoded();
  CertificateFactory cf =
    CertificateFactory.getInstance("X.509");
  ByteArrayInputStream stream =
    new ByteArrayInputStream(buffer);
  x509Certs[i] = (java.security.cert.X509Certificate)
    cf.generateCertificate(stream);
  if(logger.isLoggable(Level.FINEST))
    logger.log(Level.FINE,"Cert #" + i + " = " + x509Certs[i]);

代码示例来源:origin: wildfly/wildfly

/**
 *
 * @param sessionId The SSL session ID
 * @param cypherSuite The cypher suite name
 * @param certificate A string representation of the client certificate
 * @throws java.security.cert.CertificateException If the client cert could not be decoded
 * @throws CertificateException If the client cert could not be decoded
 */
public BasicSSLSessionInfo(byte[] sessionId, String cypherSuite, String certificate) throws java.security.cert.CertificateException, CertificateException {
  this.sessionId = sessionId;
  this.cypherSuite = cypherSuite;
  if (certificate != null) {
    java.security.cert.CertificateFactory cf = java.security.cert.CertificateFactory.getInstance("X.509");
    byte[] certificateBytes = certificate.getBytes(StandardCharsets.US_ASCII);
    ByteArrayInputStream stream = new ByteArrayInputStream(certificateBytes);
    Collection<? extends java.security.cert.Certificate> certCol = cf.generateCertificates(stream);
    this.peerCertificate = new java.security.cert.Certificate[certCol.size()];
    this.certificate = new X509Certificate[certCol.size()];
    int i=0;
    for(java.security.cert.Certificate cert : certCol) {
      this.peerCertificate[i] = cert;
      this.certificate[i++] = X509Certificate.getInstance(cert.getEncoded());
    }
  } else {
    this.peerCertificate = null;
    this.certificate = null;
  }
}
/**

代码示例来源:origin: net.sf.jstuff/jstuff-core

/**
* Converts a javax.security.cert.X509Certificate to java.security.cert.X509Certificate
*/
@SuppressWarnings("resource")
public static X509Certificate convert(final javax.security.cert.X509Certificate cert) {
 if (cert == null)
   return null;
 try {
   final FastByteArrayInputStream bis = new FastByteArrayInputStream(cert.getEncoded());
   return (X509Certificate) CERTIFICATE_FACTORY.generateCertificate(bis);
 } catch (final Exception ex) {
   throw new IllegalArgumentException("[cert] " + cert + " is not convertable!", ex);
 }
}

代码示例来源:origin: jamorham/xDrip-plus

@Override
  public boolean verify(final String hostname, final SSLSession session) {
    try {
      final String peerCertificateHash = CipherUtils.getSHA256(session.getPeerCertificateChain()[0].getEncoded());
      if (CERTIFICATE_PIN.equals(peerCertificateHash)) {
        return true;
      } else {
        UserError.Log.e(TAG, "Remote https certificate doesn't match! " + peerCertificateHash);
        return false;
      }
    } catch (Exception e) {
      UserError.Log.e(TAG, "Unable to verify host: " + e);
      return false;
    }
  }
};

代码示例来源:origin: ARMmbed/java-coap

@Override
protected void connect(CoapReceiver coapReceiver) throws IOException {
  SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket(destination.getAddress(), destination.getPort());
  sslSocket.addHandshakeCompletedListener(handshakeCompletedEvent -> {
        try {
          LOGGER.debug("Connected [" + handshakeCompletedEvent.getSource() + ", " + sslSocket.getSession().getPeerCertificateChain()[0].getSubjectDN() + "]");
        } catch (SSLPeerUnverifiedException e) {
          LOGGER.warn(e.getMessage(), e);
        }
    coapReceiver.onConnected((InetSocketAddress) socket.getRemoteSocketAddress());
      }
  );
  sslSocket.startHandshake();
  this.socket = sslSocket;
  synchronized (this) {
    outputStream = new BufferedOutputStream(socket.getOutputStream());
  }
  inputStream = new BufferedInputStream(socket.getInputStream(), 1024);
}

代码示例来源:origin: xjdr/xio

private String getPeerIdentity(SSLEngine engine) {
 try {
  SSLSession session = engine.getSession();
  javax.security.cert.X509Certificate[] chain = session.getPeerCertificateChain();
  if (chain == null || chain.length == 0 || chain[0] == null) {
   return TlsAuthState.UNAUTHENTICATED;
  }
  // double check that the certificate is valid
  chain[0].checkValidity();
  return session.getPeerPrincipal().getName();
 } catch (javax.security.cert.CertificateExpiredException
   | javax.security.cert.CertificateNotYetValidException e) {
  return TlsAuthState.UNAUTHENTICATED;
 } catch (SSLPeerUnverifiedException e) {
  return TlsAuthState.UNAUTHENTICATED;
 }
}

代码示例来源:origin: jaredrummler/APKParser

public CertificateMeta parse() throws IOException, CertificateException {
 X509Certificate certificate = X509Certificate.getInstance(Utils.toByteArray(in));
 CertificateMeta.Builder builder = CertificateMeta.newCertificateMeta();
 byte[] bytes = certificate.getEncoded();
 String certMd5 = md5Digest(bytes);
 String publicKeyString = byteToHexString(bytes);
 String certBase64Md5 = md5Digest(publicKeyString);
 builder.data(bytes);
 builder.certBase64Md5(certBase64Md5);
 builder.certMd5(certMd5);
 builder.startDate(certificate.getNotBefore());
 builder.endDate(certificate.getNotAfter());
 builder.signAlgorithm(certificate.getSigAlgName());
 builder.signAlgorithmOID(certificate.getSigAlgOID());
 return builder.build();
}

代码示例来源:origin: redisson/redisson

private X509Certificate unwrap() {
    X509Certificate wrapped = this.wrapped;
    if (wrapped == null) {
      try {
        wrapped = this.wrapped = X509Certificate.getInstance(bytes);
      } catch (CertificateException e) {
        throw new IllegalStateException(e);
      }
    }
    return wrapped;
  }
}

代码示例来源:origin: redisson/redisson

@Override
public BigInteger getSerialNumber() {
  return unwrap().getSerialNumber();
}

代码示例来源:origin: GruppoFilippetti/vertx-mqtt-broker

public String getTenant() {
  String tenant = null;
  if(certs!=null) {
    for (X509Certificate c : certs) {
      String dn = c.getSubjectDN().getName();// info del DEVICE/TENANT
      tenant = getTenantFromDN(dn);
      logger.debug("Cert Info - " + c.getSerialNumber() + " " + dn);
    }
  }
  logger.debug("Cert Info - tenant found: "+ tenant);
  return tenant;
}

代码示例来源:origin: redisson/redisson

@Override
public Principal getIssuerDN() {
  return unwrap().getIssuerDN();
}

代码示例来源:origin: redisson/redisson

@Override
public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException {
  unwrap().checkValidity();
}

代码示例来源:origin: redisson/redisson

@Override
public PublicKey getPublicKey() {
  return unwrap().getPublicKey();
}

代码示例来源:origin: redisson/redisson

@Override
public Date getNotAfter() {
  return unwrap().getNotAfter();
}

代码示例来源:origin: redisson/redisson

@Override
public Date getNotBefore() {
  return unwrap().getNotBefore();
}

代码示例来源:origin: io.macgyver/macgyver-core

public java.security.cert.X509Certificate convert(
    javax.security.cert.X509Certificate cert) {
  try {
    byte[] encoded = cert.getEncoded();
    ByteArrayInputStream bis = new ByteArrayInputStream(encoded);
    java.security.cert.CertificateFactory cf = java.security.cert.CertificateFactory
        .getInstance("X.509");
    return (java.security.cert.X509Certificate) cf
        .generateCertificate(bis);
  } catch (java.security.cert.CertificateEncodingException e) {
  } catch (javax.security.cert.CertificateEncodingException e) {
  } catch (java.security.cert.CertificateException e) {
  }
  return null;
}

代码示例来源:origin: NightscoutFoundation/xDrip

@Override
  public boolean verify(final String hostname, final SSLSession session) {
    try {
      final String peerCertificateHash = CipherUtils.getSHA256(session.getPeerCertificateChain()[0].getEncoded());
      if (CERTIFICATE_PIN.equals(peerCertificateHash)) {
        return true;
      } else {
        UserError.Log.e(TAG, "Remote https certificate doesn't match! " + peerCertificateHash);
        return false;
      }
    } catch (Exception e) {
      UserError.Log.e(TAG, "Unable to verify host: " + e);
      return false;
    }
  }
};

相关文章