简体   繁体   English

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

[英]Unable to load and store keystore from p12 inside J2EE container

I was trying to load the keystore from a p12 file , the behaviour is highly inconsistent where in the keystore.aliases().nextElement() gives proper alias once and displaying CN in other cases. 我试图从p12文件加载密钥库,行为在keystore.aliases()。nextElement()一次提供适当别名并在其他情况下显示CN的地方非常不一致。 In the later case I am not able to store the keystore (using keystore.store) and the output stream is empty . 在后一种情况下,我无法存储密钥库(使用keystore.store),并且输出流为空。

Below is the code snippet. 下面是代码片段。 Let me know if I overlooked anything. 让我知道是否忽略了任何内容。

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

Looks like the keystore.load() is not using "SunJSSE" as the default keystore provider in my j2ee environment rather it was using oraclepki provider. 看起来keystore.load()在我的j2ee环境中没有使用“ SunJSSE”作为默认的密钥存储提供程序,而是使用了oraclepki提供程序。 Now that i am loading keystore.load(Is,"SunJSSE") it is able to load properly. 现在,我正在加载keystore.load(Is,“ SunJSSE”),它能够正确加载。

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

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