當前位置: 首頁>>代碼示例>>Java>>正文


Java CertificateFactory.generateCertificates方法代碼示例

本文整理匯總了Java中java.security.cert.CertificateFactory.generateCertificates方法的典型用法代碼示例。如果您正苦於以下問題:Java CertificateFactory.generateCertificates方法的具體用法?Java CertificateFactory.generateCertificates怎麽用?Java CertificateFactory.generateCertificates使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.security.cert.CertificateFactory的用法示例。


在下文中一共展示了CertificateFactory.generateCertificates方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getCertificateFromStream

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
/**
 * Generate Collection of Certificate from Input Stream
 *
 * @param stream InputStream of Certificate data
 * @return Collection<X509Certificate>
 * @throws PayPalRESTException
 */
@SuppressWarnings("unchecked")
public static Collection<X509Certificate> getCertificateFromStream(InputStream stream) throws PayPalRESTException {
	if (stream == null) {
		throw new PayPalRESTException("Certificate Not Found");
	}
	Collection<X509Certificate> certs = null;
	try {
		// Create a Certificate Factory
		CertificateFactory cf = CertificateFactory.getInstance("X.509");

		// Read the Trust Certs
		certs = (Collection<X509Certificate>) cf.generateCertificates(stream);
	} catch (CertificateException ex) {
		throw new PayPalRESTException(ex);
	}
	return certs;
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:25,代碼來源:SSLUtil.java

示例2: main

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.out.println("Usage: java BlacklistedCertsConverter SHA-256" +
                " < blacklisted.certs.pem > blacklisted.certs");
        System.exit(1);
    }
    String mdAlg = args[0];
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Collection<? extends Certificate> certs
            = cf.generateCertificates(System.in);
    System.out.println("Algorithm=" + mdAlg);
    for (Certificate cert: certs) {
        System.out.println(
                getCertificateFingerPrint(mdAlg, (X509Certificate)cert));
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:17,代碼來源:BlacklistedCertsConverter.java

示例3: loadCerts

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
public static X509Certificate[] loadCerts(Buffer buffer) {
    if (buffer == null) {
        throw new NullPointerException("Missing X.509 certificate");
    }
    try {
        List<byte[]> pems = loadPem(buffer, "CERTIFICATE");
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        List<X509Certificate> certs = new ArrayList<>(pems.size());
        for (byte[] pem : pems) {
            for (Certificate cert : certFactory.generateCertificates(new ByteArrayInputStream(pem))) {
                certs.add((X509Certificate) cert);
            }
        }
        return certs.toArray(new X509Certificate[certs.size()]);
    } catch (Exception e) {
        throw new RuntimeException("Problem loading certificate certWithChain", e);
    }
}
 
開發者ID:xkr47,項目名稱:vertx-acme4j,代碼行數:19,代碼來源:PemLoader.java

示例4: trustManagerForCertificates

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
/**
 * Returns a trust manager that trusts {@code certificates} and none other. HTTPS services whose
 * certificates have not been signed by these certificates will fail with a {@code
 * SSLHandshakeException}.
 *
 * <p>This can be used to replace the host platform's built-in trusted certificates with a custom
 * set. This is useful in development where certificate authority-trusted certificates aren't
 * available. Or in production, to avoid reliance on third-party certificate authorities.
 *
 * <p>See also {@link CertificatePinner}, which can limit trusted certificates while still using
 * the host platform's built-in trust store.
 *
 * <h3>Warning: Customizing Trusted Certificates is Dangerous!</h3>
 *
 * <p>Relying on your own trusted certificates limits your server team's ability to update their
 * TLS certificates. By installing a specific set of trusted certificates, you take on additional
 * operational complexity and limit your ability to migrate between certificate authorities. Do
 * not use custom trusted certificates in production without the blessing of your server's TLS
 * administrator.
 */
private X509TrustManager trustManagerForCertificates(InputStream in)
    throws GeneralSecurityException {
  CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
  Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(in);
  if (certificates.isEmpty()) {
    throw new IllegalArgumentException("expected non-empty set of trusted certificates");
  }

  // Put the certificates a key store.
  char[] password = "password".toCharArray(); // Any password will work.
  KeyStore keyStore = newEmptyKeyStore(password);
  int index = 0;
  for (Certificate certificate : certificates) {
    String certificateAlias = Integer.toString(index++);
    keyStore.setCertificateEntry(certificateAlias, certificate);
  }

  // Use it to build an X509 trust manager.
  KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
      KeyManagerFactory.getDefaultAlgorithm());
  keyManagerFactory.init(keyStore, password);
  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
      TrustManagerFactory.getDefaultAlgorithm());
  trustManagerFactory.init(keyStore);
  TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
  if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
    throw new IllegalStateException("Unexpected default trust managers:"
        + Arrays.toString(trustManagers));
  }
  return (X509TrustManager) trustManagers[0];
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:52,代碼來源:CustomTrust.java

示例5: trustManagerForCertificates

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
/**
 * Returns a trust manager that trusts {@code certificates} and none other. HTTPS services whose
 * certificates have not been signed by these certificates will fail with a {@code
 * SSLHandshakeException}.
 *
 * <p>This can be used to replace the host platform's built-in trusted certificates with a custom
 * set. This is useful in development where certificate authority-trusted certificates aren't
 * available. Or in production, to avoid reliance on third-party certificate authorities.
 *
 * <p>See also {@link CertificatePinner}, which can limit trusted certificates while still using
 * the host platform's built-in trust store.
 *
 * <h3>Warning: Customizing Trusted Certificates is Dangerous!</h3>
 *
 * <p>Relying on your own trusted certificates limits your server team's ability to update their
 * TLS certificates. By installing a specific set of trusted certificates, you take on additional
 * operational complexity and limit your ability to migrate between certificate authorities. Do
 * not use custom trusted certificates in production without the blessing of your server's TLS
 * administrator.
 */
private X509TrustManager trustManagerForCertificates(InputStream in)
        throws GeneralSecurityException {
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(in);
    if (certificates.isEmpty()) {
        throw new IllegalArgumentException("expected non-empty set of trusted certificates");
    }

    // Put the certificates a key store.
    char[] password = "password".toCharArray(); // Any password will work.
    KeyStore keyStore = newEmptyKeyStore(password);
    int index = 0;
    for (Certificate certificate : certificates) {
        String certificateAlias = Integer.toString(index++);
        keyStore.setCertificateEntry(certificateAlias, certificate);
    }

    // Use it to build an X509 trust manager.
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
            KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, password);
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
            TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
        throw new IllegalStateException("Unexpected default trust managers:"
                + Arrays.toString(trustManagers));
    }
    return (X509TrustManager) trustManagers[0];
}
 
開發者ID:ChristopherAbram,項目名稱:Book-Shelf,代碼行數:52,代碼來源:CustomTrust.java

示例6: main

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    /*
     * create an empty SignedData content type in ASN.1
     * as defined in PKCS#7
     */
    byte[] b = { 0x30, 0x23,
                 /* contentInfo ::= signedData */
                 0x06, 0x09, 0x2A, (byte)0x86, 0x48,
                 (byte)0x86, (byte)0xF7, 0x0D,
                 0x01, 0x07, 0x02,
                 0x00, 0x16,
                 0x30, 0x14,                /* SignedData */
                 0x02, 0x01, 0x01,          /* version */
                 0x31, 0x00,                /* digestAlgorithms */
                 0x30, 0x0B,                /* contentInfo ::= data */
                 0x06, 0x09, 0x2A, (byte)0x86, 0x48,
                 (byte)0x86, (byte)0xF7, 0x0D,
                 0x01, 0x07, 0x01,
                 /* certificates are absent */
                 0x31, 0x00                 /* signerInfos */
               };

    CertificateFactory cf = CertificateFactory.getInstance( "X509", "SUN");
    Collection c = cf.generateCertificates( new ByteArrayInputStream(b));
    if (!c.isEmpty())
        throw new Exception("CertificateFactory.generateCertificates() "
            + "did not return an empty Collection");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:29,代碼來源:GenerateCertificatesEmptyCollection.java

示例7: createPath

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
public static X509Certificate[] createPath(String chain) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    List list = new ArrayList();
    for (Certificate c: cf.generateCertificates(
            new FileInputStream(chain))) {
        list.add((X509Certificate)c);
    }
    return (X509Certificate[]) list.toArray(new X509Certificate[0]);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:10,代碼來源:CertReplace.java

示例8: trustManagerForCertificates

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
private static X509TrustManager trustManagerForCertificates(InputStream in)
        throws GeneralSecurityException {
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(in);
    if (certificates.isEmpty()) {
        throw new IllegalArgumentException("expected non-empty set of trusted certificates");
    }

    // Put the certificates a key store.
    char[] password = "password".toCharArray(); // Any password will work.
    KeyStore keyStore = newEmptyKeyStore(password);
    int index = 0;
    for (Certificate certificate : certificates) {
        String certificateAlias = Integer.toString(index++);
        keyStore.setCertificateEntry(certificateAlias, certificate);
    }

    // Use it to build an X509 trust manager.
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
            KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, password);
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
            TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
        throw new IllegalStateException("Unexpected default trust managers:"
                + Arrays.toString(trustManagers));
    }
    return (X509TrustManager) trustManagers[0];
}
 
開發者ID:jtduan,項目名稱:common-spider,代碼行數:32,代碼來源:HttpsUtil.java

示例9: importPrivateKey

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
void importPrivateKey(String keyAlias, String keyPassword, InputStream fl, InputStream certstream)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, CertificateException, KeyStoreException
{
    KeyInfoManager keyInfoManager = null;

    writeLock.lock();
    try
    {
        keyInfoManager = getKeyInfoManager(getKeyMetaDataFileLocation());
        KeyStore ks = loadKeyStore(getKeyStoreParameters(), keyInfoManager);

        // loading Key
        byte[] keyBytes = new byte[fl.available()];
        KeyFactory kf = KeyFactory.getInstance("RSA");
        fl.read(keyBytes, 0, fl.available());
        fl.close();
        PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(keyBytes);
        PrivateKey key = kf.generatePrivate(keysp);

        // loading CertificateChain
        CertificateFactory cf = CertificateFactory.getInstance("X.509");

        @SuppressWarnings("rawtypes")
        Collection c = cf.generateCertificates(certstream) ;
        Certificate[] certs = new Certificate[c.toArray().length];

        certs = (Certificate[])c.toArray(new Certificate[0]);

        // storing keystore
        ks.setKeyEntry(keyAlias, key, keyPassword.toCharArray(), certs);

        if(logger.isDebugEnabled())
        {
            logger.debug("Key and certificate stored.");
            logger.debug("Alias:"+ keyAlias);
        }
        OutputStream keyStoreOutStream = getKeyStoreOutStream();
        ks.store(keyStoreOutStream, keyPassword.toCharArray());
        // Workaround for MNT-15005
        keyStoreOutStream.close();
    }
    finally
    {
        if(keyInfoManager != null)
        {
            keyInfoManager.clear();
        }

        writeLock.unlock();
    }
}
 
開發者ID:Alfresco,項目名稱:alfresco-core,代碼行數:52,代碼來源:AlfrescoKeyStoreImpl.java

示例10: main

import java.security.cert.CertificateFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {

        byte[] pattern = "#! java BlacklistedCertsConverter ".getBytes();
        String mdAlg = "";

        for (int i=0; ; i++) {
            int n = System.in.read();
            if (n < 0) {
                throw new Exception("Unexpected EOF");
            }
            if (i < pattern.length) {
                if (n != pattern[i]) {
                    throw new Exception("The first line must start with \""
                            + new String(pattern) + "\"");
                }
            } else if (i < pattern.length + 100) {
                if (n < 32) {
                    break;
                } else {
                    mdAlg = mdAlg + String.format("%c", n);
                }
            }
        }

        mdAlg = mdAlg.trim();
        System.out.println("Algorithm=" + mdAlg);

        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Collection<? extends Certificate> certs
                = cf.generateCertificates(System.in);

        // Output sorted so that it's easy to locate an entry.
        Set<String> fingerprints = new TreeSet<>();
        for (Certificate cert: certs) {
            fingerprints.add(
                    getCertificateFingerPrint(mdAlg, (X509Certificate)cert));
        }

        for (String s: fingerprints) {
            System.out.println(s);
        }
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:43,代碼來源:BlacklistedCertsConverter.java


注:本文中的java.security.cert.CertificateFactory.generateCertificates方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。