Skip to content

Failed to parse the private key file of Ed25519! #1238

Description

@chen0411

try to parser the private key file of ed25519, and fail , how to fix it ?

  1. Private key file content:
    -----BEGIN PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: AES-128-CBC,b619a06a16b7b7a6436579f06a14f45e

QmysBFzoMkgvVTM39kvHjkKhcBjK6PVMZ6a/taF44ZXeOl3t5DUp4EWxyfs8htng
tjsKIb0yKJigIZGrCeHROQ==
-----END PRIVATE KEY-----

  1. exception info:
    org.bouncycastle.openssl.PEMException: problem parsing PRIVATE KEY: java.lang.IllegalArgumentException: failed to construct sequence from byte[]: corrupted stream - out of bounds length found: 108 >= 64
    at org.bouncycastle.openssl.PEMParser$PrivateKeyParser.parseObject(Unknown Source)
    at org.bouncycastle.openssl.PEMParser.readObject(Unknown Source)
    ....

  2. core code:
    ` PrivateKeyInfo pki;
    try (PEMParser pemParser = new PEMParser(new StringReader(encryptPrivateKey))) {
    Object o = pemParser.readObject();
    if (o instanceof PKCS8EncryptedPrivateKeyInfo) {
    PKCS8EncryptedPrivateKeyInfo epki = (PKCS8EncryptedPrivateKeyInfo) o;
    JcePKCSPBEInputDecryptorProviderBuilder builder
    = new JcePKCSPBEInputDecryptorProviderBuilder().setProvider("BC");
    InputDecryptorProvider idp = builder.build(password.toCharArray());
    pki = epki.decryptPrivateKeyInfo(idp);

         } else if (o instanceof PEMEncryptedKeyPair) {
             PEMEncryptedKeyPair epki = (PEMEncryptedKeyPair) o;
             PEMKeyPair pkp = epki.decryptKeyPair(new BcPEMDecryptorProvider(password.toCharArray()));
             pki = pkp.getPrivateKeyInfo();
         } else {
             log.error("Invalid encrypted private key class:{}", o.getClass().getName());
             return null;
         }
         JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
         return converter.getPrivateKey(pki);
     } catch (Exception e) {
         log.error("loadPrivateKey exception:{}", ExceptionUtil.getBriefExceptionStackTrace(e));
     }
     return null;`
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions