2016-05-18 02:51:34 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:34 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/evp.h>
|
2020-02-11 13:29:08 +08:00
|
|
|
#include <openssl/serializer.h>
|
|
|
|
#include <openssl/buffer.h>
|
2007-11-20 21:37:51 +08:00
|
|
|
#include <openssl/x509.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/asn1.h"
|
|
|
|
#include "crypto/evp.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (a->ameth && a->ameth->old_priv_encode) {
|
|
|
|
return a->ameth->old_priv_encode(a, pp);
|
|
|
|
}
|
|
|
|
if (a->ameth && a->ameth->priv_encode) {
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8 = EVP_PKEY2PKCS8(a);
|
2016-05-10 03:52:11 +08:00
|
|
|
int ret = 0;
|
|
|
|
if (p8 != NULL) {
|
|
|
|
ret = i2d_PKCS8_PRIV_KEY_INFO(p8, pp);
|
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8);
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2020-02-21 03:26:16 +08:00
|
|
|
if (a->keymgmt != NULL) {
|
2020-02-11 13:29:08 +08:00
|
|
|
const char *serprop = OSSL_SERIALIZER_PrivateKey_TO_DER_PQ;
|
|
|
|
OSSL_SERIALIZER_CTX *ctx =
|
|
|
|
OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(a, serprop);
|
|
|
|
BIO *out = BIO_new(BIO_s_mem());
|
|
|
|
BUF_MEM *buf = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (ctx != NULL
|
|
|
|
&& out != NULL
|
|
|
|
&& OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL
|
|
|
|
&& OSSL_SERIALIZER_to_bio(ctx, out)
|
|
|
|
&& BIO_get_mem_ptr(out, &buf) > 0) {
|
|
|
|
ret = buf->length;
|
|
|
|
|
|
|
|
if (pp != NULL) {
|
|
|
|
if (*pp == NULL) {
|
|
|
|
*pp = (unsigned char *)buf->data;
|
|
|
|
buf->length = 0;
|
|
|
|
buf->data = NULL;
|
|
|
|
} else {
|
|
|
|
memcpy(*pp, buf->data, ret);
|
|
|
|
*pp += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BIO_free(out);
|
|
|
|
OSSL_SERIALIZER_CTX_free(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1err(ASN1_F_I2D_PRIVATEKEY, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
|
2016-05-10 03:52:11 +08:00
|
|
|
return -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|