无法从J2EE容器中的p12加载和存储密钥库

时间:2014-11-25 05:45:13

标签: java java-ee keystore

我试图从p12文件加载密钥库,行为高度不一致,在keystore.aliases()中,nextElement()提供一次正确的别名,并在其他情况下显示CN。在后一种情况下,我无法存储密钥库(使用keystore.store),输出流为空。

以下是代码段。如果我忽略了任何事情,请告诉我。

//  the main code where i am facing issue
private byte[] generateKeyStoreData(String appName, Map<String, String> credentials) 
        throws ApplicationException {
    try {
        if (!credentials.containsKey(KEYSTORE)) {
            throw new NullPointerException("No keystore provided");
        }
        if (!credentials.containsKey(KEYSTORE_PASSWORD)) {
            throw new NullPointerException("No keystore password provided");
        }

        String keystoreStr = credentials.get(KEYSTORE);
        char[] keystorePass = credentials.get(KEYSTORE_PASSWORD).toCharArray();

         // I have printed the base64 string here and tried loading inside a standalone code 
         and  it is working. The method is below
        InputStream keystoreIs = base64stringToInputStream(keystoreStr);


        KeyStore keyStore = KeyStore.getInstance("PKCS12");


        keyStore.load(keystoreIs, keystorePass);

        // I printed the keyStore.aliases().nextElement() which returns correct alias "omss" 
        // but returns CN in cases where it fails.

        ByteArrayOutputStream keyStoreOut = new ByteArrayOutputStream();
        keyStore.store(keyStoreOut, keystorePass);

        // I printed the keystoreOut.toByteArray() and it is empty in failing cases
        return keyStoreOut.toByteArray();

    } catch (Exception e) {
              // exception
    }
}

// the conversion code from base64string to bytearrayinputstream

 private InputStream base64stringToInputStream(String str) {
    byte[] ba = DatatypeConverter.parseBase64Binary(str);
    return new ByteArrayInputStream(ba);
}

  //--------------------------------------------------------------------
  // Below is api which calls the generateKeystore
  //-------------------------------------------------------------------

//    We get the inputstream from the uploaded p12 file and the below api is called

 public void createKeystore(InputStream certFile,
        char[] password) {
    Util.nullCheck(certFile,
            "Certificate File cannot be null or empty");
    Util.nullCheck(password, "Password Cannot be null");
    try {

        // the method is below
        byte[] raw = toByteArray(certFile);

        // converting to base64 string 
        String base64encodedString = DatatypeConverter
                .printBase64Binary(raw);

         //....... we create a map of keystore string and password 
         // and the call is made to generateKeystore method above


        }
      catch(Exception e){
      }



// the inputstream is converted to bytearray inputstream
private static byte[] toByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int reads = is.read();

    while (reads != -1) {
        baos.write(reads);
        reads = is.read();
    }

    return baos.toByteArray();
}

1 个答案:

答案 0 :(得分:0)

看起来keystore.load()在我的j2ee环境中没有使用“SunJSSE”作为默认的密钥库提供程序,而是使用了oraclepki提供程序。现在我正在加载keystore.load(是,“SunJSSE”),它能够正确加载。