How to import a .cer certificate into a java keystore?

后端 未结 8 847
有刺的猬
有刺的猬 2020-11-22 15:38

During the development of a Java webservice client I ran into a problem. Authentication for the webservice is using a client certificate, a username and a password. The clie

相关标签:
8条回答
  • 2020-11-22 16:17

    Here is the code I've been using for programatically importing .cer files into a new KeyStore.

    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    //VERY IMPORTANT.  SOME OF THESE EXIST IN MORE THAN ONE PACKAGE!
    import java.security.GeneralSecurityException;
    import java.security.KeyStore;
    import java.security.cert.Certificate;
    import java.security.cert.CertificateFactory;
    
    //Put everything after here in your function.
    KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null);//Make an empty store
    InputStream fis = /* insert your file path here */;
    BufferedInputStream bis = new BufferedInputStream(fis);
    
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    
    while (bis.available() > 0) {
        Certificate cert = cf.generateCertificate(bis);
        trustStore.setCertificateEntry("fiddler"+bis.available(), cert);
    }
    
    0 讨论(0)
  • 2020-11-22 16:18

    An open source GUI tool is available at keystore-explorer.org

    KeyStore Explorer

    KeyStore Explorer is an open source GUI replacement for the Java command-line utilities keytool and jarsigner. KeyStore Explorer presents their functionality, and more, via an intuitive graphical user interface.

    Following screens will help (they are from the official site)

    Default screen that you get by running the command:

    shantha@shantha:~$./Downloads/kse-521/kse.sh
    

    And go to Examine and Examine a URL option and then give the web URL that you want to import.

    The result window will be like below if you give google site link.

    This is one of Use case and rest is up-to the user(all credits go to the keystore-explorer.org)

    0 讨论(0)
  • 2020-11-22 16:25

    The certificate that you already have is probably the server's certificate, or the certificate used to sign the server's certificate. You will need it so that your web service client can authenticate the server.

    But if additionally you need to perform client authentication with SSL, then you need to get your own certificate, to authenticate your web service client. For this you need to create a certificate request; the process involves creating your own private key, and the corresponding public key, and attaching that public key along with some of your info (email, name, domain name, etc) to a file that's called the certificate request. Then you send that certificate request to the company that's already asked you for it, and they will create your certificate, by signing your public key with their private key, and they'll send you back an X509 file with your certificate, which you can now add to your keystore, and you'll be ready to connect to a web service using SSL requiring client authentication.

    To generate your certificate request, use "keytool -certreq -alias -file -keypass -keystore ". Send the resulting file to the company that's going to sign it.

    When you get back your certificate, run "keytool -importcert -alias -keypass -keystore ".

    You may need to used -storepass in both cases if the keystore is protected (which is a good idea).

    0 讨论(0)
  • 2020-11-22 16:30

    Here's a script I used to batch import a bunch of crt files in the current directory into the java keystore. Just save this to the same folder as your certificate, and run it like so:

    ./import_all_certs.sh
    

    import_all_certs.sh

    KEYSTORE="$(/usr/libexec/java_home)/jre/lib/security/cacerts";
    
    function running_as_root()
    {
      if [ "$EUID" -ne 0 ]
        then echo "NO"
        exit
      fi
    
      echo "YES"
    }
    
    function import_certs_to_java_keystore
    {
      for crt in *.crt; do 
        echo prepping $crt 
        keytool -import -file $crt -storepass changeit -noprompt --alias alias__${crt} -keystore $KEYSTORE
        echo 
      done
    }
    
    if [ "$(running_as_root)" == "YES" ]
    then
      import_certs_to_java_keystore
    else
      echo "This script needs to be run as root!"
    fi
    
    0 讨论(0)
  • 2020-11-22 16:32

    Importing .cer certificate file downloaded from browser (open the url and dig for details) into cacerts keystore in java_home\jre\lib\security worked for me, as opposed to attemps to generate and use my own keystore.

    1. Go to your java_home\jre\lib\security
    2. (Windows) Open admin command line there using cmd and CTRL+SHIFT+ENTER
    3. Run keytool to import certificate:
      • (Replace yourAliasName and path\to\certificate.cer respectively)

     ..\..\bin\keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -alias yourAliasName -file path\to\certificate.cer
    

    This way you don't have to specify any additional JVM options and the certificate should be recognized by the JRE.

    0 讨论(0)
  • 2020-11-22 16:33
    • If you want to authenticate you need the private key - there is no other option.
    • A certificate is a public key with extra properties (like company name, country,...) that is signed by some Certificate authority that guarantees that the attached properties are true.
    • .CER files are certificates and don't have the private key. The private key is provided with a .PFX keystore file normally. If you really authenticate is because you already had imported the private key.
    • You normally can import .CER certificates without any problems with

      keytool -importcert -file certificate.cer -keystore keystore.jks -alias "Alias" 
      
    0 讨论(0)
提交回复
热议问题