]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/encoders.inc
Decoding PKCS#8: separate decoding of encrypted and unencrypted PKCS#8
[thirdparty/openssl.git] / providers / encoders.inc
CommitLineData
ece9304c 1/*
a28d06f3 2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
ece9304c
RL
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
c319b627
RL
10#ifndef ENCODER_PROVIDER
11# error Macro ENCODER_PROVIDER undefined
ece9304c
RL
12#endif
13
c319b627
RL
14#define ENCODER_STRUCTURE_type_specific_keypair "type-specific"
15#define ENCODER_STRUCTURE_type_specific_params "type-specific"
16#define ENCODER_STRUCTURE_type_specific "type-specific"
17#define ENCODER_STRUCTURE_type_specific_no_pub "type-specific"
6a2b8ff3 18#define ENCODER_STRUCTURE_PrivateKeyInfo "PrivateKeyInfo"
c319b627
RL
19#define ENCODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo"
20#define ENCODER_STRUCTURE_DH "dh"
21#define ENCODER_STRUCTURE_DHX "dhx"
22#define ENCODER_STRUCTURE_DSA "dsa"
23#define ENCODER_STRUCTURE_EC "ec"
24#define ENCODER_STRUCTURE_RSA "rsa"
25#define ENCODER_STRUCTURE_PKCS1 "pkcs1"
26#define ENCODER_STRUCTURE_PKCS3 "pkcs3"
27#define ENCODER_STRUCTURE_X9_42 "X9.42"
28#define ENCODER_STRUCTURE_X9_62 "X9.62"
29
30/* Arguments are prefixed with '_' to avoid build breaks on certain platforms */
31#define ENCODER_TEXT(_name, _sym, _fips) \
32 { _name, \
33 "provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=text", \
34 (ossl_##_sym##_to_text_encoder_functions) }
0cc0164d
RL
35#define ENCODER(_name, _sym, _fips, _output) \
36 { _name, \
37 "provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=" #_output, \
38 (ossl_##_sym##_to_##_output##_encoder_functions) }
39#define ENCODER_w_structure(_name, _sym, _fips, _output, _structure) \
c319b627
RL
40 { _name, \
41 "provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=" #_output \
42 ",structure=" ENCODER_STRUCTURE_##_structure, \
43 (ossl_##_sym##_to_##_structure##_##_output##_encoder_functions) }
44
45/*
46 * Entries for human text "encoders"
47 */
48ENCODER_TEXT("RSA", rsa, yes),
49ENCODER_TEXT("RSA-PSS", rsapss, yes),
50#ifndef OPENSSL_NO_DH
51ENCODER_TEXT("DH", dh, yes),
52ENCODER_TEXT("DHX", dhx, yes),
53#endif
54#ifndef OPENSSL_NO_DSA
55ENCODER_TEXT("DSA", dsa, yes),
56#endif
57#ifndef OPENSSL_NO_EC
58ENCODER_TEXT("EC", ec, yes),
59ENCODER_TEXT("ED25519", ed25519, yes),
60ENCODER_TEXT("ED448", ed448, yes),
61ENCODER_TEXT("X25519", x25519, yes),
62ENCODER_TEXT("X448", x448, yes),
f2db0528
RL
63# ifndef OPENSSL_NO_SM2
64ENCODER_TEXT("SM2", sm2, yes),
65# endif
c319b627
RL
66#endif
67
68/*
69 * Entries for key type specific output formats. The structure name on these
70 * is the same as the key type name. This allows us to say something like:
71 *
72 * To replace i2d_{TYPE}PrivateKey(), i2d_{TYPE}PublicKey() and
73 * i2d_{TYPE}Params(), use OSSL_ENCODER functions with an OSSL_ENCODER_CTX
74 * created like this:
75 *
76 * OSSL_ENCODER_CTX *ctx =
fe75766c
TM
77 * OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER", "type-specific",
78 * NULL, NULL);
c319b627
RL
79 *
80 * To replace PEM_write_bio_{TYPE}PrivateKey(), PEM_write_bio_{TYPE}PublicKey()
81 * and PEM_write_bio_{TYPE}Params(), use OSSL_ENCODER functions with an
82 * OSSL_ENCODER_CTX created like this:
83 *
84 * OSSL_ENCODER_CTX *ctx =
fe75766c
TM
85 * OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "PEM", "type-specific",
86 * NULL, NULL);
c319b627
RL
87 *
88 * We only implement those for which there are current i2d_ and PEM_write_bio
89 * implementations.
90 */
91
92/* The RSA encoders only support private key and public key output */
0cc0164d
RL
93ENCODER_w_structure("RSA", rsa, yes, der, type_specific_keypair),
94ENCODER_w_structure("RSA", rsa, yes, pem, type_specific_keypair),
c319b627
RL
95#ifndef OPENSSL_NO_DH
96/* DH and X9.42 DH only support key parameters output. */
0cc0164d
RL
97ENCODER_w_structure("DH", dh, yes, der, type_specific_params),
98ENCODER_w_structure("DH", dh, yes, pem, type_specific_params),
99ENCODER_w_structure("DHX", dhx, yes, der, type_specific_params),
100ENCODER_w_structure("DHX", dhx, yes, pem, type_specific_params),
c319b627
RL
101#endif
102#ifndef OPENSSL_NO_DSA
0cc0164d
RL
103ENCODER_w_structure("DSA", dsa, yes, der, type_specific),
104ENCODER_w_structure("DSA", dsa, yes, pem, type_specific),
c319b627
RL
105#endif
106#ifndef OPENSSL_NO_EC
c8182743 107/* EC only supports keypair and parameters DER and PEM output. */
0cc0164d
RL
108ENCODER_w_structure("EC", ec, yes, der, type_specific_no_pub),
109ENCODER_w_structure("EC", ec, yes, pem, type_specific_no_pub),
c8182743
RL
110/* EC supports blob output for the public key */
111ENCODER("EC", ec, yes, blob),
f2db0528
RL
112# ifndef OPENSSL_NO_SM2
113ENCODER_w_structure("SM2", sm2, yes, der, type_specific_no_pub),
114ENCODER_w_structure("SM2", sm2, yes, pem, type_specific_no_pub),
c8182743 115ENCODER("SM2", sm2, yes, blob),
f2db0528 116# endif
0cc0164d
RL
117#endif
118
119/*
120 * Entries for the output formats MSBLOB and PVK
121 */
122ENCODER("RSA", rsa, yes, msblob),
123ENCODER("RSA", rsa, yes, pvk),
124#ifndef OPENSSL_NO_DSA
125ENCODER("DSA", dsa, yes, msblob),
126ENCODER("DSA", dsa, yes, pvk),
c319b627
RL
127#endif
128
129/*
6a2b8ff3 130 * Entries for PKCS#8 (PrivateKeyInfo) and SubjectPublicKeyInfo.
c319b627
RL
131 * The "der" ones are added convenience for any user that wants to use
132 * OSSL_ENCODER directly.
133 * The "pem" ones also support PEM_write_bio_PrivateKey() and
134 * PEM_write_bio_PUBKEY().
135 */
6a2b8ff3
RL
136ENCODER_w_structure("RSA", rsa, yes, der, PrivateKeyInfo),
137ENCODER_w_structure("RSA", rsa, yes, pem, PrivateKeyInfo),
0cc0164d
RL
138ENCODER_w_structure("RSA", rsa, yes, der, SubjectPublicKeyInfo),
139ENCODER_w_structure("RSA", rsa, yes, pem, SubjectPublicKeyInfo),
c319b627 140
6a2b8ff3
RL
141ENCODER_w_structure("RSA-PSS", rsapss, yes, der, PrivateKeyInfo),
142ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, PrivateKeyInfo),
0cc0164d
RL
143ENCODER_w_structure("RSA-PSS", rsapss, yes, der, SubjectPublicKeyInfo),
144ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, SubjectPublicKeyInfo),
ece9304c
RL
145
146#ifndef OPENSSL_NO_DH
6a2b8ff3
RL
147ENCODER_w_structure("DH", dh, yes, der, PrivateKeyInfo),
148ENCODER_w_structure("DH", dh, yes, pem, PrivateKeyInfo),
0cc0164d
RL
149ENCODER_w_structure("DH", dh, yes, der, SubjectPublicKeyInfo),
150ENCODER_w_structure("DH", dh, yes, pem, SubjectPublicKeyInfo),
151
6a2b8ff3
RL
152ENCODER_w_structure("DHX", dhx, yes, der, PrivateKeyInfo),
153ENCODER_w_structure("DHX", dhx, yes, pem, PrivateKeyInfo),
0cc0164d
RL
154ENCODER_w_structure("DHX", dhx, yes, der, SubjectPublicKeyInfo),
155ENCODER_w_structure("DHX", dhx, yes, pem, SubjectPublicKeyInfo),
ece9304c
RL
156#endif
157
158#ifndef OPENSSL_NO_DSA
6a2b8ff3
RL
159ENCODER_w_structure("DSA", dsa, yes, der, PrivateKeyInfo),
160ENCODER_w_structure("DSA", dsa, yes, pem, PrivateKeyInfo),
0cc0164d
RL
161ENCODER_w_structure("DSA", dsa, yes, der, SubjectPublicKeyInfo),
162ENCODER_w_structure("DSA", dsa, yes, pem, SubjectPublicKeyInfo),
ece9304c
RL
163#endif
164
165#ifndef OPENSSL_NO_EC
6a2b8ff3
RL
166ENCODER_w_structure("EC", ec, yes, der, PrivateKeyInfo),
167ENCODER_w_structure("EC", ec, yes, pem, PrivateKeyInfo),
0cc0164d
RL
168ENCODER_w_structure("EC", ec, yes, der, SubjectPublicKeyInfo),
169ENCODER_w_structure("EC", ec, yes, pem, SubjectPublicKeyInfo),
170
6a2b8ff3
RL
171ENCODER_w_structure("X25519", x25519, yes, der, PrivateKeyInfo),
172ENCODER_w_structure("X25519", x25519, yes, pem, PrivateKeyInfo),
0cc0164d
RL
173ENCODER_w_structure("X25519", x25519, yes, der, SubjectPublicKeyInfo),
174ENCODER_w_structure("X25519", x25519, yes, pem, SubjectPublicKeyInfo),
175
6a2b8ff3
RL
176ENCODER_w_structure("X448", x448, yes, der, PrivateKeyInfo),
177ENCODER_w_structure("X448", x448, yes, pem, PrivateKeyInfo),
0cc0164d
RL
178ENCODER_w_structure("X448", x448, yes, der, SubjectPublicKeyInfo),
179ENCODER_w_structure("X448", x448, yes, pem, SubjectPublicKeyInfo),
180
6a2b8ff3
RL
181ENCODER_w_structure("ED25519", ed25519, yes, der, PrivateKeyInfo),
182ENCODER_w_structure("ED25519", ed25519, yes, pem, PrivateKeyInfo),
0cc0164d
RL
183ENCODER_w_structure("ED25519", ed25519, yes, der, SubjectPublicKeyInfo),
184ENCODER_w_structure("ED25519", ed25519, yes, pem, SubjectPublicKeyInfo),
185
6a2b8ff3
RL
186ENCODER_w_structure("ED448", ed448, yes, der, PrivateKeyInfo),
187ENCODER_w_structure("ED448", ed448, yes, pem, PrivateKeyInfo),
0cc0164d
RL
188ENCODER_w_structure("ED448", ed448, yes, der, SubjectPublicKeyInfo),
189ENCODER_w_structure("ED448", ed448, yes, pem, SubjectPublicKeyInfo),
f2db0528
RL
190
191# ifndef OPENSSL_NO_SM2
6a2b8ff3
RL
192ENCODER_w_structure("SM2", sm2, yes, der, PrivateKeyInfo),
193ENCODER_w_structure("SM2", sm2, yes, pem, PrivateKeyInfo),
f2db0528
RL
194ENCODER_w_structure("SM2", sm2, yes, der, SubjectPublicKeyInfo),
195ENCODER_w_structure("SM2", sm2, yes, pem, SubjectPublicKeyInfo),
196# endif
c319b627
RL
197#endif
198
199/*
200 * Entries for key type specific output formats. These are exactly the
201 * same as the type specific above, except that they use the key type
202 * name as structure name instead of "type-specific", in the call on
fe75766c 203 * OSSL_ENCODER_CTX_new_for_pkey().
c319b627
RL
204 */
205
206/* The RSA encoders only support private key and public key output */
0cc0164d
RL
207ENCODER_w_structure("RSA", rsa, yes, der, RSA),
208ENCODER_w_structure("RSA", rsa, yes, pem, RSA),
c319b627
RL
209#ifndef OPENSSL_NO_DH
210/* DH and X9.42 DH only support key parameters output. */
0cc0164d
RL
211ENCODER_w_structure("DH", dh, yes, der, DH),
212ENCODER_w_structure("DH", dh, yes, pem, DH),
213ENCODER_w_structure("DHX", dhx, yes, der, DHX),
214ENCODER_w_structure("DHX", dhx, yes, pem, DHX),
c319b627
RL
215#endif
216#ifndef OPENSSL_NO_DSA
0cc0164d
RL
217ENCODER_w_structure("DSA", dsa, yes, der, DSA),
218ENCODER_w_structure("DSA", dsa, yes, pem, DSA),
c319b627
RL
219#endif
220#ifndef OPENSSL_NO_EC
0cc0164d
RL
221ENCODER_w_structure("EC", ec, yes, der, EC),
222ENCODER_w_structure("EC", ec, yes, pem, EC),
c319b627
RL
223#endif
224
225/*
226 * Additional entries with structure names being the standard name.
227 * This is entirely for the convenience of the user that wants to use
228 * OSSL_ENCODER directly with names they may fancy. These do not impact
229 * on libcrypto functionality in any way.
230 */
231/* PKCS#1 is a well known for plain RSA keys, so we add that too */
0cc0164d
RL
232ENCODER_w_structure("RSA", rsa, yes, der, PKCS1),
233ENCODER_w_structure("RSA", rsa, yes, pem, PKCS1),
234ENCODER_w_structure("RSA-PSS", rsapss, yes, der, PKCS1),
235ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, PKCS1),
c319b627
RL
236#ifndef OPENSSL_NO_DH
237/* PKCS#3 defines the format for DH parameters */
0cc0164d
RL
238ENCODER_w_structure("DH", dh, yes, der, PKCS3),
239ENCODER_w_structure("DH", dh, yes, pem, PKCS3),
c319b627 240/* X9.42 defines the format for DHX parameters */
0cc0164d
RL
241ENCODER_w_structure("DHX", dhx, yes, der, X9_42),
242ENCODER_w_structure("DHX", dhx, yes, pem, X9_42),
c319b627
RL
243#endif
244#ifndef OPENSSL_NO_EC
245/* RFC 5915 defines the format for EC keys and parameters */
0cc0164d
RL
246ENCODER_w_structure("EC", ec, yes, der, X9_62),
247ENCODER_w_structure("EC", ec, yes, pem, X9_62),
ece9304c 248#endif