xref: /freebsd/crypto/openssl/crypto/rsa/rsa_sign.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
397b2ed56SKris Kennaway  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
897b2ed56SKris Kennaway  */
997b2ed56SKris Kennaway 
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery  * RSA 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 
1697b2ed56SKris Kennaway #include <stdio.h>
17e71b7053SJung-uk Kim #include "internal/cryptlib.h"
1897b2ed56SKris Kennaway #include <openssl/bn.h>
1997b2ed56SKris Kennaway #include <openssl/rsa.h>
2097b2ed56SKris Kennaway #include <openssl/objects.h>
21*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
22*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD2
23*b077aed3SPierre Pronchery #  include <openssl/md2.h> /* uses MD2_DIGEST_LENGTH */
24*b077aed3SPierre Pronchery # endif
25*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD4
26*b077aed3SPierre Pronchery #  include <openssl/md4.h> /* uses MD4_DIGEST_LENGTH */
27*b077aed3SPierre Pronchery # endif
28*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD5
29*b077aed3SPierre Pronchery #  include <openssl/md5.h> /* uses MD5_DIGEST_LENGTH */
30*b077aed3SPierre Pronchery # endif
31*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MDC2
32*b077aed3SPierre Pronchery #  include <openssl/mdc2.h> /* uses MDC2_DIGEST_LENGTH */
33*b077aed3SPierre Pronchery # endif
34*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_RMD160
35*b077aed3SPierre Pronchery #  include <openssl/ripemd.h> /* uses RIPEMD160_DIGEST_LENGTH */
36*b077aed3SPierre Pronchery # endif
37*b077aed3SPierre Pronchery #endif
38*b077aed3SPierre Pronchery #include <openssl/sha.h> /* uses SHA???_DIGEST_LENGTH */
39*b077aed3SPierre Pronchery #include "crypto/rsa.h"
4017f01e99SJung-uk Kim #include "rsa_local.h"
4197b2ed56SKris Kennaway 
42*b077aed3SPierre Pronchery /*
43*b077aed3SPierre Pronchery  * The general purpose ASN1 code is not available inside the FIPS provider.
44*b077aed3SPierre Pronchery  * To remove the dependency RSASSA-PKCS1-v1_5 DigestInfo encodings can be
45*b077aed3SPierre Pronchery  * treated as a special case by pregenerating the required ASN1 encoding.
46*b077aed3SPierre Pronchery  * This encoding will also be shared by the default provider.
47*b077aed3SPierre Pronchery  *
48*b077aed3SPierre Pronchery  * The EMSA-PKCS1-v1_5 encoding method includes an ASN.1 value of type
49*b077aed3SPierre Pronchery  * DigestInfo, where the type DigestInfo has the syntax
50*b077aed3SPierre Pronchery  *
51*b077aed3SPierre Pronchery  *     DigestInfo ::= SEQUENCE {
52*b077aed3SPierre Pronchery  *         digestAlgorithm DigestAlgorithm,
53*b077aed3SPierre Pronchery  *         digest OCTET STRING
54*b077aed3SPierre Pronchery  *     }
55*b077aed3SPierre Pronchery  *
56*b077aed3SPierre Pronchery  *     DigestAlgorithm ::= AlgorithmIdentifier {
57*b077aed3SPierre Pronchery  *         {PKCS1-v1-5DigestAlgorithms}
58*b077aed3SPierre Pronchery  *     }
59*b077aed3SPierre Pronchery  *
60*b077aed3SPierre Pronchery  * The AlgorithmIdentifier is a sequence containing the digest OID and
61*b077aed3SPierre Pronchery  * parameters (a value of type NULL).
62*b077aed3SPierre Pronchery  *
63*b077aed3SPierre Pronchery  * The ENCODE_DIGESTINFO_SHA() and ENCODE_DIGESTINFO_MD() macros define an
64*b077aed3SPierre Pronchery  * initialized array containing the DER encoded DigestInfo for the specified
65*b077aed3SPierre Pronchery  * SHA or MD digest. The content of the OCTET STRING is not included.
66*b077aed3SPierre Pronchery  * |name| is the digest name.
67*b077aed3SPierre Pronchery  * |n| is last byte in the encoded OID for the digest.
68*b077aed3SPierre Pronchery  * |sz| is the digest length in bytes. It must not be greater than 110.
69*b077aed3SPierre Pronchery  */
70*b077aed3SPierre Pronchery 
71*b077aed3SPierre Pronchery #define ASN1_SEQUENCE 0x30
72*b077aed3SPierre Pronchery #define ASN1_OCTET_STRING 0x04
73*b077aed3SPierre Pronchery #define ASN1_NULL 0x05
74*b077aed3SPierre Pronchery #define ASN1_OID 0x06
75*b077aed3SPierre Pronchery 
76*b077aed3SPierre Pronchery /* SHA OIDs are of the form: (2 16 840 1 101 3 4 2 |n|) */
77*b077aed3SPierre Pronchery #define ENCODE_DIGESTINFO_SHA(name, n, sz)                                     \
78*b077aed3SPierre Pronchery static const unsigned char digestinfo_##name##_der[] = {                       \
79*b077aed3SPierre Pronchery     ASN1_SEQUENCE, 0x11 + sz,                                                  \
80*b077aed3SPierre Pronchery       ASN1_SEQUENCE, 0x0d,                                                     \
81*b077aed3SPierre Pronchery         ASN1_OID, 0x09, 2 * 40 + 16, 0x86, 0x48, 1, 101, 3, 4, 2, n,           \
82*b077aed3SPierre Pronchery         ASN1_NULL, 0x00,                                                       \
83*b077aed3SPierre Pronchery       ASN1_OCTET_STRING, sz                                                    \
84*b077aed3SPierre Pronchery };
85*b077aed3SPierre Pronchery 
86*b077aed3SPierre Pronchery /* MD2, MD4 and MD5 OIDs are of the form: (1 2 840 113549 2 |n|) */
87*b077aed3SPierre Pronchery #define ENCODE_DIGESTINFO_MD(name, n, sz)                                      \
88*b077aed3SPierre Pronchery static const unsigned char digestinfo_##name##_der[] = {                       \
89*b077aed3SPierre Pronchery     ASN1_SEQUENCE, 0x10 + sz,                                                  \
90*b077aed3SPierre Pronchery       ASN1_SEQUENCE, 0x0c,                                                     \
91*b077aed3SPierre Pronchery         ASN1_OID, 0x08, 1 * 40 + 2, 0x86, 0x48, 0x86, 0xf7, 0x0d, 2, n,        \
92*b077aed3SPierre Pronchery         ASN1_NULL, 0x00,                                                       \
93*b077aed3SPierre Pronchery       ASN1_OCTET_STRING, sz                                                    \
94*b077aed3SPierre Pronchery };
95*b077aed3SPierre Pronchery 
96*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
97*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD2
98*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_MD(md2, 0x02, MD2_DIGEST_LENGTH)
99*b077aed3SPierre Pronchery # endif
100*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD4
101*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_MD(md4, 0x03, MD4_DIGEST_LENGTH)
102*b077aed3SPierre Pronchery # endif
103*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD5
104*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_MD(md5, 0x05, MD5_DIGEST_LENGTH)
105*b077aed3SPierre Pronchery # endif
106*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MDC2
107*b077aed3SPierre Pronchery /* MDC-2 (2 5 8 3 101) */
108*b077aed3SPierre Pronchery static const unsigned char digestinfo_mdc2_der[] = {
109*b077aed3SPierre Pronchery     ASN1_SEQUENCE, 0x0c + MDC2_DIGEST_LENGTH,
110*b077aed3SPierre Pronchery       ASN1_SEQUENCE, 0x08,
111*b077aed3SPierre Pronchery         ASN1_OID, 0x04, 2 * 40 + 5, 8, 3, 101,
112*b077aed3SPierre Pronchery         ASN1_NULL, 0x00,
113*b077aed3SPierre Pronchery       ASN1_OCTET_STRING, MDC2_DIGEST_LENGTH
114*b077aed3SPierre Pronchery };
115*b077aed3SPierre Pronchery # endif
116*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_RMD160
117*b077aed3SPierre Pronchery /* RIPEMD160 (1 3 36 3 2 1) */
118*b077aed3SPierre Pronchery static const unsigned char digestinfo_ripemd160_der[] = {
119*b077aed3SPierre Pronchery     ASN1_SEQUENCE, 0x0d + RIPEMD160_DIGEST_LENGTH,
120*b077aed3SPierre Pronchery       ASN1_SEQUENCE, 0x09,
121*b077aed3SPierre Pronchery         ASN1_OID, 0x05, 1 * 40 + 3, 36, 3, 2, 1,
122*b077aed3SPierre Pronchery         ASN1_NULL, 0x00,
123*b077aed3SPierre Pronchery       ASN1_OCTET_STRING, RIPEMD160_DIGEST_LENGTH
124*b077aed3SPierre Pronchery };
125*b077aed3SPierre Pronchery # endif
126*b077aed3SPierre Pronchery #endif /* FIPS_MODULE */
127*b077aed3SPierre Pronchery 
128*b077aed3SPierre Pronchery /* SHA-1 (1 3 14 3 2 26) */
129*b077aed3SPierre Pronchery static const unsigned char digestinfo_sha1_der[] = {
130*b077aed3SPierre Pronchery     ASN1_SEQUENCE, 0x0d + SHA_DIGEST_LENGTH,
131*b077aed3SPierre Pronchery       ASN1_SEQUENCE, 0x09,
132*b077aed3SPierre Pronchery         ASN1_OID, 0x05, 1 * 40 + 3, 14, 3, 2, 26,
133*b077aed3SPierre Pronchery         ASN1_NULL, 0x00,
134*b077aed3SPierre Pronchery       ASN1_OCTET_STRING, SHA_DIGEST_LENGTH
135*b077aed3SPierre Pronchery };
136*b077aed3SPierre Pronchery 
137*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha256, 0x01, SHA256_DIGEST_LENGTH)
138*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha384, 0x02, SHA384_DIGEST_LENGTH)
139*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha512, 0x03, SHA512_DIGEST_LENGTH)
140*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha224, 0x04, SHA224_DIGEST_LENGTH)
141*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha512_224, 0x05, SHA224_DIGEST_LENGTH)
142*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha512_256, 0x06, SHA256_DIGEST_LENGTH)
143*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha3_224, 0x07, SHA224_DIGEST_LENGTH)
144*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha3_256, 0x08, SHA256_DIGEST_LENGTH)
145*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha3_384, 0x09, SHA384_DIGEST_LENGTH)
146*b077aed3SPierre Pronchery ENCODE_DIGESTINFO_SHA(sha3_512, 0x0a, SHA512_DIGEST_LENGTH)
147*b077aed3SPierre Pronchery 
148*b077aed3SPierre Pronchery #define MD_CASE(name)                                                          \
149*b077aed3SPierre Pronchery     case NID_##name:                                                           \
150*b077aed3SPierre Pronchery         *len = sizeof(digestinfo_##name##_der);                                \
151*b077aed3SPierre Pronchery         return digestinfo_##name##_der;
152*b077aed3SPierre Pronchery 
ossl_rsa_digestinfo_encoding(int md_nid,size_t * len)153*b077aed3SPierre Pronchery const unsigned char *ossl_rsa_digestinfo_encoding(int md_nid, size_t *len)
154*b077aed3SPierre Pronchery {
155*b077aed3SPierre Pronchery     switch (md_nid) {
156*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
157*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MDC2
158*b077aed3SPierre Pronchery     MD_CASE(mdc2)
159*b077aed3SPierre Pronchery # endif
160*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD2
161*b077aed3SPierre Pronchery     MD_CASE(md2)
162*b077aed3SPierre Pronchery # endif
163*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD4
164*b077aed3SPierre Pronchery     MD_CASE(md4)
165*b077aed3SPierre Pronchery # endif
166*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD5
167*b077aed3SPierre Pronchery     MD_CASE(md5)
168*b077aed3SPierre Pronchery # endif
169*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_RMD160
170*b077aed3SPierre Pronchery     MD_CASE(ripemd160)
171*b077aed3SPierre Pronchery # endif
172*b077aed3SPierre Pronchery #endif /* FIPS_MODULE */
173*b077aed3SPierre Pronchery     MD_CASE(sha1)
174*b077aed3SPierre Pronchery     MD_CASE(sha224)
175*b077aed3SPierre Pronchery     MD_CASE(sha256)
176*b077aed3SPierre Pronchery     MD_CASE(sha384)
177*b077aed3SPierre Pronchery     MD_CASE(sha512)
178*b077aed3SPierre Pronchery     MD_CASE(sha512_224)
179*b077aed3SPierre Pronchery     MD_CASE(sha512_256)
180*b077aed3SPierre Pronchery     MD_CASE(sha3_224)
181*b077aed3SPierre Pronchery     MD_CASE(sha3_256)
182*b077aed3SPierre Pronchery     MD_CASE(sha3_384)
183*b077aed3SPierre Pronchery     MD_CASE(sha3_512)
184*b077aed3SPierre Pronchery     default:
185*b077aed3SPierre Pronchery         return NULL;
186*b077aed3SPierre Pronchery     }
187*b077aed3SPierre Pronchery }
188*b077aed3SPierre Pronchery 
189*b077aed3SPierre Pronchery #define MD_NID_CASE(name, sz)                                                  \
190*b077aed3SPierre Pronchery     case NID_##name:                                                           \
191*b077aed3SPierre Pronchery         return sz;
192*b077aed3SPierre Pronchery 
digest_sz_from_nid(int nid)193*b077aed3SPierre Pronchery static int digest_sz_from_nid(int nid)
194*b077aed3SPierre Pronchery {
195*b077aed3SPierre Pronchery     switch (nid) {
196*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
197*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MDC2
198*b077aed3SPierre Pronchery     MD_NID_CASE(mdc2, MDC2_DIGEST_LENGTH)
199*b077aed3SPierre Pronchery # endif
200*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD2
201*b077aed3SPierre Pronchery     MD_NID_CASE(md2, MD2_DIGEST_LENGTH)
202*b077aed3SPierre Pronchery # endif
203*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD4
204*b077aed3SPierre Pronchery     MD_NID_CASE(md4, MD4_DIGEST_LENGTH)
205*b077aed3SPierre Pronchery # endif
206*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_MD5
207*b077aed3SPierre Pronchery     MD_NID_CASE(md5, MD5_DIGEST_LENGTH)
208*b077aed3SPierre Pronchery # endif
209*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_RMD160
210*b077aed3SPierre Pronchery     MD_NID_CASE(ripemd160, RIPEMD160_DIGEST_LENGTH)
211*b077aed3SPierre Pronchery # endif
212*b077aed3SPierre Pronchery #endif /* FIPS_MODULE */
213*b077aed3SPierre Pronchery     MD_NID_CASE(sha1, SHA_DIGEST_LENGTH)
214*b077aed3SPierre Pronchery     MD_NID_CASE(sha224, SHA224_DIGEST_LENGTH)
215*b077aed3SPierre Pronchery     MD_NID_CASE(sha256, SHA256_DIGEST_LENGTH)
216*b077aed3SPierre Pronchery     MD_NID_CASE(sha384, SHA384_DIGEST_LENGTH)
217*b077aed3SPierre Pronchery     MD_NID_CASE(sha512, SHA512_DIGEST_LENGTH)
218*b077aed3SPierre Pronchery     MD_NID_CASE(sha512_224, SHA224_DIGEST_LENGTH)
219*b077aed3SPierre Pronchery     MD_NID_CASE(sha512_256, SHA256_DIGEST_LENGTH)
220*b077aed3SPierre Pronchery     MD_NID_CASE(sha3_224, SHA224_DIGEST_LENGTH)
221*b077aed3SPierre Pronchery     MD_NID_CASE(sha3_256, SHA256_DIGEST_LENGTH)
222*b077aed3SPierre Pronchery     MD_NID_CASE(sha3_384, SHA384_DIGEST_LENGTH)
223*b077aed3SPierre Pronchery     MD_NID_CASE(sha3_512, SHA512_DIGEST_LENGTH)
224*b077aed3SPierre Pronchery     default:
225*b077aed3SPierre Pronchery         return 0;
226*b077aed3SPierre Pronchery     }
227*b077aed3SPierre Pronchery }
228*b077aed3SPierre Pronchery 
229*b077aed3SPierre Pronchery 
230f579bf8eSKris Kennaway /* Size of an SSL signature: MD5+SHA1 */
231f579bf8eSKris Kennaway #define SSL_SIG_LENGTH  36
232f579bf8eSKris Kennaway 
233e71b7053SJung-uk Kim /*
234*b077aed3SPierre Pronchery  * Encodes a DigestInfo prefix of hash |type| and digest |m|, as
235e71b7053SJung-uk Kim  * described in EMSA-PKCS1-v1_5-ENCODE, RFC 3447 section 9.2 step 2. This
236e71b7053SJung-uk Kim  * encodes the DigestInfo (T and tLen) but does not add the padding.
237e71b7053SJung-uk Kim  *
238e71b7053SJung-uk Kim  * On success, it returns one and sets |*out| to a newly allocated buffer
239e71b7053SJung-uk Kim  * containing the result and |*out_len| to its length. The caller must free
240*b077aed3SPierre Pronchery  * |*out| with OPENSSL_free(). Otherwise, it returns zero.
241e71b7053SJung-uk Kim  */
encode_pkcs1(unsigned char ** out,size_t * out_len,int type,const unsigned char * m,size_t m_len)242*b077aed3SPierre Pronchery static int encode_pkcs1(unsigned char **out, size_t *out_len, int type,
243*b077aed3SPierre Pronchery                         const unsigned char *m, size_t m_len)
24497b2ed56SKris Kennaway {
245*b077aed3SPierre Pronchery     size_t di_prefix_len, dig_info_len;
246*b077aed3SPierre Pronchery     const unsigned char *di_prefix;
247*b077aed3SPierre Pronchery     unsigned char *dig_info;
248e71b7053SJung-uk Kim 
249*b077aed3SPierre Pronchery     if (type == NID_undef) {
250*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
251e71b7053SJung-uk Kim         return 0;
25297b2ed56SKris Kennaway     }
253*b077aed3SPierre Pronchery     di_prefix = ossl_rsa_digestinfo_encoding(type, &di_prefix_len);
254*b077aed3SPierre Pronchery     if (di_prefix == NULL) {
255*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_RSA,
2566f9291ceSJung-uk Kim                   RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
257e71b7053SJung-uk Kim         return 0;
25897b2ed56SKris Kennaway     }
259*b077aed3SPierre Pronchery     dig_info_len = di_prefix_len + m_len;
260*b077aed3SPierre Pronchery     dig_info = OPENSSL_malloc(dig_info_len);
261*b077aed3SPierre Pronchery     if (dig_info == NULL) {
262*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
263fa5fddf1SJung-uk Kim         return 0;
264*b077aed3SPierre Pronchery     }
265*b077aed3SPierre Pronchery     memcpy(dig_info, di_prefix, di_prefix_len);
266*b077aed3SPierre Pronchery     memcpy(dig_info + di_prefix_len, m, m_len);
267fa5fddf1SJung-uk Kim 
268*b077aed3SPierre Pronchery     *out = dig_info;
269*b077aed3SPierre Pronchery     *out_len = dig_info_len;
2701f13597dSJung-uk Kim     return 1;
27150ef0093SJacques Vidrine }
272f579bf8eSKris Kennaway 
RSA_sign(int type,const unsigned char * m,unsigned int m_len,unsigned char * sigret,unsigned int * siglen,RSA * rsa)273e71b7053SJung-uk Kim int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
274e71b7053SJung-uk Kim              unsigned char *sigret, unsigned int *siglen, RSA *rsa)
275e71b7053SJung-uk Kim {
276*b077aed3SPierre Pronchery     int encrypt_len, ret = 0;
277*b077aed3SPierre Pronchery     size_t encoded_len = 0;
278e71b7053SJung-uk Kim     unsigned char *tmps = NULL;
279e71b7053SJung-uk Kim     const unsigned char *encoded = NULL;
280e71b7053SJung-uk Kim 
281*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
282*b077aed3SPierre Pronchery     if (rsa->meth->rsa_sign != NULL)
283*b077aed3SPierre Pronchery         return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa) > 0;
284*b077aed3SPierre Pronchery #endif /* FIPS_MODULE */
285e71b7053SJung-uk Kim 
286e71b7053SJung-uk Kim     /* Compute the encoded digest. */
287e71b7053SJung-uk Kim     if (type == NID_md5_sha1) {
288e71b7053SJung-uk Kim         /*
289e71b7053SJung-uk Kim          * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
290e71b7053SJung-uk Kim          * earlier. It has no DigestInfo wrapper but otherwise is
291e71b7053SJung-uk Kim          * RSASSA-PKCS1-v1_5.
292e71b7053SJung-uk Kim          */
293e71b7053SJung-uk Kim         if (m_len != SSL_SIG_LENGTH) {
294*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
295e71b7053SJung-uk Kim             return 0;
296e71b7053SJung-uk Kim         }
297e71b7053SJung-uk Kim         encoded_len = SSL_SIG_LENGTH;
298e71b7053SJung-uk Kim         encoded = m;
299e71b7053SJung-uk Kim     } else {
300e71b7053SJung-uk Kim         if (!encode_pkcs1(&tmps, &encoded_len, type, m, m_len))
301e71b7053SJung-uk Kim             goto err;
302e71b7053SJung-uk Kim         encoded = tmps;
303e71b7053SJung-uk Kim     }
304e71b7053SJung-uk Kim 
305*b077aed3SPierre Pronchery     if (encoded_len + RSA_PKCS1_PADDING_SIZE > (size_t)RSA_size(rsa)) {
306*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
307e71b7053SJung-uk Kim         goto err;
308e71b7053SJung-uk Kim     }
309*b077aed3SPierre Pronchery     encrypt_len = RSA_private_encrypt((int)encoded_len, encoded, sigret, rsa,
310e71b7053SJung-uk Kim                                       RSA_PKCS1_PADDING);
311e71b7053SJung-uk Kim     if (encrypt_len <= 0)
312e71b7053SJung-uk Kim         goto err;
313e71b7053SJung-uk Kim 
314e71b7053SJung-uk Kim     *siglen = encrypt_len;
315e71b7053SJung-uk Kim     ret = 1;
316e71b7053SJung-uk Kim 
317e71b7053SJung-uk Kim err:
318*b077aed3SPierre Pronchery     OPENSSL_clear_free(tmps, encoded_len);
319e71b7053SJung-uk Kim     return ret;
320e71b7053SJung-uk Kim }
321e71b7053SJung-uk Kim 
322e71b7053SJung-uk Kim /*
323*b077aed3SPierre Pronchery  * Verify an RSA signature in |sigbuf| using |rsa|.
324*b077aed3SPierre Pronchery  * |type| is the NID of the digest algorithm to use.
325*b077aed3SPierre Pronchery  * If |rm| is NULL, it verifies the signature for digest |m|, otherwise
326*b077aed3SPierre Pronchery  * it recovers the digest from the signature, writing the digest to |rm| and
327*b077aed3SPierre Pronchery  * the length to |*prm_len|.
328*b077aed3SPierre Pronchery  *
329*b077aed3SPierre Pronchery  * It returns one on successful verification or zero otherwise.
330e71b7053SJung-uk Kim  */
ossl_rsa_verify(int type,const unsigned char * m,unsigned int m_len,unsigned char * rm,size_t * prm_len,const unsigned char * sigbuf,size_t siglen,RSA * rsa)331*b077aed3SPierre Pronchery int ossl_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
332e71b7053SJung-uk Kim                     unsigned char *rm, size_t *prm_len,
333e71b7053SJung-uk Kim                     const unsigned char *sigbuf, size_t siglen, RSA *rsa)
334e71b7053SJung-uk Kim {
335*b077aed3SPierre Pronchery     int len, ret = 0;
336*b077aed3SPierre Pronchery     size_t decrypt_len, encoded_len = 0;
337e71b7053SJung-uk Kim     unsigned char *decrypt_buf = NULL, *encoded = NULL;
338e71b7053SJung-uk Kim 
339e71b7053SJung-uk Kim     if (siglen != (size_t)RSA_size(rsa)) {
340*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
341e71b7053SJung-uk Kim         return 0;
342e71b7053SJung-uk Kim     }
343e71b7053SJung-uk Kim 
344e71b7053SJung-uk Kim     /* Recover the encoded digest. */
345e71b7053SJung-uk Kim     decrypt_buf = OPENSSL_malloc(siglen);
346e71b7053SJung-uk Kim     if (decrypt_buf == NULL) {
347*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
34897b2ed56SKris Kennaway         goto err;
34997b2ed56SKris Kennaway     }
350e71b7053SJung-uk Kim 
351*b077aed3SPierre Pronchery     len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
352e71b7053SJung-uk Kim                              RSA_PKCS1_PADDING);
353*b077aed3SPierre Pronchery     if (len <= 0)
354e71b7053SJung-uk Kim         goto err;
355*b077aed3SPierre Pronchery     decrypt_len = len;
356e71b7053SJung-uk Kim 
357*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
358e71b7053SJung-uk Kim     if (type == NID_md5_sha1) {
359e71b7053SJung-uk Kim         /*
360e71b7053SJung-uk Kim          * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
361e71b7053SJung-uk Kim          * earlier. It has no DigestInfo wrapper but otherwise is
362e71b7053SJung-uk Kim          * RSASSA-PKCS1-v1_5.
363e71b7053SJung-uk Kim          */
364e71b7053SJung-uk Kim         if (decrypt_len != SSL_SIG_LENGTH) {
365*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
366e71b7053SJung-uk Kim             goto err;
367e71b7053SJung-uk Kim         }
368e71b7053SJung-uk Kim 
369e71b7053SJung-uk Kim         if (rm != NULL) {
370e71b7053SJung-uk Kim             memcpy(rm, decrypt_buf, SSL_SIG_LENGTH);
371e71b7053SJung-uk Kim             *prm_len = SSL_SIG_LENGTH;
372e71b7053SJung-uk Kim         } else {
373e71b7053SJung-uk Kim             if (m_len != SSL_SIG_LENGTH) {
374*b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
3753b4e3dcbSSimon L. B. Nielsen                 goto err;
376f579bf8eSKris Kennaway             }
37797b2ed56SKris Kennaway 
378e71b7053SJung-uk Kim             if (memcmp(decrypt_buf, m, SSL_SIG_LENGTH) != 0) {
379*b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
3806f9291ceSJung-uk Kim                 goto err;
381e71b7053SJung-uk Kim             }
382e71b7053SJung-uk Kim         }
383e71b7053SJung-uk Kim     } else if (type == NID_mdc2 && decrypt_len == 2 + 16
384e71b7053SJung-uk Kim                && decrypt_buf[0] == 0x04 && decrypt_buf[1] == 0x10) {
3856f9291ceSJung-uk Kim         /*
3866f9291ceSJung-uk Kim          * Oddball MDC2 case: signature can be OCTET STRING. check for correct
3876f9291ceSJung-uk Kim          * tag and length octets.
3881f13597dSJung-uk Kim          */
389e71b7053SJung-uk Kim         if (rm != NULL) {
390e71b7053SJung-uk Kim             memcpy(rm, decrypt_buf + 2, 16);
3911f13597dSJung-uk Kim             *prm_len = 16;
39280815a77SJung-uk Kim         } else {
393e71b7053SJung-uk Kim             if (m_len != 16) {
394*b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
395c800238eSSimon L. B. Nielsen                 goto err;
396c800238eSSimon L. B. Nielsen             }
397c800238eSSimon L. B. Nielsen 
398e71b7053SJung-uk Kim             if (memcmp(m, decrypt_buf + 2, 16) != 0) {
399*b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
400e71b7053SJung-uk Kim                 goto err;
401e71b7053SJung-uk Kim             }
402e71b7053SJung-uk Kim         }
403*b077aed3SPierre Pronchery     } else
404*b077aed3SPierre Pronchery #endif /* FIPS_MODULE */
405*b077aed3SPierre Pronchery     {
4066f9291ceSJung-uk Kim         /*
407e71b7053SJung-uk Kim          * If recovering the digest, extract a digest-sized output from the end
408e71b7053SJung-uk Kim          * of |decrypt_buf| for |encode_pkcs1|, then compare the decryption
409e71b7053SJung-uk Kim          * output as in a standard verification.
4106f9291ceSJung-uk Kim          */
411e71b7053SJung-uk Kim         if (rm != NULL) {
412*b077aed3SPierre Pronchery             len = digest_sz_from_nid(type);
413c800238eSSimon L. B. Nielsen 
414*b077aed3SPierre Pronchery             if (len <= 0)
415*b077aed3SPierre Pronchery                 goto err;
416*b077aed3SPierre Pronchery             m_len = (unsigned int)len;
417*b077aed3SPierre Pronchery             if (m_len > decrypt_len) {
418*b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
419e71b7053SJung-uk Kim                 goto err;
4201f13597dSJung-uk Kim             }
421e71b7053SJung-uk Kim             m = decrypt_buf + decrypt_len - m_len;
42297b2ed56SKris Kennaway         }
42397b2ed56SKris Kennaway 
424e71b7053SJung-uk Kim         /* Construct the encoded digest and ensure it matches. */
425e71b7053SJung-uk Kim         if (!encode_pkcs1(&encoded, &encoded_len, type, m, m_len))
426e71b7053SJung-uk Kim             goto err;
427e71b7053SJung-uk Kim 
428e71b7053SJung-uk Kim         if (encoded_len != decrypt_len
429e71b7053SJung-uk Kim                 || memcmp(encoded, decrypt_buf, encoded_len) != 0) {
430*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
431e71b7053SJung-uk Kim             goto err;
432e71b7053SJung-uk Kim         }
433e71b7053SJung-uk Kim 
434e71b7053SJung-uk Kim         /* Output the recovered digest. */
435e71b7053SJung-uk Kim         if (rm != NULL) {
436e71b7053SJung-uk Kim             memcpy(rm, m, m_len);
437e71b7053SJung-uk Kim             *prm_len = m_len;
438e71b7053SJung-uk Kim         }
439e71b7053SJung-uk Kim     }
440e71b7053SJung-uk Kim 
441e71b7053SJung-uk Kim     ret = 1;
442e71b7053SJung-uk Kim 
443e71b7053SJung-uk Kim err:
444*b077aed3SPierre Pronchery     OPENSSL_clear_free(encoded, encoded_len);
445e71b7053SJung-uk Kim     OPENSSL_clear_free(decrypt_buf, siglen);
446e71b7053SJung-uk Kim     return ret;
447e71b7053SJung-uk Kim }
448e71b7053SJung-uk Kim 
RSA_verify(int type,const unsigned char * m,unsigned int m_len,const unsigned char * sigbuf,unsigned int siglen,RSA * rsa)449e71b7053SJung-uk Kim int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
4506f9291ceSJung-uk Kim                const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
4511f13597dSJung-uk Kim {
4521f13597dSJung-uk Kim 
453*b077aed3SPierre Pronchery     if (rsa->meth->rsa_verify != NULL)
454e71b7053SJung-uk Kim         return rsa->meth->rsa_verify(type, m, m_len, sigbuf, siglen, rsa);
4551f13597dSJung-uk Kim 
456*b077aed3SPierre Pronchery     return ossl_rsa_verify(type, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
4571f13597dSJung-uk Kim }
458