2012-09-08 22 views
6

質問は、証明書チェーンをJavaでプログラムによって生成する方法です。 http://fusesource.com/docs/broker/5.3/security/i382664.html証明書チェーンをJavaで生成する

Besicallyが、私は新しいクライアントのためのRSAキーを作成することができます:他の言葉では、私はJavaで、ここで詳細な動作を実行したいと思います

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ファイルに保存します。

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 }); 
} 

今、私はCAの秘密鍵とCSRに署名する必要がありますが、私はachiveする方法を見ることができない

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(); 
} 

それはjavaのものです。私は私のjksに "私自身の" CA秘密鍵を持っています。

さらに、私がCSRに署名すると、署名されたCSRでCA証明書を連鎖する必要があります。これはどのようにjavaで行うことができますか?

私は、bcや他の外部ライブラリ、単に "sun.security"クラスを使用しないことをお勧めします。

ありがとうございます。

答えて

2

申し訳ありませんが、あなたの欲望にもかかわらず、あなたの暗号コードをすべて書き、あなたのプロジェクトにそれを含めること(推奨しません)のほかに、ここでBouncy Castleを使用することをお勧めします。

詳しくはhttps://stackoverflow.com/a/7366757/751158を参照してください。これにはあなたが探しているものを正確に示すコードが含まれています。

+0

それで。私は紀元前になるだろうし、私はあなたのリンクの利益を得るだろう。ありがとう –

1

私はすでに家のBouncyCastle側に行ってきましたが、他の誰かが不思議に思っていたのを見ると、 KeyStoreにキーを置くときに、エントリに証明書チェーンを追加することができます。例

// 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 
関連する問題