Fix grammar in certificates.txt
[openssl.git] / crypto / asn1 / i2d_pr.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/encoder.h>
14 #include <openssl/buffer.h>
15 #include <openssl/x509.h>
16 #include "crypto/asn1.h"
17 #include "crypto/evp.h"
18
19 int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp)
20 {
21     if (a->ameth && a->ameth->old_priv_encode) {
22         return a->ameth->old_priv_encode(a, pp);
23     }
24     if (a->ameth && a->ameth->priv_encode) {
25         PKCS8_PRIV_KEY_INFO *p8 = EVP_PKEY2PKCS8(a);
26         int ret = 0;
27         if (p8 != NULL) {
28             ret = i2d_PKCS8_PRIV_KEY_INFO(p8, pp);
29             PKCS8_PRIV_KEY_INFO_free(p8);
30         }
31         return ret;
32     }
33     if (a->keymgmt != NULL) {
34         /* The private key includes everything */
35         int selection =
36             OSSL_KEYMGMT_SELECT_ALL_PARAMETERS | OSSL_KEYMGMT_SELECT_KEYPAIR;
37         OSSL_ENCODER_CTX *ctx =
38             OSSL_ENCODER_CTX_new_by_EVP_PKEY(a, "DER", selection, NULL, NULL);
39         BIO *out = BIO_new(BIO_s_mem());
40         BUF_MEM *buf = NULL;
41         int ret = -1;
42
43         if (ctx != NULL
44             && out != NULL
45             && OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
46             && OSSL_ENCODER_to_bio(ctx, out)
47             && BIO_get_mem_ptr(out, &buf) > 0) {
48             ret = buf->length;
49
50             if (pp != NULL) {
51                 if (*pp == NULL) {
52                     *pp = (unsigned char *)buf->data;
53                     buf->length = 0;
54                     buf->data = NULL;
55                 } else {
56                     memcpy(*pp, buf->data, ret);
57                     *pp += ret;
58                 }
59             }
60         }
61         BIO_free(out);
62         OSSL_ENCODER_CTX_free(ctx);
63         return ret;
64     }
65     ASN1err(ASN1_F_I2D_PRIVATEKEY, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
66     return -1;
67 }