2012-09-08 8 views
6

में प्रमाणपत्र श्रृंखला जेनरेट करें सवाल यह है कि जावा में प्रोग्रामेटिक रूप से प्रमाणपत्र श्रृंखला कैसे उत्पन्न करें। http://fusesource.com/docs/broker/5.3/security/i382664.htmlजावा

Besically, मैं एक नए ग्राहक के लिए RSA कुंजियों बना सकते हैं:: दूसरे शब्दों में, मैं संचालन यहाँ विस्तृत जावा में प्रदर्शन करने के लिए चाहते हैं

private KeyPair genRSAKeyPair(){ 
    // Get RSA key factory: 
    KeyPairGenerator kpg = null; 
    try { 
     kpg = KeyPairGenerator.getInstance("RSA"); 
    } catch (NoSuchAlgorithmException e) { 
     log.error(e.getMessage()); 
     e.printStackTrace(); 
     return null; 
    } 
    // Generate RSA public/private key pair: 
    kpg.initialize(RSA_KEY_LEN); 
    KeyPair kp = kpg.genKeyPair(); 
    return kp; 

}

और मैं उत्पन्न इसी प्रमाण पत्र:

private X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm) 
    throws GeneralSecurityException, IOException { 
    PrivateKey privkey = pair.getPrivate(); 
    X509CertInfo info = new X509CertInfo(); 
    Date from = new Date(); 
    Date to = new Date(from.getTime() + days * 86400000l); 
    CertificateValidity interval = new CertificateValidity(from, to); 
    BigInteger sn = new BigInteger(64, new SecureRandom()); 
    X500Name owner = new X500Name(dn); 

    info.set(X509CertInfo.VALIDITY, interval); 
    info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn)); 
    info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner)); 
    info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner)); 
    info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic())); 
    info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3)); 
    AlgorithmId algo = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid); 
    info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo)); 

    // Sign the cert to identify the algorithm that's used. 
    X509CertImpl cert = new X509CertImpl(info); 
    cert.sign(privkey, algorithm); 

    // Update the algorith, and resign. 
    algo = (AlgorithmId)cert.get(X509CertImpl.SIG_ALG); 
    info.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algo); 
    cert = new X509CertImpl(info); 
    cert.sign(privkey, algorithm); 
    return cert; 

}

तब मैं प्रमाणपत्र हस्ताक्षर अनुरोध पैदा करते हैं और मैं इसे csrFile फ़ाइल में सहेज लें:

public static void writeCertReq(File csrFile, String alias, String keyPass, KeyStore ks) 
     throws KeyStoreException, 
       NoSuchAlgorithmException, 
       InvalidKeyException, 
       IOException, 
       CertificateException, 
       SignatureException, 
       UnrecoverableKeyException { 

    Object objs[] = getPrivateKey(ks, alias, keyPass.toCharArray()); 
    PrivateKey privKey = (PrivateKey) objs[0]; 

    PKCS10 request = null; 

    Certificate cert = ks.getCertificate(alias); 
    request = new PKCS10(cert.getPublicKey()); 
    String sigAlgName = "MD5WithRSA"; 
    Signature signature = Signature.getInstance(sigAlgName); 
    signature.initSign(privKey); 
    X500Name subject = new X500Name(((X509Certificate) cert).getSubjectDN().toString()); 
    X500Signer signer = new X500Signer(signature, subject); 
    request.encodeAndSign(signer); 
    request.print(System.out); 
    FileOutputStream fos = new FileOutputStream(csrFile); 
    PrintStream ps = new PrintStream(fos); 
    request.print(ps); 
    fos.close(); 
} 

जहां

private static Object[] getPrivateKey(KeyStore ks, String alias, char keyPass[]) 
     throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException { 
    key = null;   
    key = ks.getKey(alias, keyPass); 
    return (new Object[]{ (PrivateKey) key, keyPass }); 
} 

अब मैं सीए निजी कुंजी के साथ सीएसआर पर हस्ताक्षर करना चाहिए, लेकिन मैं नहीं देख सकते हैं कि हासिल करने के लिए जावा में मेरे पास मेरे जेक्स में "मेरा अपना" सीए निजी कुंजी है।

इसके अलावा, एक बार जब मैं सीएसआर पर हस्ताक्षर करने का प्रबंधन करता हूं तो मुझे हस्ताक्षर किए गए सीएसआर के साथ सीए प्रमाण पत्र रखना चाहिए: यह जावा में कैसे किया जा सकता है?

मैं बीसी या अन्य बाहरी libs, बस "sun.security" कक्षाओं का उपयोग नहीं करना पसंद करूंगा।

धन्यवाद।

उत्तर

2

क्षमा करें, लेकिन आपकी इच्छाओं के बावजूद, और अपने सभी क्रिप्टो कोड लिखने के अलावा और इसे अपनी परियोजना (अनुशंसित नहीं) सहित, मैं यहां बाउंसी कैसल का उपयोग करने की सलाह दूंगा।

विशेष रूप से, कृपया https://stackoverflow.com/a/7366757/751158 देखें - जिसमें आप जो भी करना चाहते हैं उसके लिए कोड शामिल है।

+0

ठीक है। ऐसा ही होगा। मैं बीसी को उखाड़ दूंगा और मुझे आपके लिंक का लाभ होगा। धन्यवाद –

1

मुझे लगता है कि आप घर के बाउंसीकास्टल पक्ष में पहले ही चले गए हैं, लेकिन अगर कोई और सोच रहा था; कुंजीस्टोर में कुंजी डालते समय आप प्रविष्टि में प्रमाणपत्र श्रृंखला जोड़ सकते हैं। उदाहरण

// build your certs 

KeyStore keyStore = KeyStore.getInstance("PKCS12"); 
keyStore.load([keystore stream], password.toCharArray());// or null, null if it's a brand new store 
X509Certificate[] chain = new X509Certificate[2]; 
chain[0] = _clientCert; 
chain[1] = _caCert; 
keyStore.setKeyEntry("Alias", _clientCertKey, password.toCharArray(), chain); 
keyStore.store([output stream], password.toCharArray()); 

// do other stuff 
6

के लिए मेरा मानना ​​है कि पद http://www.pixelstech.net/article/1406726666-Generate-certificate-in-Java----2 में कोड उदाहरण बताएंगे कि कैसे शुद्ध जावा के साथ प्रमाणपत्र श्रृंखला उत्पन्न करने के लिए होगा। आपको बाउंसी कैसल का उपयोग करने की आवश्यकता नहीं है।