loadPrivateKeyPem method

EvpPkey loadPrivateKeyPem(
  1. String pem, {
  2. String? password,
})
inherited

Carrega uma Chave Privada de PEM, opcionalmente decifrando com password.

Implementation

EvpPkey loadPrivateKeyPem(String pem, {String? password}) {
  final bio = createBioFromString(pem);
  Pointer<Uint8>? pw;

  try {
    if (password != null) {
      pw = _allocUtf8z(password);
    }

    // When callback is null but userdata is provided, OpenSSL uses it as password directly
    final pkey = bindings.PEM_read_bio_PrivateKey(
      bio,
      nullptr,
      nullptr, // No callback - OpenSSL will use default behavior
      pw?.cast<Void>() ?? nullptr,
    );

    if (pkey == nullptr) {
      final details = _drainOpenSslErrors(bindings);
      throw OpenSslException(
        'Failed to read private key.\n${details.isEmpty ? '(no OpenSSL error details)' : details}',
      );
    }

    return EvpPkey(pkey, this as dynamic);
  } finally {
    freeBio(bio);
    if (pw != null) calloc.free(pw);
  }
}