⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keytools.java.14

📁 一个免费的CA,基于EJB平台的,老师叫我们测试,现把之共享出来让大家参考
💻 14
📖 第 1 页 / 共 2 页
字号:
                            chain[i].getEncoded()));                // Set attributes on CA-cert                PKCS12BagAttributeCarrier caBagAttr = (PKCS12BagAttributeCarrier) chain[i];                // We constuct a friendly name for the CA, and try with some parts from the DN if they exist.                String cafriendly = CertTools.getPartFromDN(CertTools.getSubjectDN(cacert), "CN");                // On the ones below we +i to make it unique, O might not be otherwise                if (cafriendly == null) {                    cafriendly = CertTools.getPartFromDN(CertTools.getSubjectDN(cacert), "O")+i;                }                if (cafriendly == null) {                    cafriendly = CertTools.getPartFromDN(CertTools.getSubjectDN(cacert), "OU"+i);                }                if (cafriendly == null) {                    cafriendly = "CA_unknown"+i;                }                caBagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName,                    new DERBMPString(cafriendly));            }        }        // Set attributes on user-cert        PKCS12BagAttributeCarrier certBagAttr = (PKCS12BagAttributeCarrier) chain[0];        certBagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias));        // in this case we just set the local key id to that of the public key        certBagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, createSubjectKeyId(chain[0].getPublicKey()));        // "Clean" private key, i.e. remove any old attributes        KeyFactory keyfact = KeyFactory.getInstance(privKey.getAlgorithm(), "BC");        PrivateKey pk = keyfact.generatePrivate(new PKCS8EncodedKeySpec(privKey.getEncoded()));        // Set attributes for private key        PKCS12BagAttributeCarrier keyBagAttr = (PKCS12BagAttributeCarrier) pk;        // in this case we just set the local key id to that of the public key        keyBagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias));        keyBagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, createSubjectKeyId(chain[0].getPublicKey()));        // store the key and the certificate chain        KeyStore store = KeyStore.getInstance("PKCS12", "BC");        store.load(null, null);        store.setKeyEntry(alias, pk, null, chain);        log.debug("<createP12: alias=" + alias + ", privKey, cert=" + CertTools.getSubjectDN(cert) + ", cachain.length=" + ((cachain == null) ? 0 : cachain.length));        return store;    } // createP12    /**     * Creates JKS-file that can be used with JDK. The alias for the private key is set to     * 'privateKey' and the private key password is null.     *     * @param alias the alias used for the key entry     * @param privKey RSA private key     * @param password user's password     * @param cert user certificate     * @param cachain CA-certificate chain or null if only one cert in chain, in that case use     *        'cert'.     *     * @return KeyStore containing JKS-keystore     *     * @exception Exception if input parameters are not OK or certificate generation fails     */    public static KeyStore createJKS(String alias, PrivateKey privKey, String password,        X509Certificate cert, Certificate[] cachain) throws Exception {        log.debug(">createJKS: alias=" + alias + ", privKey, cert=" + CertTools.getSubjectDN(cert) +            ", cachain.length=" + ((cachain == null) ? 0 : cachain.length));        String caAlias = "cacert";        // Certificate chain        if (cert == null) {            throw new IllegalArgumentException("Parameter cert cannot be null.");        }        int len = 1;        if (cachain != null) {            len += cachain.length;        }        Certificate[] chain = new Certificate[len];        chain[0] = cert;        if (cachain != null) {            for (int i = 0; i < cachain.length; i++) {                chain[i + 1] = cachain[i];            }        }        // store the key and the certificate chain        KeyStore store = KeyStore.getInstance("JKS");        store.load(null, null);        // First load the key entry        X509Certificate[] usercert = new X509Certificate[1];        usercert[0] = cert;        store.setKeyEntry(alias, privKey, password.toCharArray(), usercert);        // Add the root cert as trusted        if (cachain != null) {            if (!CertTools.isSelfSigned((X509Certificate) cachain[cachain.length - 1])) {                throw new IllegalArgumentException("Root cert is not self-signed.");            }            store.setCertificateEntry(caAlias, cachain[cachain.length - 1]);        }        // Set the complete chain        log.debug("Storing cert chain of length " + chain.length);        store.setKeyEntry(alias, privKey, password.toCharArray(), chain);        log.debug("<createJKS: alias=" + alias + ", privKey, cert=" + CertTools.getSubjectDN(cert) +            ", cachain.length=" + ((cachain == null) ? 0 : cachain.length));        return store;    } // createJKS    /**     * Retrieves the certificate chain from a keystore.     *     * @param keyStore the keystore, which has been loaded and opened.     * @param privateKeyAlias the alias of the privatekey for which the certchain belongs.     *     * @return array of Certificate, or null if no certificates are found.     */    public static Certificate[] getCertChain(KeyStore keyStore, String privateKeyAlias)        throws KeyStoreException {        log.debug(">getCertChain: alias='" + privateKeyAlias + "'");        Certificate[] certchain = keyStore.getCertificateChain(privateKeyAlias);        if (certchain == null) {            return null;        }        log.debug("Certchain retrieved from alias '" + privateKeyAlias + "' has length " +            certchain.length);        if (certchain.length < 1) {            log.error("Cannot load certificate chain with alias '" + privateKeyAlias +                "' from keystore.");            log.debug("<getCertChain: alias='" + privateKeyAlias + "', retlength=" +                certchain.length);            return certchain;        } else if (certchain.length > 0) {            if (CertTools.isSelfSigned((X509Certificate) certchain[certchain.length - 1])) {                log.debug("Issuer='" +                    CertTools.getIssuerDN((X509Certificate) certchain[certchain.length - 1]) +                    "'.");                log.debug("Subject='" +                    CertTools.getSubjectDN((X509Certificate) certchain[certchain.length - 1]) +                    "'.");                log.debug("<getCertChain: alias='" + privateKeyAlias + "', retlength=" +                    certchain.length);                return certchain;            }        }        // If we came here, we have a cert which is not root cert in 'cert'        ArrayList array = new ArrayList();        for (int i = 0; i < certchain.length; i++) {            array.add(certchain[i]);        }        boolean stop = false;        while (!stop) {            X509Certificate cert = (X509Certificate) array.get(array.size() - 1);            String ialias = CertTools.getPartFromDN(CertTools.getIssuerDN(cert), "CN");            Certificate[] chain1 = keyStore.getCertificateChain(ialias);            if (chain1 == null) {                stop = true;            } else {                log.debug("Loaded certificate chain with length " + chain1.length +                    " with alias '" + ialias + "'.");                if (chain1.length == 0) {                    log.error("No RootCA certificate found!");                    stop = true;                }                for (int j = 0; j < chain1.length; j++) {                    array.add(chain1[j]);                    // If one cert is slefsigned, we have found a root certificate, we don't need to go on anymore                    if (CertTools.isSelfSigned((X509Certificate) chain1[j])) {                        stop = true;                    }                }            }        }        Certificate[] ret = new Certificate[array.size()];        for (int i = 0; i < ret.length; i++) {            ret[i] = (X509Certificate) array.get(i);            log.debug("Issuer='" + CertTools.getIssuerDN((X509Certificate) ret[i]) + "'.");            log.debug("Subject='" + CertTools.getSubjectDN((X509Certificate) ret[i]) + "'.");        }        log.debug("<getCertChain: alias='" + privateKeyAlias + "', retlength=" + ret.length);        return ret;    } // getCertChain    /**     * create the subject key identifier.     *     * @param pubKey the public key     *     * @return SubjectKeyIdentifer asn.1 structure     */    public static SubjectKeyIdentifier createSubjectKeyId(PublicKey pubKey) {        try {            ByteArrayInputStream bIn = new ByteArrayInputStream(pubKey.getEncoded());            SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence) new ASN1InputStream(                        bIn).readObject());            return new SubjectKeyIdentifier(info);        } catch (Exception e) {            throw new RuntimeException("error creating key");        }    } // createSubjectKeyId    } // KeyTools

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -