Refactor the provider side DER constants and writers
[openssl.git] / providers / common / der / der_rsa_sig.c
1 /*
2  * Copyright 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 <openssl/obj_mac.h>
11 #include "internal/packet.h"
12 #include "prov/der_rsa.h"
13 #include "prov/der_digests.h"
14
15 /* Aliases so we can have a uniform MD_with_RSA_CASE */
16 #define der_oid_sha3_224WithRSAEncryption \
17     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_224
18 #define der_oid_sha3_256WithRSAEncryption \
19     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_256
20 #define der_oid_sha3_384WithRSAEncryption \
21     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_384
22 #define der_oid_sha3_512WithRSAEncryption \
23     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_512
24
25 #define MD_with_RSA_CASE(name, var)                                     \
26     case NID_##name:                                                    \
27         var = der_oid_##name##WithRSAEncryption;                        \
28         var##_sz = sizeof(der_oid_##name##WithRSAEncryption);           \
29         break;
30
31 int DER_w_algorithmIdentifier_MDWithRSAEncryption(WPACKET *pkt, int tag,
32                                                   RSA *rsa, int mdnid)
33 {
34     const unsigned char *precompiled = NULL;
35     size_t precompiled_sz = 0;
36
37     switch (mdnid) {
38 #ifndef FIPS_MODULE
39         MD_with_RSA_CASE(md2, precompiled);
40         MD_with_RSA_CASE(md5, precompiled);
41         MD_with_RSA_CASE(md4, precompiled);
42         MD_with_RSA_CASE(ripemd160, precompiled);
43 /* TODO(3.0) Decide what to do about mdc2 and md5_sha1 */
44 #endif
45         MD_with_RSA_CASE(sha1, precompiled);
46         MD_with_RSA_CASE(sha224, precompiled);
47         MD_with_RSA_CASE(sha256, precompiled);
48         MD_with_RSA_CASE(sha384, precompiled);
49         MD_with_RSA_CASE(sha512, precompiled);
50         MD_with_RSA_CASE(sha512_224, precompiled);
51         MD_with_RSA_CASE(sha512_256, precompiled);
52         MD_with_RSA_CASE(sha3_224, precompiled);
53         MD_with_RSA_CASE(sha3_256, precompiled);
54         MD_with_RSA_CASE(sha3_384, precompiled);
55         MD_with_RSA_CASE(sha3_512, precompiled);
56     default:
57         return 0;
58     }
59
60     return DER_w_begin_sequence(pkt, tag)
61         /* No parameters (yet?) */
62         && DER_w_precompiled(pkt, -1, precompiled, precompiled_sz)
63         && DER_w_end_sequence(pkt, tag);
64 }