| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
#include <openssl/bytestring.h> |
| 6 |
#include <openssl/dsa.h> |
| 7 |
#include <openssl/ec_key.h> |
| 8 |
#include <openssl/err.h> |
| 9 |
#include <openssl/evp.h> |
| 10 |
#include <openssl/rsa.h> |
| 11 |
|
| 12 |
#include "../bytestring/internal.h" |
| 13 |
|
| 14 |
|
| 15 |
int i2d_PrivateKey(const EVP_PKEY *a, uint8_t **pp) { |
| 16 |
switch (EVP_PKEY_id(a)) { |
| 17 |
case EVP_PKEY_RSA: |
| 18 |
return i2d_RSAPrivateKey(EVP_PKEY_get0_RSA(a), pp); |
| 19 |
case EVP_PKEY_EC: |
| 20 |
return i2d_ECPrivateKey(EVP_PKEY_get0_EC_KEY(a), pp); |
| 21 |
case EVP_PKEY_DSA: |
| 22 |
return i2d_DSAPrivateKey(EVP_PKEY_get0_DSA(a), pp); |
| 23 |
default: { |
| 24 |
// Fall back to PKCS#8 for key types without legacy formats (e.g. |
| 25 |
// Ed25519). Initial capacity of 128 should hold most asymmetric keys. |
| 26 |
CBB cbb; |
| 27 |
if (!CBB_init(&cbb, 128) || |
| 28 |
!EVP_marshal_private_key(&cbb, a)) { |
| 29 |
CBB_cleanup(&cbb); |
| 30 |
// Although this file is in crypto/x509 for layering reasons, it emits |
| 31 |
// an error code from ASN1 for OpenSSL compatibility. |
| 32 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
| 33 |
return -1; |
| 34 |
} |
| 35 |
return CBB_finish_i2d(&cbb, pp); |
| 36 |
} |
| 37 |
} |
| 38 |
} |