xref: /freebsd/crypto/openssl/crypto/asn1/i2d_evp.c (revision b077aed3)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery  * Low level APIs are deprecated for public use, but still ok for
12*b077aed3SPierre Pronchery  * internal use.
13*b077aed3SPierre Pronchery  */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery 
16*b077aed3SPierre Pronchery #include <stdio.h>
17*b077aed3SPierre Pronchery #include "internal/cryptlib.h"
18*b077aed3SPierre Pronchery #include <openssl/evp.h>
19*b077aed3SPierre Pronchery #include <openssl/encoder.h>
20*b077aed3SPierre Pronchery #include <openssl/buffer.h>
21*b077aed3SPierre Pronchery #include <openssl/x509.h>
22*b077aed3SPierre Pronchery #include <openssl/rsa.h>         /* For i2d_RSAPublicKey */
23*b077aed3SPierre Pronchery #include <openssl/dsa.h>         /* For i2d_DSAPublicKey */
24*b077aed3SPierre Pronchery #include <openssl/ec.h>          /* For i2o_ECPublicKey */
25*b077aed3SPierre Pronchery #include "crypto/asn1.h"
26*b077aed3SPierre Pronchery #include "crypto/evp.h"
27*b077aed3SPierre Pronchery 
28*b077aed3SPierre Pronchery struct type_and_structure_st {
29*b077aed3SPierre Pronchery     const char *output_type;
30*b077aed3SPierre Pronchery     const char *output_structure;
31*b077aed3SPierre Pronchery };
32*b077aed3SPierre Pronchery 
i2d_provided(const EVP_PKEY * a,int selection,const struct type_and_structure_st * output_info,unsigned char ** pp)33*b077aed3SPierre Pronchery static int i2d_provided(const EVP_PKEY *a, int selection,
34*b077aed3SPierre Pronchery                         const struct type_and_structure_st *output_info,
35*b077aed3SPierre Pronchery                         unsigned char **pp)
36*b077aed3SPierre Pronchery {
37*b077aed3SPierre Pronchery     OSSL_ENCODER_CTX *ctx = NULL;
38*b077aed3SPierre Pronchery     int ret;
39*b077aed3SPierre Pronchery 
40*b077aed3SPierre Pronchery     for (ret = -1;
41*b077aed3SPierre Pronchery          ret == -1 && output_info->output_type != NULL;
42*b077aed3SPierre Pronchery          output_info++) {
43*b077aed3SPierre Pronchery         /*
44*b077aed3SPierre Pronchery          * The i2d_ calls don't take a boundary length for *pp.  However,
45*b077aed3SPierre Pronchery          * OSSL_ENCODER_to_data() needs one, so we make one up.  Because
46*b077aed3SPierre Pronchery          * OSSL_ENCODER_to_data() decrements this number by the amount of
47*b077aed3SPierre Pronchery          * bytes written, we need to calculate the length written further
48*b077aed3SPierre Pronchery          * down, when pp != NULL.
49*b077aed3SPierre Pronchery          */
50*b077aed3SPierre Pronchery         size_t len = INT_MAX;
51*b077aed3SPierre Pronchery         int pp_was_NULL = (pp == NULL || *pp == NULL);
52*b077aed3SPierre Pronchery 
53*b077aed3SPierre Pronchery         ctx = OSSL_ENCODER_CTX_new_for_pkey(a, selection,
54*b077aed3SPierre Pronchery                                             output_info->output_type,
55*b077aed3SPierre Pronchery                                             output_info->output_structure,
56*b077aed3SPierre Pronchery                                             NULL);
57*b077aed3SPierre Pronchery         if (ctx == NULL)
58*b077aed3SPierre Pronchery             return -1;
59*b077aed3SPierre Pronchery         if (OSSL_ENCODER_to_data(ctx, pp, &len)) {
60*b077aed3SPierre Pronchery             if (pp_was_NULL)
61*b077aed3SPierre Pronchery                 ret = (int)len;
62*b077aed3SPierre Pronchery             else
63*b077aed3SPierre Pronchery                 ret = INT_MAX - (int)len;
64*b077aed3SPierre Pronchery         }
65*b077aed3SPierre Pronchery         OSSL_ENCODER_CTX_free(ctx);
66*b077aed3SPierre Pronchery         ctx = NULL;
67*b077aed3SPierre Pronchery     }
68*b077aed3SPierre Pronchery 
69*b077aed3SPierre Pronchery     if (ret == -1)
70*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
71*b077aed3SPierre Pronchery     return ret;
72*b077aed3SPierre Pronchery }
73*b077aed3SPierre Pronchery 
i2d_KeyParams(const EVP_PKEY * a,unsigned char ** pp)74*b077aed3SPierre Pronchery int i2d_KeyParams(const EVP_PKEY *a, unsigned char **pp)
75*b077aed3SPierre Pronchery {
76*b077aed3SPierre Pronchery     if (evp_pkey_is_provided(a)) {
77*b077aed3SPierre Pronchery         static const struct type_and_structure_st output_info[] = {
78*b077aed3SPierre Pronchery             { "DER", "type-specific" },
79*b077aed3SPierre Pronchery             { NULL, }
80*b077aed3SPierre Pronchery         };
81*b077aed3SPierre Pronchery 
82*b077aed3SPierre Pronchery         return i2d_provided(a, EVP_PKEY_KEY_PARAMETERS, output_info, pp);
83*b077aed3SPierre Pronchery     }
84*b077aed3SPierre Pronchery     if (a->ameth != NULL && a->ameth->param_encode != NULL)
85*b077aed3SPierre Pronchery         return a->ameth->param_encode(a, pp);
86*b077aed3SPierre Pronchery     ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
87*b077aed3SPierre Pronchery     return -1;
88*b077aed3SPierre Pronchery }
89*b077aed3SPierre Pronchery 
i2d_KeyParams_bio(BIO * bp,const EVP_PKEY * pkey)90*b077aed3SPierre Pronchery int i2d_KeyParams_bio(BIO *bp, const EVP_PKEY *pkey)
91*b077aed3SPierre Pronchery {
92*b077aed3SPierre Pronchery     return ASN1_i2d_bio_of(EVP_PKEY, i2d_KeyParams, bp, pkey);
93*b077aed3SPierre Pronchery }
94*b077aed3SPierre Pronchery 
i2d_PrivateKey(const EVP_PKEY * a,unsigned char ** pp)95*b077aed3SPierre Pronchery int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp)
96*b077aed3SPierre Pronchery {
97*b077aed3SPierre Pronchery     if (evp_pkey_is_provided(a)) {
98*b077aed3SPierre Pronchery         static const struct type_and_structure_st output_info[] = {
99*b077aed3SPierre Pronchery             { "DER", "type-specific" },
100*b077aed3SPierre Pronchery             { "DER", "PrivateKeyInfo" },
101*b077aed3SPierre Pronchery             { NULL, }
102*b077aed3SPierre Pronchery         };
103*b077aed3SPierre Pronchery 
104*b077aed3SPierre Pronchery         return i2d_provided(a, EVP_PKEY_KEYPAIR, output_info, pp);
105*b077aed3SPierre Pronchery     }
106*b077aed3SPierre Pronchery     if (a->ameth != NULL && a->ameth->old_priv_encode != NULL) {
107*b077aed3SPierre Pronchery         return a->ameth->old_priv_encode(a, pp);
108*b077aed3SPierre Pronchery     }
109*b077aed3SPierre Pronchery     if (a->ameth != NULL && a->ameth->priv_encode != NULL) {
110*b077aed3SPierre Pronchery         PKCS8_PRIV_KEY_INFO *p8 = EVP_PKEY2PKCS8(a);
111*b077aed3SPierre Pronchery         int ret = 0;
112*b077aed3SPierre Pronchery 
113*b077aed3SPierre Pronchery         if (p8 != NULL) {
114*b077aed3SPierre Pronchery             ret = i2d_PKCS8_PRIV_KEY_INFO(p8, pp);
115*b077aed3SPierre Pronchery             PKCS8_PRIV_KEY_INFO_free(p8);
116*b077aed3SPierre Pronchery         }
117*b077aed3SPierre Pronchery         return ret;
118*b077aed3SPierre Pronchery     }
119*b077aed3SPierre Pronchery     ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
120*b077aed3SPierre Pronchery     return -1;
121*b077aed3SPierre Pronchery }
122*b077aed3SPierre Pronchery 
i2d_PublicKey(const EVP_PKEY * a,unsigned char ** pp)123*b077aed3SPierre Pronchery int i2d_PublicKey(const EVP_PKEY *a, unsigned char **pp)
124*b077aed3SPierre Pronchery {
125*b077aed3SPierre Pronchery     if (evp_pkey_is_provided(a)) {
126*b077aed3SPierre Pronchery         static const struct type_and_structure_st output_info[] = {
127*b077aed3SPierre Pronchery             { "DER", "type-specific" },
128*b077aed3SPierre Pronchery             { "blob", NULL },    /* for EC */
129*b077aed3SPierre Pronchery             { NULL, }
130*b077aed3SPierre Pronchery         };
131*b077aed3SPierre Pronchery 
132*b077aed3SPierre Pronchery         return i2d_provided(a, EVP_PKEY_PUBLIC_KEY, output_info, pp);
133*b077aed3SPierre Pronchery     }
134*b077aed3SPierre Pronchery     switch (EVP_PKEY_get_base_id(a)) {
135*b077aed3SPierre Pronchery     case EVP_PKEY_RSA:
136*b077aed3SPierre Pronchery         return i2d_RSAPublicKey(EVP_PKEY_get0_RSA(a), pp);
137*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DSA
138*b077aed3SPierre Pronchery     case EVP_PKEY_DSA:
139*b077aed3SPierre Pronchery         return i2d_DSAPublicKey(EVP_PKEY_get0_DSA(a), pp);
140*b077aed3SPierre Pronchery #endif
141*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_EC
142*b077aed3SPierre Pronchery     case EVP_PKEY_EC:
143*b077aed3SPierre Pronchery         return i2o_ECPublicKey(EVP_PKEY_get0_EC_KEY(a), pp);
144*b077aed3SPierre Pronchery #endif
145*b077aed3SPierre Pronchery     default:
146*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
147*b077aed3SPierre Pronchery         return -1;
148*b077aed3SPierre Pronchery     }
149*b077aed3SPierre Pronchery }
150