/src/openssl/crypto/rsa/rsa_pmeth.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2006-2024 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  |  |  * RSA low level APIs are deprecated for public use, but still ok for  | 
12  |  |  * internal use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include "internal/constant_time.h"  | 
17  |  |  | 
18  |  | #include <stdio.h>  | 
19  |  | #include "internal/cryptlib.h"  | 
20  |  | #include <openssl/asn1t.h>  | 
21  |  | #include <openssl/x509.h>  | 
22  |  | #include <openssl/rsa.h>  | 
23  |  | #include <openssl/bn.h>  | 
24  |  | #include <openssl/evp.h>  | 
25  |  | #include <openssl/x509v3.h>  | 
26  |  | #include <openssl/cms.h>  | 
27  |  | #include "crypto/evp.h"  | 
28  |  | #include "crypto/rsa.h"  | 
29  |  | #include "rsa_local.h"  | 
30  |  |  | 
31  |  | /* RSA pkey context structure */  | 
32  |  |  | 
33  |  | typedef struct { | 
34  |  |     /* Key gen parameters */  | 
35  |  |     int nbits;  | 
36  |  |     BIGNUM *pub_exp;  | 
37  |  |     int primes;  | 
38  |  |     /* Keygen callback info */  | 
39  |  |     int gentmp[2];  | 
40  |  |     /* RSA padding mode */  | 
41  |  |     int pad_mode;  | 
42  |  |     /* message digest */  | 
43  |  |     const EVP_MD *md;  | 
44  |  |     /* message digest for MGF1 */  | 
45  |  |     const EVP_MD *mgf1md;  | 
46  |  |     /* PSS salt length */  | 
47  |  |     int saltlen;  | 
48  |  |     /* Minimum salt length or -1 if no PSS parameter restriction */  | 
49  |  |     int min_saltlen;  | 
50  |  |     /* Temp buffer */  | 
51  |  |     unsigned char *tbuf;  | 
52  |  |     /* OAEP label */  | 
53  |  |     unsigned char *oaep_label;  | 
54  |  |     size_t oaep_labellen;  | 
55  |  |     /* if to use implicit rejection in PKCS#1 v1.5 decryption */  | 
56  |  |     int implicit_rejection;  | 
57  |  | } RSA_PKEY_CTX;  | 
58  |  |  | 
59  |  | /* True if PSS parameters are restricted */  | 
60  | 0  | #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)  | 
61  |  |  | 
62  |  | static int pkey_rsa_init(EVP_PKEY_CTX *ctx)  | 
63  | 0  | { | 
64  | 0  |     RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));  | 
65  |  | 
  | 
66  | 0  |     if (rctx == NULL)  | 
67  | 0  |         return 0;  | 
68  | 0  |     rctx->nbits = 2048;  | 
69  | 0  |     rctx->primes = RSA_DEFAULT_PRIME_NUM;  | 
70  | 0  |     if (pkey_ctx_is_pss(ctx))  | 
71  | 0  |         rctx->pad_mode = RSA_PKCS1_PSS_PADDING;  | 
72  | 0  |     else  | 
73  | 0  |         rctx->pad_mode = RSA_PKCS1_PADDING;  | 
74  |  |     /* Maximum for sign, auto for verify */  | 
75  | 0  |     rctx->saltlen = RSA_PSS_SALTLEN_AUTO;  | 
76  | 0  |     rctx->min_saltlen = -1;  | 
77  | 0  |     rctx->implicit_rejection = 1;  | 
78  | 0  |     ctx->data = rctx;  | 
79  | 0  |     ctx->keygen_info = rctx->gentmp;  | 
80  | 0  |     ctx->keygen_info_count = 2;  | 
81  |  | 
  | 
82  | 0  |     return 1;  | 
83  | 0  | }  | 
84  |  |  | 
85  |  | static int pkey_rsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)  | 
86  | 0  | { | 
87  | 0  |     RSA_PKEY_CTX *dctx, *sctx;  | 
88  |  | 
  | 
89  | 0  |     if (!pkey_rsa_init(dst))  | 
90  | 0  |         return 0;  | 
91  | 0  |     sctx = src->data;  | 
92  | 0  |     dctx = dst->data;  | 
93  | 0  |     dctx->nbits = sctx->nbits;  | 
94  | 0  |     if (sctx->pub_exp) { | 
95  | 0  |         dctx->pub_exp = BN_dup(sctx->pub_exp);  | 
96  | 0  |         if (!dctx->pub_exp)  | 
97  | 0  |             return 0;  | 
98  | 0  |     }  | 
99  | 0  |     dctx->pad_mode = sctx->pad_mode;  | 
100  | 0  |     dctx->md = sctx->md;  | 
101  | 0  |     dctx->mgf1md = sctx->mgf1md;  | 
102  | 0  |     dctx->saltlen = sctx->saltlen;  | 
103  | 0  |     dctx->implicit_rejection = sctx->implicit_rejection;  | 
104  | 0  |     if (sctx->oaep_label) { | 
105  | 0  |         OPENSSL_free(dctx->oaep_label);  | 
106  | 0  |         dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);  | 
107  | 0  |         if (!dctx->oaep_label)  | 
108  | 0  |             return 0;  | 
109  | 0  |         dctx->oaep_labellen = sctx->oaep_labellen;  | 
110  | 0  |     }  | 
111  | 0  |     return 1;  | 
112  | 0  | }  | 
113  |  |  | 
114  |  | static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)  | 
115  | 0  | { | 
116  | 0  |     if (ctx->tbuf != NULL)  | 
117  | 0  |         return 1;  | 
118  | 0  |     if ((ctx->tbuf =  | 
119  | 0  |             OPENSSL_malloc(RSA_size(EVP_PKEY_get0_RSA(pk->pkey)))) == NULL)  | 
120  | 0  |         return 0;  | 
121  | 0  |     return 1;  | 
122  | 0  | }  | 
123  |  |  | 
124  |  | static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)  | 
125  | 0  | { | 
126  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
127  | 0  |     if (rctx) { | 
128  | 0  |         BN_free(rctx->pub_exp);  | 
129  | 0  |         OPENSSL_free(rctx->tbuf);  | 
130  | 0  |         OPENSSL_free(rctx->oaep_label);  | 
131  | 0  |         OPENSSL_free(rctx);  | 
132  | 0  |     }  | 
133  | 0  | }  | 
134  |  |  | 
135  |  | static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,  | 
136  |  |                          size_t *siglen, const unsigned char *tbs,  | 
137  |  |                          size_t tbslen)  | 
138  | 0  | { | 
139  | 0  |     int ret;  | 
140  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
141  |  |     /*  | 
142  |  |      * Discard const. Its marked as const because this may be a cached copy of  | 
143  |  |      * the "real" key. These calls don't make any modifications that need to  | 
144  |  |      * be reflected back in the "original" key.  | 
145  |  |      */  | 
146  | 0  |     RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);  | 
147  | 0  |     int md_size;  | 
148  |  | 
  | 
149  | 0  |     if (rctx->md) { | 
150  | 0  |         md_size = EVP_MD_get_size(rctx->md);  | 
151  | 0  |         if (md_size <= 0) { | 
152  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);  | 
153  | 0  |             return -1;  | 
154  | 0  |         }  | 
155  |  |  | 
156  | 0  |         if (tbslen != (size_t)md_size) { | 
157  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);  | 
158  | 0  |             return -1;  | 
159  | 0  |         }  | 
160  |  |  | 
161  | 0  |         if (EVP_MD_get_type(rctx->md) == NID_mdc2) { | 
162  | 0  |             unsigned int sltmp;  | 
163  | 0  |             if (rctx->pad_mode != RSA_PKCS1_PADDING)  | 
164  | 0  |                 return -1;  | 
165  | 0  |             ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp, rsa);  | 
166  |  | 
  | 
167  | 0  |             if (ret <= 0)  | 
168  | 0  |                 return ret;  | 
169  | 0  |             ret = sltmp;  | 
170  | 0  |         } else if (rctx->pad_mode == RSA_X931_PADDING) { | 
171  | 0  |             if ((size_t)RSA_size(rsa) < tbslen + 1) { | 
172  | 0  |                 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);  | 
173  | 0  |                 return -1;  | 
174  | 0  |             }  | 
175  | 0  |             if (!setup_tbuf(rctx, ctx)) { | 
176  | 0  |                 ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);  | 
177  | 0  |                 return -1;  | 
178  | 0  |             }  | 
179  | 0  |             memcpy(rctx->tbuf, tbs, tbslen);  | 
180  | 0  |             rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_get_type(rctx->md));  | 
181  | 0  |             ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,  | 
182  | 0  |                                       sig, rsa, RSA_X931_PADDING);  | 
183  | 0  |         } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { | 
184  | 0  |             unsigned int sltmp;  | 
185  | 0  |             ret = RSA_sign(EVP_MD_get_type(rctx->md),  | 
186  | 0  |                            tbs, tbslen, sig, &sltmp, rsa);  | 
187  | 0  |             if (ret <= 0)  | 
188  | 0  |                 return ret;  | 
189  | 0  |             ret = sltmp;  | 
190  | 0  |         } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { | 
191  | 0  |             if (!setup_tbuf(rctx, ctx))  | 
192  | 0  |                 return -1;  | 
193  | 0  |             if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,  | 
194  | 0  |                                                 rctx->tbuf, tbs,  | 
195  | 0  |                                                 rctx->md, rctx->mgf1md,  | 
196  | 0  |                                                 rctx->saltlen))  | 
197  | 0  |                 return -1;  | 
198  | 0  |             ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,  | 
199  | 0  |                                       sig, rsa, RSA_NO_PADDING);  | 
200  | 0  |         } else { | 
201  | 0  |             return -1;  | 
202  | 0  |         }  | 
203  | 0  |     } else { | 
204  | 0  |         ret = RSA_private_encrypt(tbslen, tbs, sig, rsa, rctx->pad_mode);  | 
205  | 0  |     }  | 
206  | 0  |     if (ret < 0)  | 
207  | 0  |         return ret;  | 
208  | 0  |     *siglen = ret;  | 
209  | 0  |     return 1;  | 
210  | 0  | }  | 
211  |  |  | 
212  |  | static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,  | 
213  |  |                                   unsigned char *rout, size_t *routlen,  | 
214  |  |                                   const unsigned char *sig, size_t siglen)  | 
215  | 0  | { | 
216  | 0  |     int ret;  | 
217  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
218  |  |     /*  | 
219  |  |      * Discard const. Its marked as const because this may be a cached copy of  | 
220  |  |      * the "real" key. These calls don't make any modifications that need to  | 
221  |  |      * be reflected back in the "original" key.  | 
222  |  |      */  | 
223  | 0  |     RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);  | 
224  |  | 
  | 
225  | 0  |     if (rctx->md) { | 
226  | 0  |         if (rctx->pad_mode == RSA_X931_PADDING) { | 
227  | 0  |             if (!setup_tbuf(rctx, ctx))  | 
228  | 0  |                 return -1;  | 
229  | 0  |             ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, rsa,  | 
230  | 0  |                                      RSA_X931_PADDING);  | 
231  | 0  |             if (ret < 1)  | 
232  | 0  |                 return 0;  | 
233  | 0  |             ret--;  | 
234  | 0  |             if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_get_type(rctx->md))) { | 
235  | 0  |                 ERR_raise(ERR_LIB_RSA, RSA_R_ALGORITHM_MISMATCH);  | 
236  | 0  |                 return 0;  | 
237  | 0  |             }  | 
238  | 0  |             if (ret != EVP_MD_get_size(rctx->md)) { | 
239  | 0  |                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);  | 
240  | 0  |                 return 0;  | 
241  | 0  |             }  | 
242  | 0  |             if (rout)  | 
243  | 0  |                 memcpy(rout, rctx->tbuf, ret);  | 
244  | 0  |         } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { | 
245  | 0  |             size_t sltmp;  | 
246  | 0  |             ret = ossl_rsa_verify(EVP_MD_get_type(rctx->md),  | 
247  | 0  |                                   NULL, 0, rout, &sltmp,  | 
248  | 0  |                                   sig, siglen, rsa);  | 
249  | 0  |             if (ret <= 0)  | 
250  | 0  |                 return 0;  | 
251  | 0  |             ret = sltmp;  | 
252  | 0  |         } else { | 
253  | 0  |             return -1;  | 
254  | 0  |         }  | 
255  | 0  |     } else { | 
256  | 0  |         ret = RSA_public_decrypt(siglen, sig, rout, rsa, rctx->pad_mode);  | 
257  | 0  |     }  | 
258  | 0  |     if (ret < 0)  | 
259  | 0  |         return ret;  | 
260  | 0  |     *routlen = ret;  | 
261  | 0  |     return 1;  | 
262  | 0  | }  | 
263  |  |  | 
264  |  | static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,  | 
265  |  |                            const unsigned char *sig, size_t siglen,  | 
266  |  |                            const unsigned char *tbs, size_t tbslen)  | 
267  | 0  | { | 
268  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
269  |  |     /*  | 
270  |  |      * Discard const. Its marked as const because this may be a cached copy of  | 
271  |  |      * the "real" key. These calls don't make any modifications that need to  | 
272  |  |      * be reflected back in the "original" key.  | 
273  |  |      */  | 
274  | 0  |     RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);  | 
275  | 0  |     size_t rslen;  | 
276  | 0  |     int md_size;  | 
277  |  | 
  | 
278  | 0  |     if (rctx->md) { | 
279  | 0  |         if (rctx->pad_mode == RSA_PKCS1_PADDING)  | 
280  | 0  |             return RSA_verify(EVP_MD_get_type(rctx->md), tbs, tbslen,  | 
281  | 0  |                               sig, siglen, rsa);  | 
282  | 0  |         md_size = EVP_MD_get_size(rctx->md);  | 
283  | 0  |         if (md_size <= 0) { | 
284  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);  | 
285  | 0  |             return -1;  | 
286  | 0  |         }  | 
287  | 0  |         if (tbslen != (size_t)md_size) { | 
288  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);  | 
289  | 0  |             return -1;  | 
290  | 0  |         }  | 
291  | 0  |         if (rctx->pad_mode == RSA_X931_PADDING) { | 
292  | 0  |             if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)  | 
293  | 0  |                 return 0;  | 
294  | 0  |         } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { | 
295  | 0  |             int ret;  | 
296  | 0  |             if (!setup_tbuf(rctx, ctx))  | 
297  | 0  |                 return -1;  | 
298  | 0  |             ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,  | 
299  | 0  |                                      rsa, RSA_NO_PADDING);  | 
300  | 0  |             if (ret <= 0)  | 
301  | 0  |                 return 0;  | 
302  | 0  |             ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,  | 
303  | 0  |                                             rctx->md, rctx->mgf1md,  | 
304  | 0  |                                             rctx->tbuf, rctx->saltlen);  | 
305  | 0  |             if (ret <= 0)  | 
306  | 0  |                 return 0;  | 
307  | 0  |             return 1;  | 
308  | 0  |         } else { | 
309  | 0  |             return -1;  | 
310  | 0  |         }  | 
311  | 0  |     } else { | 
312  | 0  |         if (!setup_tbuf(rctx, ctx))  | 
313  | 0  |             return -1;  | 
314  | 0  |         rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,  | 
315  | 0  |                                    rsa, rctx->pad_mode);  | 
316  | 0  |         if (rslen == 0)  | 
317  | 0  |             return 0;  | 
318  | 0  |     }  | 
319  |  |  | 
320  | 0  |     if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))  | 
321  | 0  |         return 0;  | 
322  |  |  | 
323  | 0  |     return 1;  | 
324  |  | 
  | 
325  | 0  | }  | 
326  |  |  | 
327  |  | static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,  | 
328  |  |                             unsigned char *out, size_t *outlen,  | 
329  |  |                             const unsigned char *in, size_t inlen)  | 
330  | 0  | { | 
331  | 0  |     int ret;  | 
332  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
333  |  |     /*  | 
334  |  |      * Discard const. Its marked as const because this may be a cached copy of  | 
335  |  |      * the "real" key. These calls don't make any modifications that need to  | 
336  |  |      * be reflected back in the "original" key.  | 
337  |  |      */  | 
338  | 0  |     RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);  | 
339  |  | 
  | 
340  | 0  |     if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { | 
341  | 0  |         int klen = RSA_size(rsa);  | 
342  | 0  |         if (!setup_tbuf(rctx, ctx))  | 
343  | 0  |             return -1;  | 
344  | 0  |         if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,  | 
345  | 0  |                                              in, inlen,  | 
346  | 0  |                                              rctx->oaep_label,  | 
347  | 0  |                                              rctx->oaep_labellen,  | 
348  | 0  |                                              rctx->md, rctx->mgf1md))  | 
349  | 0  |             return -1;  | 
350  | 0  |         ret = RSA_public_encrypt(klen, rctx->tbuf, out, rsa, RSA_NO_PADDING);  | 
351  | 0  |     } else { | 
352  | 0  |         ret = RSA_public_encrypt(inlen, in, out, rsa, rctx->pad_mode);  | 
353  | 0  |     }  | 
354  | 0  |     if (ret < 0)  | 
355  | 0  |         return ret;  | 
356  | 0  |     *outlen = ret;  | 
357  | 0  |     return 1;  | 
358  | 0  | }  | 
359  |  |  | 
360  |  | static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,  | 
361  |  |                             unsigned char *out, size_t *outlen,  | 
362  |  |                             const unsigned char *in, size_t inlen)  | 
363  | 0  | { | 
364  | 0  |     int ret;  | 
365  | 0  |     int pad_mode;  | 
366  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
367  |  |     /*  | 
368  |  |      * Discard const. Its marked as const because this may be a cached copy of  | 
369  |  |      * the "real" key. These calls don't make any modifications that need to  | 
370  |  |      * be reflected back in the "original" key.  | 
371  |  |      */  | 
372  | 0  |     RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);  | 
373  |  | 
  | 
374  | 0  |     if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { | 
375  | 0  |         if (!setup_tbuf(rctx, ctx))  | 
376  | 0  |             return -1;  | 
377  | 0  |         ret = RSA_private_decrypt(inlen, in, rctx->tbuf, rsa, RSA_NO_PADDING);  | 
378  | 0  |         if (ret <= 0)  | 
379  | 0  |             return ret;  | 
380  | 0  |         ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,  | 
381  | 0  |                                                 ret, ret,  | 
382  | 0  |                                                 rctx->oaep_label,  | 
383  | 0  |                                                 rctx->oaep_labellen,  | 
384  | 0  |                                                 rctx->md, rctx->mgf1md);  | 
385  | 0  |     } else { | 
386  | 0  |         if (rctx->pad_mode == RSA_PKCS1_PADDING &&  | 
387  | 0  |               rctx->implicit_rejection == 0)  | 
388  | 0  |             pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;  | 
389  | 0  |         else  | 
390  | 0  |             pad_mode = rctx->pad_mode;  | 
391  | 0  |         ret = RSA_private_decrypt(inlen, in, out, rsa, pad_mode);  | 
392  | 0  |     }  | 
393  | 0  |     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);  | 
394  | 0  |     ret = constant_time_select_int(constant_time_msb(ret), ret, 1);  | 
395  | 0  |     return ret;  | 
396  | 0  | }  | 
397  |  |  | 
398  |  | static int check_padding_md(const EVP_MD *md, int padding)  | 
399  | 0  | { | 
400  | 0  |     int mdnid;  | 
401  |  | 
  | 
402  | 0  |     if (!md)  | 
403  | 0  |         return 1;  | 
404  |  |  | 
405  | 0  |     mdnid = EVP_MD_get_type(md);  | 
406  |  | 
  | 
407  | 0  |     if (padding == RSA_NO_PADDING) { | 
408  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);  | 
409  | 0  |         return 0;  | 
410  | 0  |     }  | 
411  |  |  | 
412  | 0  |     if (padding == RSA_X931_PADDING) { | 
413  | 0  |         if (RSA_X931_hash_id(mdnid) == -1) { | 
414  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_X931_DIGEST);  | 
415  | 0  |             return 0;  | 
416  | 0  |         }  | 
417  | 0  |     } else { | 
418  | 0  |         switch (mdnid) { | 
419  |  |         /* List of all supported RSA digests */  | 
420  | 0  |         case NID_sha1:  | 
421  | 0  |         case NID_sha224:  | 
422  | 0  |         case NID_sha256:  | 
423  | 0  |         case NID_sha384:  | 
424  | 0  |         case NID_sha512:  | 
425  | 0  |         case NID_sha512_224:  | 
426  | 0  |         case NID_sha512_256:  | 
427  | 0  |         case NID_md5:  | 
428  | 0  |         case NID_md5_sha1:  | 
429  | 0  |         case NID_md2:  | 
430  | 0  |         case NID_md4:  | 
431  | 0  |         case NID_mdc2:  | 
432  | 0  |         case NID_ripemd160:  | 
433  | 0  |         case NID_sha3_224:  | 
434  | 0  |         case NID_sha3_256:  | 
435  | 0  |         case NID_sha3_384:  | 
436  | 0  |         case NID_sha3_512:  | 
437  | 0  |             return 1;  | 
438  |  |  | 
439  | 0  |         default:  | 
440  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST);  | 
441  | 0  |             return 0;  | 
442  |  | 
  | 
443  | 0  |         }  | 
444  | 0  |     }  | 
445  |  |  | 
446  | 0  |     return 1;  | 
447  | 0  | }  | 
448  |  |  | 
449  |  | static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)  | 
450  | 0  | { | 
451  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
452  | 0  |     int md_size;  | 
453  |  | 
  | 
454  | 0  |     switch (type) { | 
455  | 0  |     case EVP_PKEY_CTRL_RSA_PADDING:  | 
456  | 0  |         if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) { | 
457  | 0  |             if (!check_padding_md(rctx->md, p1))  | 
458  | 0  |                 return 0;  | 
459  | 0  |             if (p1 == RSA_PKCS1_PSS_PADDING) { | 
460  | 0  |                 if (!(ctx->operation &  | 
461  | 0  |                       (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))  | 
462  | 0  |                     goto bad_pad;  | 
463  | 0  |                 if (!rctx->md)  | 
464  | 0  |                     rctx->md = EVP_sha1();  | 
465  | 0  |             } else if (pkey_ctx_is_pss(ctx)) { | 
466  | 0  |                 goto bad_pad;  | 
467  | 0  |             }  | 
468  | 0  |             if (p1 == RSA_PKCS1_OAEP_PADDING) { | 
469  | 0  |                 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))  | 
470  | 0  |                     goto bad_pad;  | 
471  | 0  |                 if (!rctx->md)  | 
472  | 0  |                     rctx->md = EVP_sha1();  | 
473  | 0  |             }  | 
474  | 0  |             rctx->pad_mode = p1;  | 
475  | 0  |             return 1;  | 
476  | 0  |         }  | 
477  | 0  |  bad_pad:  | 
478  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);  | 
479  | 0  |         return -2;  | 
480  |  |  | 
481  | 0  |     case EVP_PKEY_CTRL_GET_RSA_PADDING:  | 
482  | 0  |         *(int *)p2 = rctx->pad_mode;  | 
483  | 0  |         return 1;  | 
484  |  |  | 
485  | 0  |     case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:  | 
486  | 0  |     case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:  | 
487  | 0  |         if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) { | 
488  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);  | 
489  | 0  |             return -2;  | 
490  | 0  |         }  | 
491  | 0  |         if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) { | 
492  | 0  |             *(int *)p2 = rctx->saltlen;  | 
493  | 0  |         } else { | 
494  | 0  |             if (p1 < RSA_PSS_SALTLEN_MAX)  | 
495  | 0  |                 return -2;  | 
496  | 0  |             if (rsa_pss_restricted(rctx)) { | 
497  | 0  |                 if (p1 == RSA_PSS_SALTLEN_AUTO  | 
498  | 0  |                     && ctx->operation == EVP_PKEY_OP_VERIFY) { | 
499  | 0  |                     ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);  | 
500  | 0  |                     return -2;  | 
501  | 0  |                 }  | 
502  | 0  |                 md_size = EVP_MD_get_size(rctx->md);  | 
503  | 0  |                 if (md_size <= 0) { | 
504  | 0  |                     ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);  | 
505  | 0  |                     return -2;  | 
506  | 0  |                 }  | 
507  | 0  |                 if ((p1 == RSA_PSS_SALTLEN_DIGEST  | 
508  | 0  |                      && rctx->min_saltlen > md_size)  | 
509  | 0  |                     || (p1 >= 0 && p1 < rctx->min_saltlen)) { | 
510  | 0  |                     ERR_raise(ERR_LIB_RSA, RSA_R_PSS_SALTLEN_TOO_SMALL);  | 
511  | 0  |                     return 0;  | 
512  | 0  |                 }  | 
513  | 0  |             }  | 
514  | 0  |             rctx->saltlen = p1;  | 
515  | 0  |         }  | 
516  | 0  |         return 1;  | 
517  |  |  | 
518  | 0  |     case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:  | 
519  | 0  |         if (p1 < RSA_MIN_MODULUS_BITS) { | 
520  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);  | 
521  | 0  |             return -2;  | 
522  | 0  |         }  | 
523  | 0  |         rctx->nbits = p1;  | 
524  | 0  |         return 1;  | 
525  |  |  | 
526  | 0  |     case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:  | 
527  | 0  |         if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) { | 
528  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);  | 
529  | 0  |             return -2;  | 
530  | 0  |         }  | 
531  | 0  |         BN_free(rctx->pub_exp);  | 
532  | 0  |         rctx->pub_exp = p2;  | 
533  | 0  |         return 1;  | 
534  |  |  | 
535  | 0  |     case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:  | 
536  | 0  |         if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) { | 
537  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);  | 
538  | 0  |             return -2;  | 
539  | 0  |         }  | 
540  | 0  |         rctx->primes = p1;  | 
541  | 0  |         return 1;  | 
542  |  |  | 
543  | 0  |     case EVP_PKEY_CTRL_RSA_OAEP_MD:  | 
544  | 0  |     case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:  | 
545  | 0  |         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | 
546  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);  | 
547  | 0  |             return -2;  | 
548  | 0  |         }  | 
549  | 0  |         if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)  | 
550  | 0  |             *(const EVP_MD **)p2 = rctx->md;  | 
551  | 0  |         else  | 
552  | 0  |             rctx->md = p2;  | 
553  | 0  |         return 1;  | 
554  |  |  | 
555  | 0  |     case EVP_PKEY_CTRL_MD:  | 
556  | 0  |         if (!check_padding_md(p2, rctx->pad_mode))  | 
557  | 0  |             return 0;  | 
558  | 0  |         if (rsa_pss_restricted(rctx)) { | 
559  | 0  |             if (EVP_MD_get_type(rctx->md) == EVP_MD_get_type(p2))  | 
560  | 0  |                 return 1;  | 
561  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);  | 
562  | 0  |             return 0;  | 
563  | 0  |         }  | 
564  | 0  |         rctx->md = p2;  | 
565  | 0  |         return 1;  | 
566  |  |  | 
567  | 0  |     case EVP_PKEY_CTRL_GET_MD:  | 
568  | 0  |         *(const EVP_MD **)p2 = rctx->md;  | 
569  | 0  |         return 1;  | 
570  |  |  | 
571  | 0  |     case EVP_PKEY_CTRL_RSA_MGF1_MD:  | 
572  | 0  |     case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:  | 
573  | 0  |         if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING  | 
574  | 0  |             && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | 
575  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MGF1_MD);  | 
576  | 0  |             return -2;  | 
577  | 0  |         }  | 
578  | 0  |         if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) { | 
579  | 0  |             if (rctx->mgf1md)  | 
580  | 0  |                 *(const EVP_MD **)p2 = rctx->mgf1md;  | 
581  | 0  |             else  | 
582  | 0  |                 *(const EVP_MD **)p2 = rctx->md;  | 
583  | 0  |         } else { | 
584  | 0  |             if (rsa_pss_restricted(rctx)) { | 
585  | 0  |                 if (EVP_MD_get_type(rctx->mgf1md) == EVP_MD_get_type(p2))  | 
586  | 0  |                     return 1;  | 
587  | 0  |                 ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);  | 
588  | 0  |                 return 0;  | 
589  | 0  |             }  | 
590  | 0  |             rctx->mgf1md = p2;  | 
591  | 0  |         }  | 
592  | 0  |         return 1;  | 
593  |  |  | 
594  | 0  |     case EVP_PKEY_CTRL_RSA_OAEP_LABEL:  | 
595  | 0  |         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | 
596  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);  | 
597  | 0  |             return -2;  | 
598  | 0  |         }  | 
599  | 0  |         OPENSSL_free(rctx->oaep_label);  | 
600  | 0  |         if (p2 && p1 > 0) { | 
601  | 0  |             rctx->oaep_label = p2;  | 
602  | 0  |             rctx->oaep_labellen = p1;  | 
603  | 0  |         } else { | 
604  | 0  |             rctx->oaep_label = NULL;  | 
605  | 0  |             rctx->oaep_labellen = 0;  | 
606  | 0  |         }  | 
607  | 0  |         return 1;  | 
608  |  |  | 
609  | 0  |     case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:  | 
610  | 0  |         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | 
611  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);  | 
612  | 0  |             return -2;  | 
613  | 0  |         }  | 
614  | 0  |         if (p2 == NULL) { | 
615  | 0  |             ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);  | 
616  | 0  |             return 0;  | 
617  | 0  |         }  | 
618  | 0  |         *(unsigned char **)p2 = rctx->oaep_label;  | 
619  | 0  |         return rctx->oaep_labellen;  | 
620  |  |  | 
621  | 0  |     case EVP_PKEY_CTRL_RSA_IMPLICIT_REJECTION:  | 
622  | 0  |         if (rctx->pad_mode != RSA_PKCS1_PADDING) { | 
623  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);  | 
624  | 0  |             return -2;  | 
625  | 0  |         }  | 
626  | 0  |         rctx->implicit_rejection = p1;  | 
627  | 0  |         return 1;  | 
628  |  |  | 
629  | 0  |     case EVP_PKEY_CTRL_DIGESTINIT:  | 
630  | 0  |     case EVP_PKEY_CTRL_PKCS7_SIGN:  | 
631  | 0  | #ifndef OPENSSL_NO_CMS  | 
632  | 0  |     case EVP_PKEY_CTRL_CMS_SIGN:  | 
633  | 0  | #endif  | 
634  | 0  |     return 1;  | 
635  |  |  | 
636  | 0  |     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:  | 
637  | 0  |     case EVP_PKEY_CTRL_PKCS7_DECRYPT:  | 
638  | 0  | #ifndef OPENSSL_NO_CMS  | 
639  | 0  |     case EVP_PKEY_CTRL_CMS_DECRYPT:  | 
640  | 0  |     case EVP_PKEY_CTRL_CMS_ENCRYPT:  | 
641  | 0  | #endif  | 
642  | 0  |     if (!pkey_ctx_is_pss(ctx))  | 
643  | 0  |         return 1;  | 
644  |  |     /* fall through */  | 
645  | 0  |     case EVP_PKEY_CTRL_PEER_KEY:  | 
646  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
647  | 0  |         return -2;  | 
648  |  |  | 
649  | 0  |     default:  | 
650  | 0  |         return -2;  | 
651  |  | 
  | 
652  | 0  |     }  | 
653  | 0  | }  | 
654  |  |  | 
655  |  | static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,  | 
656  |  |                              const char *type, const char *value)  | 
657  | 0  | { | 
658  | 0  |     if (value == NULL) { | 
659  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING);  | 
660  | 0  |         return 0;  | 
661  | 0  |     }  | 
662  | 0  |     if (strcmp(type, "rsa_padding_mode") == 0) { | 
663  | 0  |         int pm;  | 
664  |  | 
  | 
665  | 0  |         if (strcmp(value, "pkcs1") == 0) { | 
666  | 0  |             pm = RSA_PKCS1_PADDING;  | 
667  | 0  |         } else if (strcmp(value, "none") == 0) { | 
668  | 0  |             pm = RSA_NO_PADDING;  | 
669  | 0  |         } else if (strcmp(value, "oeap") == 0) { | 
670  | 0  |             pm = RSA_PKCS1_OAEP_PADDING;  | 
671  | 0  |         } else if (strcmp(value, "oaep") == 0) { | 
672  | 0  |             pm = RSA_PKCS1_OAEP_PADDING;  | 
673  | 0  |         } else if (strcmp(value, "x931") == 0) { | 
674  | 0  |             pm = RSA_X931_PADDING;  | 
675  | 0  |         } else if (strcmp(value, "pss") == 0) { | 
676  | 0  |             pm = RSA_PKCS1_PSS_PADDING;  | 
677  | 0  |         } else { | 
678  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);  | 
679  | 0  |             return -2;  | 
680  | 0  |         }  | 
681  | 0  |         return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);  | 
682  | 0  |     }  | 
683  |  |  | 
684  | 0  |     if (strcmp(type, "rsa_pss_saltlen") == 0) { | 
685  | 0  |         int saltlen;  | 
686  |  | 
  | 
687  | 0  |         if (!strcmp(value, "digest"))  | 
688  | 0  |             saltlen = RSA_PSS_SALTLEN_DIGEST;  | 
689  | 0  |         else if (!strcmp(value, "max"))  | 
690  | 0  |             saltlen = RSA_PSS_SALTLEN_MAX;  | 
691  | 0  |         else if (!strcmp(value, "auto"))  | 
692  | 0  |             saltlen = RSA_PSS_SALTLEN_AUTO;  | 
693  | 0  |         else  | 
694  | 0  |             saltlen = atoi(value);  | 
695  | 0  |         return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);  | 
696  | 0  |     }  | 
697  |  |  | 
698  | 0  |     if (strcmp(type, "rsa_keygen_bits") == 0) { | 
699  | 0  |         int nbits = atoi(value);  | 
700  |  | 
  | 
701  | 0  |         return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);  | 
702  | 0  |     }  | 
703  |  |  | 
704  | 0  |     if (strcmp(type, "rsa_keygen_pubexp") == 0) { | 
705  | 0  |         int ret;  | 
706  |  | 
  | 
707  | 0  |         BIGNUM *pubexp = NULL;  | 
708  | 0  |         if (!BN_asc2bn(&pubexp, value))  | 
709  | 0  |             return 0;  | 
710  | 0  |         ret = EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, pubexp);  | 
711  | 0  |         BN_free(pubexp);  | 
712  | 0  |         return ret;  | 
713  | 0  |     }  | 
714  |  |  | 
715  | 0  |     if (strcmp(type, "rsa_keygen_primes") == 0) { | 
716  | 0  |         int nprimes = atoi(value);  | 
717  |  | 
  | 
718  | 0  |         return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes);  | 
719  | 0  |     }  | 
720  |  |  | 
721  | 0  |     if (strcmp(type, "rsa_mgf1_md") == 0)  | 
722  | 0  |         return EVP_PKEY_CTX_md(ctx,  | 
723  | 0  |                                EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,  | 
724  | 0  |                                EVP_PKEY_CTRL_RSA_MGF1_MD, value);  | 
725  |  |  | 
726  | 0  |     if (pkey_ctx_is_pss(ctx)) { | 
727  |  | 
  | 
728  | 0  |         if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)  | 
729  | 0  |             return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,  | 
730  | 0  |                                    EVP_PKEY_CTRL_RSA_MGF1_MD, value);  | 
731  |  |  | 
732  | 0  |         if (strcmp(type, "rsa_pss_keygen_md") == 0)  | 
733  | 0  |             return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,  | 
734  | 0  |                                    EVP_PKEY_CTRL_MD, value);  | 
735  |  |  | 
736  | 0  |         if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) { | 
737  | 0  |             int saltlen = atoi(value);  | 
738  |  | 
  | 
739  | 0  |             return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);  | 
740  | 0  |         }  | 
741  | 0  |     }  | 
742  |  |  | 
743  | 0  |     if (strcmp(type, "rsa_oaep_md") == 0)  | 
744  | 0  |         return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,  | 
745  | 0  |                                EVP_PKEY_CTRL_RSA_OAEP_MD, value);  | 
746  |  |  | 
747  | 0  |     if (strcmp(type, "rsa_oaep_label") == 0) { | 
748  | 0  |         unsigned char *lab;  | 
749  | 0  |         long lablen;  | 
750  | 0  |         int ret;  | 
751  |  | 
  | 
752  | 0  |         lab = OPENSSL_hexstr2buf(value, &lablen);  | 
753  | 0  |         if (!lab)  | 
754  | 0  |             return 0;  | 
755  | 0  |         ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);  | 
756  | 0  |         if (ret <= 0)  | 
757  | 0  |             OPENSSL_free(lab);  | 
758  | 0  |         return ret;  | 
759  | 0  |     }  | 
760  |  |  | 
761  | 0  |     return -2;  | 
762  | 0  | }  | 
763  |  |  | 
764  |  | /* Set PSS parameters when generating a key, if necessary */  | 
765  |  | static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)  | 
766  | 0  | { | 
767  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
768  |  | 
  | 
769  | 0  |     if (!pkey_ctx_is_pss(ctx))  | 
770  | 0  |         return 1;  | 
771  |  |     /* If all parameters are default values don't set pss */  | 
772  | 0  |     if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)  | 
773  | 0  |         return 1;  | 
774  | 0  |     rsa->pss = ossl_rsa_pss_params_create(rctx->md, rctx->mgf1md,  | 
775  | 0  |                                           rctx->saltlen == -2  | 
776  | 0  |                                           ? 0 : rctx->saltlen);  | 
777  | 0  |     if (rsa->pss == NULL)  | 
778  | 0  |         return 0;  | 
779  | 0  |     return 1;  | 
780  | 0  | }  | 
781  |  |  | 
782  |  | static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)  | 
783  | 0  | { | 
784  | 0  |     RSA *rsa = NULL;  | 
785  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
786  | 0  |     BN_GENCB *pcb;  | 
787  | 0  |     int ret;  | 
788  |  | 
  | 
789  | 0  |     if (rctx->pub_exp == NULL) { | 
790  | 0  |         rctx->pub_exp = BN_new();  | 
791  | 0  |         if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))  | 
792  | 0  |             return 0;  | 
793  | 0  |     }  | 
794  | 0  |     rsa = RSA_new();  | 
795  | 0  |     if (rsa == NULL)  | 
796  | 0  |         return 0;  | 
797  | 0  |     if (ctx->pkey_gencb) { | 
798  | 0  |         pcb = BN_GENCB_new();  | 
799  | 0  |         if (pcb == NULL) { | 
800  | 0  |             RSA_free(rsa);  | 
801  | 0  |             return 0;  | 
802  | 0  |         }  | 
803  | 0  |         evp_pkey_set_cb_translate(pcb, ctx);  | 
804  | 0  |     } else { | 
805  | 0  |         pcb = NULL;  | 
806  | 0  |     }  | 
807  | 0  |     ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes,  | 
808  | 0  |                                        rctx->pub_exp, pcb);  | 
809  | 0  |     BN_GENCB_free(pcb);  | 
810  | 0  |     if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) { | 
811  | 0  |         RSA_free(rsa);  | 
812  | 0  |         return 0;  | 
813  | 0  |     }  | 
814  | 0  |     if (ret > 0)  | 
815  | 0  |         EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);  | 
816  | 0  |     else  | 
817  | 0  |         RSA_free(rsa);  | 
818  | 0  |     return ret;  | 
819  | 0  | }  | 
820  |  |  | 
821  |  | static const EVP_PKEY_METHOD rsa_pkey_meth = { | 
822  |  |     EVP_PKEY_RSA,  | 
823  |  |     EVP_PKEY_FLAG_AUTOARGLEN,  | 
824  |  |     pkey_rsa_init,  | 
825  |  |     pkey_rsa_copy,  | 
826  |  |     pkey_rsa_cleanup,  | 
827  |  |  | 
828  |  |     0, 0,  | 
829  |  |  | 
830  |  |     0,  | 
831  |  |     pkey_rsa_keygen,  | 
832  |  |  | 
833  |  |     0,  | 
834  |  |     pkey_rsa_sign,  | 
835  |  |  | 
836  |  |     0,  | 
837  |  |     pkey_rsa_verify,  | 
838  |  |  | 
839  |  |     0,  | 
840  |  |     pkey_rsa_verifyrecover,  | 
841  |  |  | 
842  |  |     0, 0, 0, 0,  | 
843  |  |  | 
844  |  |     0,  | 
845  |  |     pkey_rsa_encrypt,  | 
846  |  |  | 
847  |  |     0,  | 
848  |  |     pkey_rsa_decrypt,  | 
849  |  |  | 
850  |  |     0, 0,  | 
851  |  |  | 
852  |  |     pkey_rsa_ctrl,  | 
853  |  |     pkey_rsa_ctrl_str  | 
854  |  | };  | 
855  |  |  | 
856  |  | const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void)  | 
857  | 0  | { | 
858  | 0  |     return &rsa_pkey_meth;  | 
859  | 0  | }  | 
860  |  |  | 
861  |  | /*  | 
862  |  |  * Called for PSS sign or verify initialisation: checks PSS parameter  | 
863  |  |  * sanity and sets any restrictions on key usage.  | 
864  |  |  */  | 
865  |  |  | 
866  |  | static int pkey_pss_init(EVP_PKEY_CTX *ctx)  | 
867  | 0  | { | 
868  | 0  |     const RSA *rsa;  | 
869  | 0  |     RSA_PKEY_CTX *rctx = ctx->data;  | 
870  | 0  |     const EVP_MD *md;  | 
871  | 0  |     const EVP_MD *mgf1md;  | 
872  | 0  |     int min_saltlen, max_saltlen, md_size;  | 
873  |  |  | 
874  |  |     /* Should never happen */  | 
875  | 0  |     if (!pkey_ctx_is_pss(ctx))  | 
876  | 0  |         return 0;  | 
877  | 0  |     rsa = EVP_PKEY_get0_RSA(ctx->pkey);  | 
878  |  |     /* If no restrictions just return */  | 
879  | 0  |     if (rsa->pss == NULL)  | 
880  | 0  |         return 1;  | 
881  |  |     /* Get and check parameters */  | 
882  | 0  |     if (!ossl_rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))  | 
883  | 0  |         return 0;  | 
884  |  |  | 
885  |  |     /* See if minimum salt length exceeds maximum possible */  | 
886  | 0  |     md_size = EVP_MD_get_size(md);  | 
887  | 0  |     if (md_size <= 0) { | 
888  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);  | 
889  | 0  |         return 0;  | 
890  | 0  |     }  | 
891  | 0  |     max_saltlen = RSA_size(rsa) - md_size;  | 
892  | 0  |     if ((RSA_bits(rsa) & 0x7) == 1)  | 
893  | 0  |         max_saltlen--;  | 
894  | 0  |     if (min_saltlen > max_saltlen) { | 
895  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);  | 
896  | 0  |         return 0;  | 
897  | 0  |     }  | 
898  |  |  | 
899  | 0  |     rctx->min_saltlen = min_saltlen;  | 
900  |  |  | 
901  |  |     /*  | 
902  |  |      * Set PSS restrictions as defaults: we can then block any attempt to  | 
903  |  |      * use invalid values in pkey_rsa_ctrl  | 
904  |  |      */  | 
905  |  | 
  | 
906  | 0  |     rctx->md = md;  | 
907  | 0  |     rctx->mgf1md = mgf1md;  | 
908  | 0  |     rctx->saltlen = min_saltlen;  | 
909  |  | 
  | 
910  | 0  |     return 1;  | 
911  | 0  | }  | 
912  |  |  | 
913  |  | static const EVP_PKEY_METHOD rsa_pss_pkey_meth = { | 
914  |  |     EVP_PKEY_RSA_PSS,  | 
915  |  |     EVP_PKEY_FLAG_AUTOARGLEN,  | 
916  |  |     pkey_rsa_init,  | 
917  |  |     pkey_rsa_copy,  | 
918  |  |     pkey_rsa_cleanup,  | 
919  |  |  | 
920  |  |     0, 0,  | 
921  |  |  | 
922  |  |     0,  | 
923  |  |     pkey_rsa_keygen,  | 
924  |  |  | 
925  |  |     pkey_pss_init,  | 
926  |  |     pkey_rsa_sign,  | 
927  |  |  | 
928  |  |     pkey_pss_init,  | 
929  |  |     pkey_rsa_verify,  | 
930  |  |  | 
931  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  | 
932  |  |  | 
933  |  |     pkey_rsa_ctrl,  | 
934  |  |     pkey_rsa_ctrl_str  | 
935  |  | };  | 
936  |  |  | 
937  |  | const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void)  | 
938  | 0  | { | 
939  | 0  |     return &rsa_pss_pkey_meth;  | 
940  | 0  | }  |