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