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

前端 未结 15 862
说谎
说谎 2020-11-22 08:05

I have this in an ActiveMQ config:


        

        
15条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 08:13

    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):
    #
    #  
    #
    
    # 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"
    

提交回复
热议问题