Remove todos in decode_der2key.c and decode_ms2key.c
[openssl.git] / crypto / pem / pem_local.h
1 /*
2  * Copyright 2019-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 /*
11  * TODO(v3.0): the IMPLEMENT macros in include/openssl/pem.h should be
12  * moved here.
13  */
14
15 #include <openssl/core_dispatch.h>
16 #include <openssl/pem.h>
17 #include <openssl/encoder.h>
18
19 /*
20  * Selectors, named according to the ASN.1 names used throughout libcrypto.
21  *
22  * Note that these are not absolutely mandatory, they are rather a wishlist
23  * of sorts.  The provider implementations are free to make choices that
24  * make sense for them, based on these selectors.
25  * For example, the EC backend is likely to really just output the private
26  * key to a PKCS#8 structure, even thought PEM_SELECTION_PrivateKey specifies
27  * the public key as well.  This is fine, as long as the corresponding
28  * decoding operation can return an object that contains what libcrypto
29  * expects.
30  */
31 # define PEM_SELECTION_PUBKEY           EVP_PKEY_PUBLIC_KEY
32 # define PEM_SELECTION_PrivateKey       EVP_PKEY_KEYPAIR
33 # define PEM_SELECTION_Parameters       EVP_PKEY_KEY_PARAMETERS
34
35 /*
36  * Properties, named according to the ASN.1 names used throughout libcrypto.
37  */
38 # define PEM_STRUCTURE_PUBKEY "SubjectPublicKeyInfo"
39 # define PEM_STRUCTURE_PrivateKey "pkcs8"
40 # define PEM_STRUCTURE_Parameters "type-specific"
41
42 # define PEM_STRUCTURE_RSAPrivateKey "type-specific"
43 # define PEM_STRUCTURE_RSAPublicKey "type-specific"
44
45 /* Alternative IMPLEMENT macros for provided encoders */
46
47 # define IMPLEMENT_PEM_provided_write_body_vars(type, asn1, pq)         \
48     int ret = 0;                                                        \
49     OSSL_ENCODER_CTX *ctx =                                             \
50         OSSL_ENCODER_CTX_new_by_##type(x, PEM_SELECTION_##asn1,         \
51                                        "PEM", PEM_STRUCTURE_##asn1,     \
52                                        (pq));                           \
53                                                                         \
54     if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) {                  \
55         OSSL_ENCODER_CTX_free(ctx);                                     \
56         goto legacy;                                                    \
57     }
58 # define IMPLEMENT_PEM_provided_write_body_pass()                       \
59     ret = 1;                                                            \
60     if (kstr == NULL && cb == NULL) {                                   \
61         if (u != NULL) {                                                \
62             kstr = u;                                                   \
63             klen = strlen(u);                                           \
64         } else {                                                        \
65             cb = PEM_def_callback;                                      \
66         }                                                               \
67     }                                                                   \
68     if (enc != NULL) {                                                  \
69         ret = 0;                                                        \
70         if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc),      \
71                                         NULL)) {                        \
72             ret = 1;                                                    \
73             if (kstr != NULL                                            \
74                 && !OSSL_ENCODER_CTX_set_passphrase(ctx, kstr, klen))   \
75                 ret = 0;                                                \
76             else if (cb != NULL                                         \
77                      && !OSSL_ENCODER_CTX_set_pem_password_cb(ctx,      \
78                                                               cb, u))   \
79                 ret = 0;                                                \
80         }                                                               \
81     }                                                                   \
82     if (!ret) {                                                         \
83         OSSL_ENCODER_CTX_free(ctx);                                     \
84         return 0;                                                       \
85     }
86 # define IMPLEMENT_PEM_provided_write_body_main(type, outtype)          \
87     ret = OSSL_ENCODER_to_##outtype(ctx, out);                          \
88     OSSL_ENCODER_CTX_free(ctx);                                         \
89     return ret
90 # define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,          \
91                                                     writename)          \
92     legacy:                                                             \
93     return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out,    \
94                                 x, NULL, NULL, 0, NULL, NULL)
95 # define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1,       \
96                                                        writename)       \
97     legacy:                                                             \
98     return PEM_ASN1_##writename##((i2d_of_void *)i2d_##asn1, str, out,  \
99                                   x, enc, kstr, klen, cb, u)
100
101 # define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1,         \
102                                          OUTTYPE, outtype, writename)   \
103     PEM_write_fnsig(name, type, OUTTYPE, writename)                     \
104     {                                                                   \
105         IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL);       \
106         IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
107         IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,           \
108                                                    writename);          \
109     }                                                                   \
110     PEM_write_ex_fnsig(name, type, OUTTYPE, writename)                  \
111     {                                                                   \
112         IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq);      \
113         IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
114         IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,           \
115                                                    writename);          \
116     }
117
118
119 # define IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1,      \
120                                             OUTTYPE, outtype, writename) \
121     PEM_write_cb_fnsig(name, type, OUTTYPE, writename)                  \
122     {                                                                   \
123         IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL);       \
124         IMPLEMENT_PEM_provided_write_body_pass();                       \
125         IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
126         IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1,        \
127                                                       writename);       \
128     }                                                                   \
129     PEM_write_ex_cb_fnsig(name, type, OUTTYPE, writename)               \
130     {                                                                   \
131         IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq);      \
132         IMPLEMENT_PEM_provided_write_body_pass();                       \
133         IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
134         IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,           \
135                                                    writename);          \
136     }
137
138 # ifdef OPENSSL_NO_STDIO
139
140 #  define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
141 #  define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
142
143 # else
144
145 #  define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)        \
146     IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, FILE, fp, write)
147 #  define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)     \
148     IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, FILE, fp, write)
149
150 # endif
151
152 # define IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1)        \
153     IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, BIO, bio, write_bio)
154 # define IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1)     \
155     IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, BIO, bio, write_bio)
156
157 # define IMPLEMENT_PEM_provided_write(name, type, str, asn1)    \
158     IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1)     \
159     IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
160
161 # define IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1)         \
162     IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1)          \
163     IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
164
165 # define IMPLEMENT_PEM_provided_rw(name, type, str, asn1)       \
166     IMPLEMENT_PEM_read(name, type, str, asn1)                   \
167     IMPLEMENT_PEM_provided_write(name, type, str, asn1)
168
169 # define IMPLEMENT_PEM_provided_rw_cb(name, type, str, asn1)    \
170     IMPLEMENT_PEM_read(name, type, str, asn1)                   \
171     IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1)
172