我在ActiveMQ配置中有这个:

<sslContext>
        <sslContext keyStore="file:/home/alex/work/amq/broker.ks"  
 keyStorePassword="password" trustStore="file:${activemq.base}/conf/broker.ts" 
 trustStorePassword="password"/>
</sslContext>

我有一对X.509证书和一个密钥文件。

我如何导入这两个,以便在SSL和SSL+stomp连接器中使用它们?所有的例子,我总能自己生成键,但我已经有一个键了。

我试过了

keytool -import  -keystore ./broker.ks -file mycert.crt

但这只导入证书,而不导入密钥文件,并导致

2009-05-25 13:16:24,270 [localhost:61612] ERROR TransportConnector - Could not accept connection : No available certificate or key corresponds to the SSL cipher suites which are enabled.

我已经尝试连接证书和密钥,但得到相同的结果。

如何导入密钥?


当前回答

是的,keytool没有导入私钥的功能,这确实是一个可悲的事实。

在最后,我使用了这里描述的解决方案

其他回答

在椭圆曲线的情况下,回答这个问题,导入一个现有的x509证书和私钥在Java keystore中,你可能也想看看这个线程如何在Java中读取EC私钥。pem文件格式

信不信由你,keytool并没有提供像将私钥导入keystore这样的基本功能。您可以尝试将PKSC12文件与私钥合并到keystore中:

keytool -importkeystore \
  -deststorepass storepassword \
  -destkeypass keypassword \
  -destkeystore my-keystore.jks \
  -srckeystore cert-and-key.p12 \
  -srcstoretype PKCS12 \
  -srcstorepass p12password \
  -alias 1

或者使用IBM的更友好的KeyMan来处理密钥库,而不是keytool。

是的,keytool没有导入私钥的功能,这确实是一个可悲的事实。

在最后,我使用了这里描述的解决方案

我试图实现的是使用已经提供的私钥和证书对消息进行签名,该消息要发送到某个需要确保消息来自我的地方(私钥签名而公钥加密)。

如果你已经有了。key文件和。crt文件?

试试这个:

步骤1:将密钥和证书转换为.p12文件

openssl pkcs12 -export -in certificate.crt -inkey privateKey.key -name alias -out yourconvertedfile.p12

步骤2:导入密钥并使用单个命令创建.jsk文件

keytool -importkeystore -deststorepass changeit -destkeystore keystore.jks -srckeystore umeme.p12 -srcstoretype PKCS12

步骤3:在java中:

char[] keyPassword = "changeit".toCharArray();

KeyStore keyStore = KeyStore.getInstance("JKS");
InputStream keyStoreData = new FileInputStream("keystore.jks");

keyStore.load(keyStoreData, keyPassword);
KeyStore.ProtectionParameter entryPassword = new KeyStore.PasswordProtection(keyPassword);
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry("alias", entryPassword);

System.out.println(privateKeyEntry.toString());

如果你需要用这个键给某个字符串签名,请执行以下操作:

步骤1:转换要加密的文本

byte[] data = "test".getBytes("UTF8");

步骤2:获取base64编码的私钥

keyStore.load(keyStoreData, keyPassword);

//get cert, pubkey and private key from the store by alias
Certificate cert = keyStore.getCertificate("localhost");
PublicKey publicKey = cert.getPublicKey();
KeyPair keyPair = new KeyPair(publicKey, (PrivateKey) key);

//sign with this alg
Signature sig = Signature.getInstance("SHA1WithRSA");
sig.initSign(keyPair.getPrivate());
sig.update(data);
byte[] signatureBytes = sig.sign();
System.out.println("Signature:" + Base64.getEncoder().encodeToString(signatureBytes));

sig.initVerify(keyPair.getPublic());
sig.update(data);

System.out.println(sig.verify(signatureBytes));

引用:

如何在Java密钥存储库中导入现有的x509证书和私钥以用于SSL? http://tutorials.jenkov.com/java-cryptography/keystore.html http://www.java2s.com/Code/Java/Security/RetrievingaKeyPairfromaKeyStore.htm 如何签署字符串与私钥

最后的程序

public static void main(String[] args) throws Exception {

    byte[] data = "test".getBytes("UTF8");

    // load keystore
    char[] keyPassword = "changeit".toCharArray();

    KeyStore keyStore = KeyStore.getInstance("JKS");
    //System.getProperty("user.dir") + "" < for a file in particular path 
    InputStream keyStoreData = new FileInputStream("keystore.jks");
    keyStore.load(keyStoreData, keyPassword);

    Key key = keyStore.getKey("localhost", keyPassword);

    Certificate cert = keyStore.getCertificate("localhost");

    PublicKey publicKey = cert.getPublicKey();

    KeyPair keyPair = new KeyPair(publicKey, (PrivateKey) key);

    Signature sig = Signature.getInstance("SHA1WithRSA");

    sig.initSign(keyPair.getPrivate());
    sig.update(data);
    byte[] signatureBytes = sig.sign();
    System.out.println("Signature:" + Base64.getEncoder().encodeToString(signatureBytes));

    sig.initVerify(keyPair.getPublic());
    sig.update(data);

    System.out.println(sig.verify(signatureBytes));
}

在我的例子中,我有一个pem文件,其中包含两个证书和一个用于相互SSL身份验证的加密私钥。 我的pem文件是这样的:

-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,C8BF220FC76AA5F9
...
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----

以下是我所做的:

将文件拆分为三个单独的文件,这样每个文件只包含一个条目, 以“——BEGIN..”开头,以“——END..”结尾。让我们假设现在有三个文件:cert1。pem cert2。Pem和pkey.pem

pkey进行转换。使用openssl将pem文件转换为DER格式,并执行如下语法:

Openssl pkcs8 -topk8 -nocrypt -in pkey. exepem -out pkey.der -outform DER . pem

注意,如果私钥是加密的,你需要提供一个密码(从原始pem文件的供应商那里获得) 转换为DER格式, Openssl会像这样要求你输入密码:“enter a pass phrase for pkey。pem:“ 如果转换成功,您将得到一个名为“pkey.der”的新文件。

创建一个新的java密钥存储库,并导入私钥和证书:

String keypass = "password";  // this is a new password, you need to come up with to protect your java key store file
String defaultalias = "importkey";
KeyStore ks = KeyStore.getInstance("JKS", "SUN");

// this section does not make much sense to me, 
// but I will leave it intact as this is how it was in the original example I found on internet:   
ks.load( null, keypass.toCharArray());
ks.store( new FileOutputStream ( "mykeystore"  ), keypass.toCharArray());
ks.load( new FileInputStream ( "mykeystore" ),    keypass.toCharArray());
// end of section..


// read the key file from disk and create a PrivateKey

FileInputStream fis = new FileInputStream("pkey.der");
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[dis.available()];
dis.readFully(bytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

byte[] key = new byte[bais.available()];
KeyFactory kf = KeyFactory.getInstance("RSA");
bais.read(key, 0, bais.available());
bais.close();

PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec ( key );
PrivateKey ff = kf.generatePrivate (keysp);


// read the certificates from the files and load them into the key store:

Collection  col_crt1 = CertificateFactory.getInstance("X509").generateCertificates(new FileInputStream("cert1.pem"));
Collection  col_crt2 = CertificateFactory.getInstance("X509").generateCertificates(new FileInputStream("cert2.pem"));

Certificate crt1 = (Certificate) col_crt1.iterator().next();
Certificate crt2 = (Certificate) col_crt2.iterator().next();
Certificate[] chain = new Certificate[] { crt1, crt2 };

String alias1 = ((X509Certificate) crt1).getSubjectX500Principal().getName();
String alias2 = ((X509Certificate) crt2).getSubjectX500Principal().getName();

ks.setCertificateEntry(alias1, crt1);
ks.setCertificateEntry(alias2, crt2);

// store the private key
ks.setKeyEntry(defaultalias, ff, keypass.toCharArray(), chain );

// save the key store to a file         
ks.store(new FileOutputStream ( "mykeystore" ),keypass.toCharArray());

(可选)验证新密钥存储库的内容:

keytool -list -keystore mykeystore -storepass password

密钥库类型:JKS密钥库提供者:SUN 您的密钥存储库包含3个条目 cn =…,ou =…,o = . ., 2014年9月2日,trustedCertEntry,证书 fingerprint (SHA1): 2C:B8:… importkey, 2014年9月2日,PrivateKeyEntry,证书指纹 (sha1): 9c: b0:… cn =…,o =…, 2014年9月2日,trustedCertEntry,证书指纹 (sha1): 83:63:…

(可选)在SSL服务器上测试新密钥库中的证书和私钥: (你可能想把调试作为一个虚拟机选项:-Djavax.net.debug=all)

        char[] passw = "password".toCharArray();
        KeyStore ks = KeyStore.getInstance("JKS", "SUN");
        ks.load(new FileInputStream ( "mykeystore" ), passw );

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, passw);

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        TrustManager[] tm = tmf.getTrustManagers();

        SSLContext sclx = SSLContext.getInstance("TLS");
        sclx.init( kmf.getKeyManagers(), tm, null);

        SSLSocketFactory factory = sclx.getSocketFactory();
        SSLSocket socket = (SSLSocket) factory.createSocket( "192.168.1.111", 443 );
        socket.startHandshake();

        //if no exceptions are thrown in the startHandshake method, then everything is fine..

最后注册你的证书与HttpsURLConnection如果计划使用它:

        char[] passw = "password".toCharArray();
        KeyStore ks = KeyStore.getInstance("JKS", "SUN");
        ks.load(new FileInputStream ( "mykeystore" ), passw );

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, passw);

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        TrustManager[] tm = tmf.getTrustManagers();

        SSLContext sclx = SSLContext.getInstance("TLS");
        sclx.init( kmf.getKeyManagers(), tm, null);

        HostnameVerifier hv = new HostnameVerifier()
        {
            public boolean verify(String urlHostName, SSLSession session)
            {
                if (!urlHostName.equalsIgnoreCase(session.getPeerHost()))
                {
                    System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'.");
                }
                return true;
            }
        };

        HttpsURLConnection.setDefaultSSLSocketFactory( sclx.getSocketFactory() );
        HttpsURLConnection.setDefaultHostnameVerifier(hv);