2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/obj_mac.h>
11 #include "internal/cryptlib.h"
12 #include "prov/der_rsa.h"
13 #include "prov/der_digests.h"
15 /* More complex pre-compiled sequences. TODO(3.0) refactor? */
17 * From https://tools.ietf.org/html/rfc8017#appendix-A.2.1
19 * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
20 * { OID id-sha1 PARAMETERS NULL }|
21 * { OID id-sha224 PARAMETERS NULL }|
22 * { OID id-sha256 PARAMETERS NULL }|
23 * { OID id-sha384 PARAMETERS NULL }|
24 * { OID id-sha512 PARAMETERS NULL }|
25 * { OID id-sha512-224 PARAMETERS NULL }|
26 * { OID id-sha512-256 PARAMETERS NULL },
27 * ... -- Allows for future expansion --
30 #define DER_V_NULL DER_P_NULL, 0
34 * The names for the hash function AlgorithmIdentifiers are borrowed and
35 * expanded from https://tools.ietf.org/html/rfc4055#section-2.1
37 * sha1Identifier AlgorithmIdentifier ::= { id-sha1, NULL }
38 * sha224Identifier AlgorithmIdentifier ::= { id-sha224, NULL }
39 * sha256Identifier AlgorithmIdentifier ::= { id-sha256, NULL }
40 * sha384Identifier AlgorithmIdentifier ::= { id-sha384, NULL }
41 * sha512Identifier AlgorithmIdentifier ::= { id-sha512, NULL }
44 * NOTE: Some of the arrays aren't used other than inside sizeof(), which
45 * clang complains about (-Wno-unneeded-internal-declaration). To get
46 * around that, we make them non-static, and declare them an extra time to
47 * avoid compilers complaining about definitions without declarations.
49 #if 0 /* Currently unused */
50 #define DER_AID_V_sha1Identifier \
51 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
52 DER_OID_SZ_id_sha1 + DER_SZ_NULL, \
55 extern const unsigned char der_aid_sha1Identifier
[];
56 const unsigned char der_aid_sha1Identifier
[] = {
57 DER_AID_V_sha1Identifier
59 #define DER_AID_SZ_sha1Identifier sizeof(der_aid_sha1Identifier)
62 #define DER_AID_V_sha224Identifier \
63 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
64 DER_OID_SZ_id_sha224 + DER_SZ_NULL, \
65 DER_OID_V_id_sha224, \
67 extern const unsigned char der_aid_sha224Identifier
[];
68 const unsigned char der_aid_sha224Identifier
[] = {
69 DER_AID_V_sha224Identifier
71 #define DER_AID_SZ_sha224Identifier sizeof(der_aid_sha224Identifier)
73 #define DER_AID_V_sha256Identifier \
74 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
75 DER_OID_SZ_id_sha256 + DER_SZ_NULL, \
76 DER_OID_V_id_sha256, \
78 extern const unsigned char der_aid_sha256Identifier
[];
79 const unsigned char der_aid_sha256Identifier
[] = {
80 DER_AID_V_sha256Identifier
82 #define DER_AID_SZ_sha256Identifier sizeof(der_aid_sha256Identifier)
84 #define DER_AID_V_sha384Identifier \
85 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
86 DER_OID_SZ_id_sha384 + DER_SZ_NULL, \
87 DER_OID_V_id_sha384, \
89 extern const unsigned char der_aid_sha384Identifier
[];
90 const unsigned char der_aid_sha384Identifier
[] = {
91 DER_AID_V_sha384Identifier
93 #define DER_AID_SZ_sha384Identifier sizeof(der_aid_sha384Identifier)
95 #define DER_AID_V_sha512Identifier \
96 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
97 DER_OID_SZ_id_sha512 + DER_SZ_NULL, \
98 DER_OID_V_id_sha512, \
100 extern const unsigned char der_aid_sha512Identifier
[];
101 const unsigned char der_aid_sha512Identifier
[] = {
102 DER_AID_V_sha512Identifier
104 #define DER_AID_SZ_sha512Identifier sizeof(der_aid_sha512Identifier)
106 #define DER_AID_V_sha512_224Identifier \
107 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
108 DER_OID_SZ_id_sha512_224 + DER_SZ_NULL, \
109 DER_OID_V_id_sha512_224, \
111 extern const unsigned char der_aid_sha512_224Identifier
[];
112 const unsigned char der_aid_sha512_224Identifier
[] = {
113 DER_AID_V_sha512_224Identifier
115 #define DER_AID_SZ_sha512_224Identifier sizeof(der_aid_sha512_224Identifier)
117 #define DER_AID_V_sha512_256Identifier \
118 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
119 DER_OID_SZ_id_sha512_256 + DER_SZ_NULL, \
120 DER_OID_V_id_sha512_256, \
122 extern const unsigned char der_aid_sha512_256Identifier
[];
123 const unsigned char der_aid_sha512_256Identifier
[] = {
124 DER_AID_V_sha512_256Identifier
126 #define DER_AID_SZ_sha512_256Identifier sizeof(der_aid_sha512_256Identifier)
129 * From https://tools.ietf.org/html/rfc8017#appendix-A.2.1
131 * HashAlgorithm ::= AlgorithmIdentifier {
132 * {OAEP-PSSDigestAlgorithms}
137 * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {
138 * { OID id-mgf1 PARAMETERS HashAlgorithm },
139 * ... -- Allows for future expansion --
144 * The names for the MGF1 AlgorithmIdentifiers are borrowed and expanded
145 * from https://tools.ietf.org/html/rfc4055#section-2.1
147 * mgf1SHA1Identifier AlgorithmIdentifier ::=
148 * { id-mgf1, sha1Identifier }
149 * mgf1SHA224Identifier AlgorithmIdentifier ::=
150 * { id-mgf1, sha224Identifier }
151 * mgf1SHA256Identifier AlgorithmIdentifier ::=
152 * { id-mgf1, sha256Identifier }
153 * mgf1SHA384Identifier AlgorithmIdentifier ::=
154 * { id-mgf1, sha384Identifier }
155 * mgf1SHA512Identifier AlgorithmIdentifier ::=
156 * { id-mgf1, sha512Identifier }
158 #if 0 /* Currently unused */
159 #define DER_AID_V_mgf1SHA1Identifier \
160 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
161 DER_OID_SZ_id_mgf1 + DER_AID_SZ_sha1Identifier, \
163 DER_AID_V_sha1Identifier
164 static const unsigned char der_aid_mgf1SHA1Identifier
[] = {
165 DER_AID_V_mgf1SHA1Identifier
167 #define DER_AID_SZ_mgf1SHA1Identifier sizeof(der_aid_mgf1SHA1Identifier)
170 #define DER_AID_V_mgf1SHA224Identifier \
171 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
172 DER_OID_SZ_id_mgf1 + DER_AID_SZ_sha224Identifier, \
174 DER_AID_V_sha224Identifier
175 static const unsigned char der_aid_mgf1SHA224Identifier
[] = {
176 DER_AID_V_mgf1SHA224Identifier
178 #define DER_AID_SZ_mgf1SHA224Identifier sizeof(der_aid_mgf1SHA224Identifier)
180 #define DER_AID_V_mgf1SHA256Identifier \
181 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
182 DER_OID_SZ_id_mgf1 + DER_AID_SZ_sha256Identifier, \
184 DER_AID_V_sha256Identifier
185 static const unsigned char der_aid_mgf1SHA256Identifier
[] = {
186 DER_AID_V_mgf1SHA256Identifier
188 #define DER_AID_SZ_mgf1SHA256Identifier sizeof(der_aid_mgf1SHA256Identifier)
190 #define DER_AID_V_mgf1SHA384Identifier \
191 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
192 DER_OID_SZ_id_mgf1 + DER_AID_SZ_sha384Identifier, \
194 DER_AID_V_sha384Identifier
195 static const unsigned char der_aid_mgf1SHA384Identifier
[] = {
196 DER_AID_V_mgf1SHA384Identifier
198 #define DER_AID_SZ_mgf1SHA384Identifier sizeof(der_aid_mgf1SHA384Identifier)
200 #define DER_AID_V_mgf1SHA512Identifier \
201 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
202 DER_OID_SZ_id_mgf1 + DER_AID_SZ_sha512Identifier, \
204 DER_AID_V_sha512Identifier
205 static const unsigned char der_aid_mgf1SHA512Identifier
[] = {
206 DER_AID_V_mgf1SHA512Identifier
208 #define DER_AID_SZ_mgf1SHA512Identifier sizeof(der_aid_mgf1SHA512Identifier)
210 #define DER_AID_V_mgf1SHA512_224Identifier \
211 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
212 DER_OID_SZ_id_mgf1 + DER_AID_SZ_sha512_224Identifier, \
214 DER_AID_V_sha512_224Identifier
215 static const unsigned char der_aid_mgf1SHA512_224Identifier
[] = {
216 DER_AID_V_mgf1SHA512_224Identifier
218 #define DER_AID_SZ_mgf1SHA512_224Identifier sizeof(der_aid_mgf1SHA512_224Identifier)
220 #define DER_AID_V_mgf1SHA512_256Identifier \
221 DER_P_SEQUENCE|DER_F_CONSTRUCTED, \
222 DER_OID_SZ_id_mgf1 + DER_AID_SZ_sha512_256Identifier, \
224 DER_AID_V_sha512_256Identifier
225 static const unsigned char der_aid_mgf1SHA512_256Identifier
[] = {
226 DER_AID_V_mgf1SHA512_256Identifier
228 #define DER_AID_SZ_mgf1SHA512_256Identifier sizeof(der_aid_mgf1SHA512_256Identifier)
231 #define MGF1_SHA_CASE(bits, var) \
232 case NID_sha##bits: \
233 var = der_aid_mgf1SHA##bits##Identifier; \
234 var##_sz = sizeof(der_aid_mgf1SHA##bits##Identifier); \
238 * The name is borrowed from https://tools.ietf.org/html/rfc8017#appendix-A.2.1
240 * MaskGenAlgorithm ::= AlgorithmIdentifier { {PKCS1MGFAlgorithms} }
242 static int DER_w_MaskGenAlgorithm(WPACKET
*pkt
, int tag
,
243 const RSA_PSS_PARAMS_30
*pss
)
245 if (pss
!= NULL
&& rsa_pss_params_30_maskgenalg(pss
) == NID_mgf1
) {
246 int maskgenhashalg_nid
= rsa_pss_params_30_maskgenhashalg(pss
);
247 const unsigned char *maskgenalg
= NULL
;
248 size_t maskgenalg_sz
= 0;
250 switch (maskgenhashalg_nid
) {
253 MGF1_SHA_CASE(224, maskgenalg
);
254 MGF1_SHA_CASE(256, maskgenalg
);
255 MGF1_SHA_CASE(384, maskgenalg
);
256 MGF1_SHA_CASE(512, maskgenalg
);
257 MGF1_SHA_CASE(512_224
, maskgenalg
);
258 MGF1_SHA_CASE(512_256
, maskgenalg
);
263 /* If there is none (or it was the default), we write nothing */
264 if (maskgenalg
== NULL
)
267 return DER_w_precompiled(pkt
, tag
, maskgenalg
, maskgenalg_sz
);
272 #define OAEP_PSS_MD_CASE(name, var) \
274 var = der_oid_id_##name; \
275 var##_sz = sizeof(der_oid_id_##name); \
278 int DER_w_RSASSA_PSS_params(WPACKET
*pkt
, int tag
, const RSA_PSS_PARAMS_30
*pss
)
280 int hashalg_nid
, default_hashalg_nid
;
281 int saltlen
, default_saltlen
;
282 int trailerfield
, default_trailerfield
;
283 const unsigned char *hashalg
= NULL
;
284 size_t hashalg_sz
= 0;
287 * For an unrestricted key, this function should not have been called;
288 * the caller must be in control, because unrestricted keys are permitted
289 * in some situations (when encoding the public key in a SubjectKeyInfo,
290 * for example) while not in others, and this function doesn't know the
291 * intent. Therefore, we assert that here, the PSS parameters must show
292 * that the key is restricted.
294 if (!ossl_assert(pss
!= NULL
&& !rsa_pss_params_30_is_unrestricted(pss
)))
297 hashalg_nid
= rsa_pss_params_30_hashalg(pss
);
298 saltlen
= rsa_pss_params_30_saltlen(pss
);
299 trailerfield
= rsa_pss_params_30_trailerfield(pss
);
301 /* Getting default values */
302 default_hashalg_nid
= rsa_pss_params_30_hashalg(NULL
);
303 default_saltlen
= rsa_pss_params_30_saltlen(NULL
);
304 default_trailerfield
= rsa_pss_params_30_trailerfield(NULL
);
307 * From https://tools.ietf.org/html/rfc8017#appendix-A.2.1:
309 * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
310 * { OID id-sha1 PARAMETERS NULL }|
311 * { OID id-sha224 PARAMETERS NULL }|
312 * { OID id-sha256 PARAMETERS NULL }|
313 * { OID id-sha384 PARAMETERS NULL }|
314 * { OID id-sha512 PARAMETERS NULL }|
315 * { OID id-sha512-224 PARAMETERS NULL }|
316 * { OID id-sha512-256 PARAMETERS NULL },
317 * ... -- Allows for future expansion --
320 switch (hashalg_nid
) {
321 OAEP_PSS_MD_CASE(sha1
, hashalg
);
322 OAEP_PSS_MD_CASE(sha224
, hashalg
);
323 OAEP_PSS_MD_CASE(sha256
, hashalg
);
324 OAEP_PSS_MD_CASE(sha384
, hashalg
);
325 OAEP_PSS_MD_CASE(sha512
, hashalg
);
326 OAEP_PSS_MD_CASE(sha512_224
, hashalg
);
327 OAEP_PSS_MD_CASE(sha512_256
, hashalg
);
332 return DER_w_begin_sequence(pkt
, tag
)
333 && (trailerfield
== default_trailerfield
334 || DER_w_ulong(pkt
, 3, trailerfield
))
335 && (saltlen
== default_saltlen
|| DER_w_ulong(pkt
, 2, saltlen
))
336 && DER_w_MaskGenAlgorithm(pkt
, 1, pss
)
337 && (hashalg_nid
== default_hashalg_nid
338 || DER_w_precompiled(pkt
, 0, hashalg
, hashalg_sz
))
339 && DER_w_end_sequence(pkt
, tag
);
342 /* Aliases so we can have a uniform RSA_CASE */
343 #define der_oid_rsassaPss der_oid_id_RSASSA_PSS
345 #define RSA_CASE(name, var) \
346 var##_nid = NID_##name; \
347 var##_oid = der_oid_##name; \
348 var##_oid_sz = sizeof(der_oid_##name); \
351 int DER_w_algorithmIdentifier_RSA(WPACKET
*pkt
, int tag
, RSA
*rsa
)
353 int rsa_nid
= NID_undef
;
354 const unsigned char *rsa_oid
= NULL
;
355 size_t rsa_oid_sz
= 0;
356 RSA_PSS_PARAMS_30
*pss_params
= rsa_get0_pss_params_30(rsa
);
358 switch (RSA_test_flags(rsa
, RSA_FLAG_TYPE_MASK
)) {
359 case RSA_FLAG_TYPE_RSA
:
360 RSA_CASE(rsaEncryption
, rsa
);
361 case RSA_FLAG_TYPE_RSASSAPSS
:
362 RSA_CASE(rsassaPss
, rsa
);
368 return DER_w_begin_sequence(pkt
, tag
)
369 && (rsa_nid
!= NID_rsassaPss
370 || rsa_pss_params_30_is_unrestricted(pss_params
)
371 || DER_w_RSASSA_PSS_params(pkt
, -1, pss_params
))
372 && DER_w_precompiled(pkt
, -1, rsa_oid
, rsa_oid_sz
)
373 && DER_w_end_sequence(pkt
, tag
);