简体   繁体   English

如何在 Java 密钥库中导入现有的 X.509 证书和私钥以在 SSL 中使用?

[英]How to import an existing X.509 certificate and private key in Java keystore to use in SSL?

I have this in an ActiveMQ config:我在 ActiveMQ 配置中有这个:

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

I have a pair of X.509 cert and a key file.我有一对 X.509 证书和一个密钥文件。

How do I import those two in order to use them in SSL and SSL+stomp connectors?如何导入这两个以便在 SSL 和 SSL+stomp 连接器中使用它们? All examples I could google always generate the key themselves, but I already have a key.我可以用谷歌搜索的所有示例总是自己生成密钥,但我已经有了一个密钥。

I have tried我努力了

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

but this only imports the certificate and not the key file and results in但这只会导入证书而不是密钥文件并导致

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.

I have tried concatenating the cert and the key but got the same result.我尝试连接证书和密钥,但得到了相同的结果。

How do I import the key?如何导入密钥?

I used the following two steps which I found in the comments/posts linked in the other answers:我使用了以下两个步骤,我在其他答案中链接的评论/帖子中找到了这些步骤:

Step one: Convert the x.509 cert and key to a pkcs12 file第一步:将 x.509 证书和密钥转换为 pkcs12 文件

openssl pkcs12 -export -in server.crt -inkey server.key \
               -out server.p12 -name [some-alias] \
               -CAfile ca.crt -caname root

Note: Make sure you put a password on the pkcs12 file - otherwise you'll get a null pointer exception when you try to import it.注意:确保在 pkcs12 文件上设置了密码 - 否则在尝试导入时会出现空指针异常。 (In case anyone else had this headache). (以防其他人有这种头痛)。 ( Thanks jocull! ) 谢谢jocull!

Note 2: You might want to add the -chain option to preserve the full certificate chain.注 2:您可能希望添加-chain选项以保留完整的证书链。 ( Thanks Mafuba ) 感谢马福巴

Step two: Convert the pkcs12 file to a Java keystore第二步:将 pkcs12 文件转换为 Java 密钥库

keytool -importkeystore \
        -deststorepass [changeit] -destkeypass [changeit] -destkeystore server.keystore \
        -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass some-password \
        -alias [some-alias]

Finished完成的

OPTIONAL Step zero: Create self-signed certificate可选步骤零:创建自签名证书

openssl genrsa -out server.key 2048
openssl req -new -out server.csr -key server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Cheers!干杯!

Keytool in Java 6 does have this capability: Importing private keys into a Java keystore using keytool Java 6 中的 Keytool 确实具有此功能: 使用 keytool 将私钥导入 Java 密钥库

Here are the basic details from that post.以下是该帖子的基本细节。

  1. Convert the existing cert to a PKCS12 using OpenSSL.使用 OpenSSL 将现有证书转换为 PKCS12。 A password is required when asked or the 2nd step will complain.询问时需要密码,否则第 2 步会报错。

     openssl pkcs12 -export -in [my_certificate.crt] -inkey [my_key.key] -out [keystore.p12] -name [new_alias] -CAfile [my_ca_bundle.crt] -caname root
  2. Convert the PKCS12 to a Java Keystore File.将 PKCS12 转换为 Java 密钥库文件。

     keytool -importkeystore -deststorepass [new_keystore_pass] -destkeypass [new_key_pass] -destkeystore [keystore.jks] -srckeystore [keystore.p12] -srcstoretype PKCS12 -srcstorepass [pass_used_in_p12_keystore] -alias [alias_used_in_p12_keystore]

Believe or not, keytool does not provide such basic functionality like importing private key to keystore.信不信由你,keytool 并没有提供像将私钥导入密钥库这样的基本功能。 You can try this workaround with merging PKSC12 file with private key to a keystore:您可以通过将 PKSC12 文件与私钥合并到密钥库来尝试此解决方法

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

Or just use more user-friendly KeyMan from IBM for keystore handling instead of keytool.或者只是使用 IBM 提供的更加用户友好的KeyMan来处理密钥库,而不是使用 keytool。

Using Let's Encrypt certificates使用 Let's Encrypt 证书

Assuming you've created your certificates and private keys with Let's Encrypt in /etc/letsencrypt/live/you.com :假设您已经在/etc/letsencrypt/live/you.com使用Let's Encrypt创建了您的证书和私钥:

1. Create a PKCS #12 file 1. 创建一个PKCS #12文件

openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out pkcs.p12 \
        -name letsencrypt

This combines your SSL certificate fullchain.pem and your private key privkey.pem into a single file, pkcs.p12 .fullchain.pem您的 SSL 证书fullchain.pem和您的私钥privkey.pem到一个文件pkcs.p12

You'll be prompted for a password for pkcs.p12 .系统将提示您输入pkcs.p12的密码。

The export option specifies that a PKCS #12 file will be created rather than parsed (according to the manual ). export选项指定将创建而不是解析 PKCS #12 文件(根据手册)。

2. Create the Java keystore 2. 创建 Java 密钥库

keytool -importkeystore -destkeystore keystore.jks -srckeystore pkcs.p12 \
        -srcstoretype PKCS12 -alias letsencrypt

If keystore.jks doesn't exist, it will be created containing the pkcs.12 file created above.如果keystore.jks不存在,它将被创建,其中包含上面创建的pkcs.12文件。 Otherwise, you'll import pkcs.12 into the existing keystore.否则,您会将pkcs.12导入现有的密钥库。


These instructions are derived from the post "Create a Java Keystore (.JKS) from Let's Encrypt Certificates" on this blog .这些说明源自此博客上的“从让我们加密证书创建 Java 密钥库 (.JKS)”一文。

Here's more on the different kind of files in /etc/letsencrypt/live/you.com/ . 这里有更多关于/etc/letsencrypt/live/you.com/不同类型文件的/etc/letsencrypt/live/you.com/

First convert to p12:首先转换为p12:

openssl pkcs12 -export -in [filename-certificate] -inkey [filename-key] -name [host] -out [filename-new-PKCS-12.p12]

Create new JKS from p12:从 p12 创建新的 JKS:

keytool -importkeystore -deststorepass [password] -destkeystore [filename-new-keystore.jks] -srckeystore [filename-new-PKCS-12.p12] -srcstoretype PKCS12

And one more:还有一个:

#!/bin/bash

# We have:
#
# 1) $KEY : Secret key in PEM format ("-----BEGIN RSA PRIVATE KEY-----") 
# 2) $LEAFCERT : Certificate for secret key obtained from some
#    certification outfit, also in PEM format ("-----BEGIN CERTIFICATE-----")   
# 3) $CHAINCERT : Intermediate certificate linking $LEAFCERT to a trusted
#    Self-Signed Root CA Certificate 
#
# We want to create a fresh Java "keystore" $TARGET_KEYSTORE with the
# password $TARGET_STOREPW, to be used by Tomcat for HTTPS Connector.
#
# The keystore must contain: $KEY, $LEAFCERT, $CHAINCERT
# The Self-Signed Root CA Certificate is obtained by Tomcat from the
# JDK's truststore in /etc/pki/java/cacerts

# The non-APR HTTPS connector (APR uses OpenSSL-like configuration, much
# easier than this) in server.xml looks like this 
# (See: https://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html):
#
#  <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
#                SSLEnabled="true"
#                maxThreads="150" scheme="https" secure="true"
#                clientAuth="false" sslProtocol="TLS"
#                keystoreFile="/etc/tomcat6/etl-web.keystore.jks"
#                keystorePass="changeit" />
#

# Let's roll:    

TARGET_KEYSTORE=/etc/tomcat6/foo-server.keystore.jks
TARGET_STOREPW=changeit

TLS=/etc/pki/tls

KEY=$TLS/private/httpd/foo-server.example.com.key
LEAFCERT=$TLS/certs/httpd/foo-server.example.com.pem
CHAINCERT=$TLS/certs/httpd/chain.cert.pem

# ----
# Create PKCS#12 file to import using keytool later
# ----

# From https://www.sslshopper.com/ssl-converter.html:
# The PKCS#12 or PFX format is a binary format for storing the server certificate,
# any intermediate certificates, and the private key in one encryptable file. PFX
# files usually have extensions such as .pfx and .p12. PFX files are typically used 
# on Windows machines to import and export certificates and private keys.

TMPPW=$$ # Some random password

PKCS12FILE=`mktemp`

if [[ $? != 0 ]]; then
  echo "Creation of temporary PKCS12 file failed -- exiting" >&2; exit 1
fi

TRANSITFILE=`mktemp`

if [[ $? != 0 ]]; then
  echo "Creation of temporary transit file failed -- exiting" >&2; exit 1
fi

cat "$KEY" "$LEAFCERT" > "$TRANSITFILE"

openssl pkcs12 -export -passout "pass:$TMPPW" -in "$TRANSITFILE" -name etl-web > "$PKCS12FILE"

/bin/rm "$TRANSITFILE"

# Print out result for fun! Bug in doc (I think): "-pass " arg does not work, need "-passin"

openssl pkcs12 -passin "pass:$TMPPW" -passout "pass:$TMPPW" -in "$PKCS12FILE" -info

# ----
# Import contents of PKCS12FILE into a Java keystore. WTF, Sun, what were you thinking?
# ----

if [[ -f "$TARGET_KEYSTORE" ]]; then
  /bin/rm "$TARGET_KEYSTORE"
fi

keytool -importkeystore \
   -deststorepass  "$TARGET_STOREPW" \
   -destkeypass    "$TARGET_STOREPW" \
   -destkeystore   "$TARGET_KEYSTORE" \
   -srckeystore    "$PKCS12FILE" \
   -srcstoretype  PKCS12 \
   -srcstorepass  "$TMPPW" \
   -alias foo-the-server

/bin/rm "$PKCS12FILE"

# ----
# Import the chain certificate. This works empirically, it is not at all clear from the doc whether this is correct
# ----

echo "Importing chain"

TT=-trustcacerts

keytool -import $TT -storepass "$TARGET_STOREPW" -file "$CHAINCERT" -keystore "$TARGET_KEYSTORE" -alias chain

# ----
# Print contents
# ----

echo "Listing result"

keytool -list -storepass "$TARGET_STOREPW" -keystore "$TARGET_KEYSTORE"

In my case I had a pem file which contained two certificates and an encrypted private key to be used in mutual SSL authentication.就我而言,我有一个 pem 文件,其中包含两个证书和一个用于相互 SSL 身份验证的加密私钥。 So my pem file looked like this:所以我的 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-----

Here is what I did:这是我所做的:

Split the file into three separate files, so that each one contains just one entry, starting with "---BEGIN.." and ending with "---END.." lines.将文件拆分为三个单独的文件,以便每个文件只包含一个条目,以“---BEGIN..”开头并以“---END..”行结尾。 Lets assume we now have three files: cert1.pem cert2.pem and pkey.pem假设我们现在有三个文件:cert1.pem cert2.pem 和 pkey.pem

Convert pkey.pem into DER format using openssl and the following syntax:使用 openssl 和以下语法将 pkey.pem 转换为 DER 格式:

openssl pkcs8 -topk8 -nocrypt -in pkey.pem -inform PEM -out pkey.der -outform DER

Note, that if the private key is encrypted you need to supply a password( obtain it from the supplier of the original pem file ) to convert to DER format, openssl will ask you for the password like this: "enter a pass phraze for pkey.pem: " If conversion is successful, you will get a new file called "pkey.der"请注意,如果私钥是加密的,您需要提供密码(从原始 pem 文件的供应商处获取)以转换为 DER 格式,openssl 将要求您提供这样的密码:“输入 pkey 的密码短语.pem: " 如果转换成功,你会得到一个名为 "pkey.der" 的新文件

Create a new java key store and import the private key and the certificates:创建一个新的 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());

(optional) Verify the content of your new key store: (可选)验证新密钥库的内容:

keytool -list -keystore mykeystore -storepass password

Keystore type: JKS Keystore provider: SUN密钥库类型:JKS 密钥库提供者:SUN

Your keystore contains 3 entries您的密钥库包含 3 个条目

cn=...,ou=...,o=.., Sep 2, 2014, trustedCertEntry, Certificate fingerprint (SHA1): 2C:B8: ... cn=...,ou=...,o=.., 2014 年 9 月 2 日,trustedCertEntry,证书指纹 (SHA1): 2C:B8: ...

importkey, Sep 2, 2014, PrivateKeyEntry, Certificate fingerprint (SHA1): 9C:B0: ... importkey, Sep 2, 2014, PrivateKeyEntry, 证书指纹 (SHA1): 9C:B0: ...

cn=...,o=...., Sep 2, 2014, trustedCertEntry, Certificate fingerprint (SHA1): 83:63: ... cn=...,o=...., 2014 年 9 月 2 日,trustedCertEntry,证书指纹 (SHA1):83:63:...

(optional) Test your certificates and private key from your new key store against your SSL server: ( You may want to enable debugging as an VM option: -Djavax.net.debug=all ) (可选)针对 SSL 服务器测试新密钥库中的证书和私钥:(您可能希望启用调试作为 VM 选项: -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..

Finally register your certificates with HttpsURLConnection if plan to use it:如果计划使用它,最后使用 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);

Yes, it's indeed a sad fact that keytool has no functionality to import a private key.是的,keytool 没有导入私钥的功能确实是一个可悲的事实。

For the record, at the end I went with the solution described here为了记录,最后我使用了这里描述的解决方案

You can use these steps to import the key to an existing keystore.您可以使用这些步骤将密钥导入现有的密钥库。 The instructions are combined from answers in this thread and other sites.这些说明结合了本线程和其他站点中的答案。 These instructions worked for me (the java keystore):这些说明对我有用(Java 密钥库):

  1. Run跑步

openssl pkcs12 -export -in yourserver.crt -inkey yourkey.key -out server.p12 -name somename -certfile yourca.crt -caname root

(If required put the -chain option. Putting that failed for me). (如果需要,放置 -chain 选项。放置对我来说失败了)。 This will ask for the password - you must give the correct password else you will get an error (heading error or padding error etc).这将要求输入密码 - 您必须提供正确的密码,否则您将收到错误(标题错误或填充错误等)。

  1. It will ask you to enter a new password - you must enter a password here - enter anything but remember it.它会要求您输入新密码 - 您必须在此处输入密码 - 输入任何内容,但记住它。 (Let us assume you enter Aragorn). (让我们假设您进入 Aragorn)。
  2. This will create the server.p12 file in the pkcs format.这将创建 pkcs 格式的 server.p12 文件。
  3. Now to import it into the *.jks file run:现在要将其导入*.jks文件,请运行:
    keytool -importkeystore -srckeystore server.p12 -srcstoretype PKCS12 -destkeystore yourexistingjavakeystore.jks -deststoretype JKS -deststorepass existingjavastorepassword -destkeypass existingjavastorepassword
    (Very important - do not leave out the deststorepass and the destkeypass parameters.) (非常重要 - 不要遗漏 deststorepass 和 destkeypass 参数。)
  4. It will ask you for the src key store password.它会询问您 src 密钥库密码。 Enter Aragorn and hit enter.输入阿拉贡并按回车键。 The certificate and key is now imported into your existing java keystore.证书和密钥现在已导入到您现有的 Java 密钥库中。

Based on the answers above, here is how to create a brand new keystore for your java based web server, out of an independently created Comodo cert and private key using keytool (requires JDK 1.6+)根据上面的答案,这里是如何使用 keytool 从独立创建的 Comodo 证书和私钥中为基于 Java 的 Web 服务器创建全新的密钥库(需要 JDK 1.6+)

  1. Issue this command and at the password prompt enter somepass - 'server.crt' is your server's cert and 'server.key' is the private key you used for issuing the CSR: openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 -name www.yourdomain.com -CAfile AddTrustExternalCARoot.crt -caname "AddTrust External CA Root"发出此命令并在密码提示下输入 somepass - “server.crt”是您服务器的证书,“server.key”是您用于发布 CSR 的私钥: openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 -name www.yourdomain.com -CAfile AddTrustExternalCARoot.crt -caname "AddTrust External CA Root"

  2. Then use keytool to convert the p12 keystore into a jks keystore: keytool -importkeystore -deststorepass somepass -destkeypass somepass -destkeystore keystore.jks -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass somepass然后使用keytool将p12 keystore转换成jks keystore: keytool -importkeystore -deststorepass somepass -destkeypass somepass -destkeystore keystore.jks -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass somepass

Then import the other two root/intermediate certs you received from Comodo:然后导入您从 Comodo 收到的另外两个根/中间证书:

  1. Import COMODORSAAddTrustCA.crt: keytool -import -trustcacerts -alias cert1 -file COMODORSAAddTrustCA.crt -keystore keystore.jks导入 COMODORSAAddTrustCA.crt: keytool -import -trustcacerts -alias cert1 -file COMODORSAAddTrustCA.crt -keystore keystore.jks

  2. Import COMODORSADomainValidationSecureServerCA.crt: keytool -import -trustcacerts -alias cert2 -file COMODORSADomainValidationSecureServerCA.crt -keystore keystore.jks导入 COMODORSADomainValidationSecureServerCA.crt: keytool -import -trustcacerts -alias cert2 -file COMODORSADomainValidationSecureServerCA.crt -keystore keystore.jks

Previous answers point out correctly that you can only do this with the standard JDK tools by converting the JKS file into PKCS #12 format first.以前的答案正确地指出,您只能通过首先将 JKS 文件转换为 PKCS #12 格式来使用标准 JDK 工具执行此操作。 If you're interested, I put together a compact utility to import OpenSSL-derived keys into a JKS-formatted keystore without having to convert the keystore to PKCS #12 first: http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art049如果您有兴趣,我整理了一个紧凑的实用程序,可以将 OpenSSL 派生的密钥导入 JKS 格式的密钥库,而无需先将密钥库转换为 PKCS #12: http : //commandlinefanatic.com/cgi-bin/showarticle。 cgi?article=art049

You would use the linked utility like this:您可以像这样使用链接的实用程序:

$ openssl req -x509 -newkey rsa:2048 -keyout localhost.key -out localhost.csr -subj "/CN=localhost"

(sign the CSR, get back localhost.cer) (签署CSR,取回localhost.cer)

$ openssl rsa -in localhost.key -out localhost.rsa
Enter pass phrase for localhost.key:
writing RSA key
$ java -classpath . KeyImport -keyFile localhost.rsa -alias localhost -certificateFile localhost.cer -keystore localhost.jks -keystorePassword changeit -keystoreType JKS -keyPassword changeit

If you have a PEM file (eg server.pem ) containing:如果您有一个包含以下内容的 PEM 文件(例如server.pem ):

  • the trusted certificate受信任的证书
  • the private key私钥

then you can import the certificate and key into a JKS keystore like this:然后您可以将证书和密钥导入 JKS 密钥库,如下所示:

1 ) Copy the private key from the PEM file into an ascii file (eg server.key ) 1 ) 将私钥从 PEM 文件复制到 ascii 文件(例如server.key

2 ) Copy the cert from the PEM file into an ascii file (eg server.crt ) 2 ) 将证书从 PEM 文件复制到 ascii 文件(例如server.crt

3 ) Export the cert and key into a PKCS12 file: 3 ) 将证书和密钥导出到 PKCS12 文件中:

$ openssl pkcs12 -export -in server.crt -inkey server.key \
                 -out server.p12 -name [some-alias] -CAfile server.pem -caname root
  • the PEM file can be used as the argument to the -CAfile option . PEM 文件可用作-CAfile选项的参数
  • you are prompted for an 'export' password.系统会提示您输入“导出”密码。
  • if doing this in git bash then add winpty to the start of the command so the export password can be entered.如果在 git bash 中执行此操作,则将winpty添加到命令的开头,以便可以输入导出密码。

4 ) Convert the PKCS12 file to a JKS keystore: 4 ) 将 PKCS12 文件转换为 JKS 密钥库:

$ keytool -importkeystore -deststorepass changeit -destkeypass changeit \
          -destkeystore keystore.jks  -srckeystore server.p12 -srcstoretype PKCS12 \
          -srcstorepass changeit
  • the srcstorepass password should match the export password from step 3) srcstorepass密码应与步骤 3 中的导出密码匹配)

What I was trying to achieve was using already provided private key and certificate to sign message that was going someplace that needed to make sure that the message was coming from me (private keys sign while public keys encrypt).我试图实现的是使用已经提供的私钥和证书来签署消息,该消息需要确保消息来自我的某个地方(私钥签名而公钥加密)。

So if you already have a .key file and a .crt file?那么,如果您已经有一个 .key 文件和一个 .crt 文件?

Try this:试试这个:

Step1: Convert the key and cert to .p12 file Step1:将密钥和证书转换为 .p12 文件

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

Step 2: Import the key and create a .jsk file with a single command第 2 步:导入密钥并使用单个命令创建一个 .jsk 文件

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

Step 3: In your java:第 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());

If you need to sign some string using this key do the following:如果您需要使用此密钥对某个字符串进行签名,请执行以下操作:

Step 1: Convert the text you want to encrypt第 1 步:转换要加密的文本

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

Step 2: Get base64 encoded private key第二步:获取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));

References:参考:

  1. How to import an existing x509 certificate and private key in Java keystore to use in SSL? 如何在 Java 密钥库中导入现有的 x509 证书和私钥以在 SSL 中使用?
  2. http://tutorials.jenkov.com/java-cryptography/keystore.html http://tutorials.jenkov.com/java-cryptography/keystore.html
  3. http://www.java2s.com/Code/Java/Security/RetrievingaKeyPairfromaKeyStore.htm http://www.java2s.com/Code/Java/Security/RetrievingaKeyPairfromaKeyStore.htm
  4. How to sign string with private key 如何用私钥对字符串进行签名

Final program最终方案

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

Just make a PKCS12 keystore, Java can use it directly now.只需要制作一个PKCS12 keystore,Java现在就可以直接使用了。 In fact, if you list a Java-style keystore, keytool itself alerts you to the fact that PKCS12 is now the preferred format.事实上,如果您列出 Java 样式的密钥库,keytool 本身会提醒您 PKCS12 现在是首选格式这一事实。

openssl pkcs12 -export -in server.crt -inkey server.key \
               -out server.p12 -name [some-alias] \
               -CAfile ca.crt -caname root -chain

You should have received all three files (server.crt, server.key, ca.crt) from your certificate provider.您应该已经从您的证书提供商处收到了所有三个文件(server.crt、server.key、c​​a.crt)。 I am not sure what "-caname root" actually means, but it seems to have to be specified that way.我不确定“-caname root”的实际含义,但似乎必须以这种方式指定。

In the Java code, make sure to specify the right keystore type.在 Java 代码中,确保指定正确的密钥库类型。

KeyStore.getInstance("PKCS12")

I got my comodo.com-issued SSL certificate working fine in NanoHTTPD this way.我的 comodo.com 颁发的 SSL 证书以这种方式在 NanoHTTPD 中正常工作。

在 Elliptic Curve 的情况下并回答问题import an existing x509 certificate and private key in Java keystore ,您可能还想看看这个线程How to read EC Private key in java which is in .pem file format

If you have received a combined cert and key in a single .pem file, like the MongoDB Atlas' authentication, then,如果您在单个 .pem 文件中收到了组合证书和密钥,例如 MongoDB Atlas 的身份验证,那么,

Open the pem file with a text editor and split them into two files, for example cert.pem and key.pem (where you can make a split is very clear in the file) and then use the openssl command to create a single p12 format file like this:用文本编辑器打开pem文件,将它们拆分为两个文件,例如cert.pemkey.pem (文件中可以拆分的地方很清楚),然后使用openssl命令创建单个p12格式像这样的文件:

 openssl pkcs12 -export -out server.p12 -name test\
 -in cert.pem -inkey key.pem

I am using Java 8 and as it turns out at least in Java 8 or later the resulting p12 ( server.p12 ) is now the keystore file so you can use it directly without a need to use the keytool if you do not need to add any more certs to it.我正在使用 Java 8,而且至少在 Java 8 或更高版本中,结果 p12 ( server.p12 ) 现在是密钥库文件,因此您可以直接使用它,而无需使用keytool如果您不需要添加任何更多的证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM