javax.security.cert.X509Certificate.getEncoded()方法的使用及代码示例

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

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

X509Certificate.getEncoded介绍

暂无

代码示例

代码示例来源: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: apache/nifi

/**
 * Accepts a legacy {@link javax.security.cert.X509Certificate} and returns an {@link X509Certificate}. The {@code javax.*} package certificate classes are for legacy compatibility and should
 * not be used for new development.
 *
 * @param legacyCertificate the {@code javax.security.cert.X509Certificate}
 * @return a new {@code java.security.cert.X509Certificate}
 * @throws CertificateException if there is an error generating the new certificate
 */
public static X509Certificate convertLegacyX509Certificate(javax.security.cert.X509Certificate legacyCertificate) throws CertificateException {
  if (legacyCertificate == null) {
    throw new IllegalArgumentException("The X.509 certificate cannot be null");
  }
  try {
    return formX509Certificate(legacyCertificate.getEncoded());
  } catch (javax.security.cert.CertificateEncodingException e) {
    throw new CertificateException(e);
  }
}

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

for (int i = 0; i < x509Certs.length; i++) {
  try {
  byte buffer[] = jsseCerts[i].getEncoded();
  CertificateFactory cf =
    CertificateFactory.getInstance("X.509");

代码示例来源: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: io.undertow/undertow-core

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: org.jboss.eap/wildfly-client-all

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: 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: org.apache.nifi/nifi-security-utils

/**
 * Accepts a legacy {@link javax.security.cert.X509Certificate} and returns an {@link X509Certificate}. The {@code javax.*} package certificate classes are for legacy compatibility and should
 * not be used for new development.
 *
 * @param legacyCertificate the {@code javax.security.cert.X509Certificate}
 * @return a new {@code java.security.cert.X509Certificate}
 * @throws CertificateException if there is an error generating the new certificate
 */
public static X509Certificate convertLegacyX509Certificate(javax.security.cert.X509Certificate legacyCertificate) throws CertificateException {
  if (legacyCertificate == null) {
    throw new IllegalArgumentException("The X.509 certificate cannot be null");
  }
  try {
    return formX509Certificate(legacyCertificate.getEncoded());
  } catch (javax.security.cert.CertificateEncodingException e) {
    throw new CertificateException(e);
  }
}

代码示例来源:origin: org.apache.nifi.registry/nifi-registry-security-utils

/**
 * Accepts a legacy {@link javax.security.cert.X509Certificate} and returns an {@link X509Certificate}. The {@code javax.*} package certificate classes are for legacy compatibility and should
 * not be used for new development.
 *
 * @param legacyCertificate the {@code javax.security.cert.X509Certificate}
 * @return a new {@code java.security.cert.X509Certificate}
 * @throws CertificateException if there is an error generating the new certificate
 */
public static X509Certificate convertLegacyX509Certificate(javax.security.cert.X509Certificate legacyCertificate) throws CertificateException {
  if (legacyCertificate == null) {
    throw new IllegalArgumentException("The X.509 certificate cannot be null");
  }
  try {
    return formX509Certificate(legacyCertificate.getEncoded());
  } catch (javax.security.cert.CertificateEncodingException e) {
    throw new CertificateException(e);
  }
}

代码示例来源:origin: io.undertow/undertow-servlet

/**
 * Return the chain of X509 certificates used to negotiate the SSL Session.
 * <p>
 * We convert JSSE's javax.security.cert.X509Certificate[]  to servlet's  java.security.cert.X509Certificate[]
 *
 * @param session the   javax.net.ssl.SSLSession to use as the source of the cert chain.
 * @return the chain of java.security.cert.X509Certificates used to
 *         negotiate the SSL connection. <br>
 *         Will be null if the chain is missing or empty.
 */
private X509Certificate[] getCerts(SSLSessionInfo session) {
  try {
    javax.security.cert.X509Certificate[] javaxCerts = session.getPeerCertificateChain();
    if (javaxCerts == null || javaxCerts.length == 0) {
      return null;
    }
    X509Certificate[] javaCerts = new X509Certificate[javaxCerts.length];
    java.security.cert.CertificateFactory cf = java.security.cert.CertificateFactory.getInstance("X.509");
    for (int i = 0; i < javaxCerts.length; i++) {
      byte[] bytes = javaxCerts[i].getEncoded();
      ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
      javaCerts[i] = (X509Certificate) cf.generateCertificate(stream);
    }
    return javaCerts;
  } catch (Exception e) {
    return null;
  }
                                                                                                                                                                                                                                                                                                                                                                                          }

代码示例来源:origin: org.wso2.transport.http/org.wso2.transport.http.netty

/** Convert certificates and create a certificate chain.
   *
   * @param certs array of javax.security.cert.X509Certificate[] s.
   * @return the converted array of java.security.cert.X509Certificate[] s.
   * @throws CertificateVerificationException If an error occurs while converting certificates
   * from java to javax
   */
  private X509Certificate[] convert(javax.security.cert.X509Certificate[] certs)
      throws CertificateVerificationException {
    X509Certificate[] certChain = new X509Certificate[certs.length];
    Throwable exceptionThrown;
    for (int i = 0; i < certs.length; i++) {
      try {
        byte[] encoded = certs[i].getEncoded();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encoded);
        CertificateFactory certificateFactory = CertificateFactory.getInstance(Constants.X_509);
        certChain[i] = ((X509Certificate) certificateFactory.generateCertificate(byteArrayInputStream));
        continue;
      } catch (CertificateEncodingException | CertificateException e) {
        exceptionThrown = e;
      }
      throw new CertificateVerificationException("Cant Convert certificates from javax to java", exceptionThrown);
    }
    return certChain;
  }
}

代码示例来源:origin: org.eclipse.jetty/server

public static X509Certificate[] getCertChain(SSLSession sslSession)
{
  try
  {
    javax.security.cert.X509Certificate javaxCerts[]=sslSession.getPeerCertificateChain();
    if (javaxCerts==null||javaxCerts.length==0)
      return null;
    int length=javaxCerts.length;
    X509Certificate[] javaCerts=new X509Certificate[length];
    java.security.cert.CertificateFactory cf=java.security.cert.CertificateFactory.getInstance("X.509");
    for (int i=0; i<length; i++)
    {
      byte bytes[]=javaxCerts[i].getEncoded();
      ByteArrayInputStream stream=new ByteArrayInputStream(bytes);
      javaCerts[i]=(X509Certificate)cf.generateCertificate(stream);
    }
    return javaCerts;
  }
  catch (SSLPeerUnverifiedException pue)
  {
    return null;
  }
  catch (Exception e)
  {
    LOG.warn(Log.EXCEPTION,e);
    return null;
  }
}

代码示例来源:origin: org.eclipse.jetty.aggregate/jetty-all-server

public static X509Certificate[] getCertChain(SSLSession sslSession)
{
  try
  {
    javax.security.cert.X509Certificate javaxCerts[]=sslSession.getPeerCertificateChain();
    if (javaxCerts==null||javaxCerts.length==0)
      return null;
    int length=javaxCerts.length;
    X509Certificate[] javaCerts=new X509Certificate[length];
    java.security.cert.CertificateFactory cf=java.security.cert.CertificateFactory.getInstance("X.509");
    for (int i=0; i<length; i++)
    {
      byte bytes[]=javaxCerts[i].getEncoded();
      ByteArrayInputStream stream=new ByteArrayInputStream(bytes);
      javaCerts[i]=(X509Certificate)cf.generateCertificate(stream);
    }
    return javaCerts;
  }
  catch (SSLPeerUnverifiedException pue)
  {
    return null;
  }
  catch (Exception e)
  {
    LOG.warn(Log.EXCEPTION,e);
    return null;
  }
}

代码示例来源:origin: org.eclipse.jetty.aggregate/jetty-webapp

public static X509Certificate[] getCertChain(SSLSession sslSession)
{
  try
  {
    javax.security.cert.X509Certificate javaxCerts[]=sslSession.getPeerCertificateChain();
    if (javaxCerts==null||javaxCerts.length==0)
      return null;
    int length=javaxCerts.length;
    X509Certificate[] javaCerts=new X509Certificate[length];
    java.security.cert.CertificateFactory cf=java.security.cert.CertificateFactory.getInstance("X.509");
    for (int i=0; i<length; i++)
    {
      byte bytes[]=javaxCerts[i].getEncoded();
      ByteArrayInputStream stream=new ByteArrayInputStream(bytes);
      javaCerts[i]=(X509Certificate)cf.generateCertificate(stream);
    }
    return javaCerts;
  }
  catch (SSLPeerUnverifiedException pue)
  {
    return null;
  }
  catch (Exception e)
  {
    LOG.warn(Log.EXCEPTION,e);
    return null;
  }
}

代码示例来源:origin: com.ovea.tajin.servers/tajin-server-jetty9

public static X509Certificate[] getCertChain(SSLSession sslSession)
{
  try
  {
    javax.security.cert.X509Certificate javaxCerts[]=sslSession.getPeerCertificateChain();
    if (javaxCerts==null||javaxCerts.length==0)
      return null;
    int length=javaxCerts.length;
    X509Certificate[] javaCerts=new X509Certificate[length];
    java.security.cert.CertificateFactory cf=java.security.cert.CertificateFactory.getInstance("X.509");
    for (int i=0; i<length; i++)
    {
      byte bytes[]=javaxCerts[i].getEncoded();
      ByteArrayInputStream stream=new ByteArrayInputStream(bytes);
      javaCerts[i]=(X509Certificate)cf.generateCertificate(stream);
    }
    return javaCerts;
  }
  catch (SSLPeerUnverifiedException pue)
  {
    return null;
  }
  catch (Exception e)
  {
    LOG.warn(Log.EXCEPTION,e);
    return null;
  }
}

代码示例来源:origin: com.ovea.tajin.server/tajin-server-jetty9

public static X509Certificate[] getCertChain(SSLSession sslSession)
{
  try
  {
    javax.security.cert.X509Certificate javaxCerts[]=sslSession.getPeerCertificateChain();
    if (javaxCerts==null||javaxCerts.length==0)
      return null;
    int length=javaxCerts.length;
    X509Certificate[] javaCerts=new X509Certificate[length];
    java.security.cert.CertificateFactory cf=java.security.cert.CertificateFactory.getInstance("X.509");
    for (int i=0; i<length; i++)
    {
      byte bytes[]=javaxCerts[i].getEncoded();
      ByteArrayInputStream stream=new ByteArrayInputStream(bytes);
      javaCerts[i]=(X509Certificate)cf.generateCertificate(stream);
    }
    return javaCerts;
  }
  catch (SSLPeerUnverifiedException pue)
  {
    return null;
  }
  catch (Exception e)
  {
    LOG.warn(Log.EXCEPTION,e);
    return null;
  }
}

代码示例来源:origin: org.eclipse.jetty.aggregate/jetty-plus

public static X509Certificate[] getCertChain(SSLSession sslSession)
{
  try
  {
    javax.security.cert.X509Certificate javaxCerts[]=sslSession.getPeerCertificateChain();
    if (javaxCerts==null||javaxCerts.length==0)
      return null;
    int length=javaxCerts.length;
    X509Certificate[] javaCerts=new X509Certificate[length];
    java.security.cert.CertificateFactory cf=java.security.cert.CertificateFactory.getInstance("X.509");
    for (int i=0; i<length; i++)
    {
      byte bytes[]=javaxCerts[i].getEncoded();
      ByteArrayInputStream stream=new ByteArrayInputStream(bytes);
      javaCerts[i]=(X509Certificate)cf.generateCertificate(stream);
    }
    return javaCerts;
  }
  catch (SSLPeerUnverifiedException pue)
  {
    return null;
  }
  catch (Exception e)
  {
    LOG.warn(Log.EXCEPTION,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;
    }
  }
};

代码示例来源: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: 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();
}

相关文章