Java keytool easy way to add server cert from url/port

前端 未结 5 1959
攒了一身酷
攒了一身酷 2020-11-27 11:24

I have a server with a self signed certificate, but also requires client side cert authentication. I am having a rough time trying to get the raw CA server cert so I can imp

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 11:37

    Was looking at how to trust a certificate while using jenkins cli, and found https://issues.jenkins-ci.org/browse/JENKINS-12629 which has some recipe for that.

    This will give you the certificate:

    openssl s_client -connect ${HOST}:${PORT} 

    if you are interested only in the certificate part, cut it out by piping it to:

    | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
    

    and redirect to a file:

    > ${HOST}.cert
    

    Then import it using keytool:

    keytool -import -noprompt -trustcacerts -alias ${HOST} -file ${HOST}.cert \
        -keystore ${KEYSTOREFILE} -storepass ${KEYSTOREPASS}
    

    In one go:

    HOST=myhost.example.com
    PORT=443
    KEYSTOREFILE=dest_keystore
    KEYSTOREPASS=changeme
    
    # get the SSL certificate
    openssl s_client -connect ${HOST}:${PORT}  ${HOST}.cert
    
    # create a keystore and import certificate
    keytool -import -noprompt -trustcacerts \
        -alias ${HOST} -file ${HOST}.cert \
        -keystore ${KEYSTOREFILE} -storepass ${KEYSTOREPASS}
    
    # verify we've got it.
    keytool -list -v -keystore ${KEYSTOREFILE} -storepass ${KEYSTOREPASS} -alias ${HOST}
    

提交回复
热议问题