| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/x509.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 |
| 20 | #include <openssl/asn1.h> |
| 21 | #include <openssl/asn1t.h> |
| 22 | #include <openssl/bio.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/evp.h> |
| 25 | #include <openssl/obj.h> |
| 26 |
| 27 | #include "internal.h" |
| 28 |
| 29 |
| 30 | static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 31 | void *exarg) { |
| 32 | if (operation == ASN1_OP_FREE_PRE) { |
| 33 | RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval; |
| 34 | X509_ALGOR_free(pss->maskHash); |
| 35 | } |
| 36 | return 1; |
| 37 | } |
| 38 |
| 39 | ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = { |
| 40 | ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR, 0), |
| 41 | ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR, 1), |
| 42 | ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER, 2), |
| 43 | ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER, 3), |
| 44 | } ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS) |
| 45 |
| 46 | IMPLEMENT_ASN1_FUNCTIONS_const(RSA_PSS_PARAMS) |
| 47 |
| 48 |
| 49 | // Given an MGF1 Algorithm ID decode to an Algorithm Identifier |
| 50 | static X509_ALGOR *rsa_mgf1_decode(const X509_ALGOR *alg) { |
| 51 | if (OBJ_obj2nid(alg->algorithm) != NID_mgf1 || alg->parameter == NULL || |
| 52 | alg->parameter->type != V_ASN1_SEQUENCE) { |
| 53 | return NULL; |
| 54 | } |
| 55 |
| 56 | const uint8_t *p = alg->parameter->value.sequence->data; |
| 57 | int plen = alg->parameter->value.sequence->length; |
| 58 | return d2i_X509_ALGOR(NULL, &p, plen); |
| 59 | } |
| 60 |
| 61 | static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg) { |
| 62 | if (alg->parameter == NULL || alg->parameter->type != V_ASN1_SEQUENCE) { |
| 63 | return NULL; |
| 64 | } |
| 65 |
| 66 | const uint8_t *p = alg->parameter->value.sequence->data; |
| 67 | int plen = alg->parameter->value.sequence->length; |
| 68 | return d2i_RSA_PSS_PARAMS(NULL, &p, plen); |
| 69 | } |
| 70 |
| 71 | static int is_allowed_pss_md(const EVP_MD *md) { |
| 72 | int md_type = EVP_MD_type(md); |
| 73 | return md_type == NID_sha256 || md_type == NID_sha384 || |
| 74 | md_type == NID_sha512; |
| 75 | } |
| 76 |
| 77 | // rsa_md_to_algor sets |*palg| to an |X509_ALGOR| describing the digest |md|, |
| 78 | // which must be an allowed PSS digest. |
| 79 | static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md) { |
| 80 | // SHA-1 should be omitted (DEFAULT), but we do not allow SHA-1. |
| 81 | assert(is_allowed_pss_md(md)); |
| 82 | *palg = X509_ALGOR_new(); |
| 83 | if (*palg == NULL) { |
| 84 | return 0; |
| 85 | } |
| 86 | if (!X509_ALGOR_set_md(*palg, md)) { |
| 87 | X509_ALGOR_free(*palg); |
| 88 | *palg = NULL; |
| 89 | return 0; |
| 90 | } |
| 91 | return 1; |
| 92 | } |
| 93 |
| 94 | // rsa_md_to_mgf1 sets |*palg| to an |X509_ALGOR| describing MGF-1 with the |
| 95 | // digest |mgf1md|, which must be an allowed PSS digest. |
| 96 | static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) { |
| 97 | // SHA-1 should be omitted (DEFAULT), but we do not allow SHA-1. |
| 98 | assert(is_allowed_pss_md(mgf1md)); |
| 99 | X509_ALGOR *algtmp = NULL; |
| 100 | ASN1_STRING *stmp = NULL; |
| 101 | // need to embed algorithm ID inside another |
| 102 | if (!rsa_md_to_algor(&algtmp, mgf1md) || |
| 103 | !ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) { |
| 104 | goto err; |
| 105 | } |
| 106 | *palg = X509_ALGOR_new(); |
| 107 | if (!*palg) { |
| 108 | goto err; |
| 109 | } |
| 110 | if (!X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp)) { |
| 111 | goto err; |
| 112 | } |
| 113 | stmp = NULL; |
| 114 |
| 115 | err: |
| 116 | ASN1_STRING_free(stmp); |
| 117 | X509_ALGOR_free(algtmp); |
| 118 | if (*palg) { |
| 119 | return 1; |
| 120 | } |
| 121 |
| 122 | return 0; |
| 123 | } |
| 124 |
| 125 | static const EVP_MD *rsa_algor_to_md(const X509_ALGOR *alg) { |
| 126 | if (!alg) { |
| 127 | // If omitted, PSS defaults to SHA-1, which we do not allow. |
| 128 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 129 | return NULL; |
| 130 | } |
| 131 | const EVP_MD *md = EVP_get_digestbyobj(alg->algorithm); |
| 132 | if (md == NULL || !is_allowed_pss_md(md)) { |
| 133 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 134 | return NULL; |
| 135 | } |
| 136 | return md; |
| 137 | } |
| 138 |
| 139 | static const EVP_MD *rsa_mgf1_to_md(const X509_ALGOR *alg) { |
| 140 | if (!alg) { |
| 141 | // If omitted, PSS defaults to MGF-1 with SHA-1, which we do not allow. |
| 142 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 143 | return NULL; |
| 144 | } |
| 145 | // Check mask and lookup mask hash algorithm. |
| 146 | X509_ALGOR *maskHash = rsa_mgf1_decode(alg); |
| 147 | if (maskHash == NULL) { |
| 148 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 149 | return NULL; |
| 150 | } |
| 151 | const EVP_MD *ret = rsa_algor_to_md(maskHash); |
| 152 | X509_ALGOR_free(maskHash); |
| 153 | return ret; |
| 154 | } |
| 155 |
| 156 | int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) { |
| 157 | const EVP_MD *sigmd, *mgf1md; |
| 158 | int saltlen; |
| 159 | if (!EVP_PKEY_CTX_get_signature_md(ctx->pctx, &sigmd) || |
| 160 | !EVP_PKEY_CTX_get_rsa_mgf1_md(ctx->pctx, &mgf1md) || |
| 161 | !EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx->pctx, &saltlen)) { |
| 162 | return 0; |
| 163 | } |
| 164 |
| 165 | if (sigmd != mgf1md || !is_allowed_pss_md(sigmd)) { |
| 166 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 167 | return 0; |
| 168 | } |
| 169 | int md_len = (int)EVP_MD_size(sigmd); |
| 170 | if (saltlen == -1) { |
| 171 | saltlen = md_len; |
| 172 | } else if (saltlen != md_len) { |
| 173 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 174 | return 0; |
| 175 | } |
| 176 |
| 177 | int ret = 0; |
| 178 | ASN1_STRING *os = NULL; |
| 179 | RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new(); |
| 180 | if (!pss) { |
| 181 | goto err; |
| 182 | } |
| 183 |
| 184 | // The DEFAULT value is 20, but this does not match any supported digest. |
| 185 | assert(saltlen != 20); |
| 186 | pss->saltLength = ASN1_INTEGER_new(); |
| 187 | if (!pss->saltLength || // |
| 188 | !ASN1_INTEGER_set_int64(pss->saltLength, saltlen)) { |
| 189 | goto err; |
| 190 | } |
| 191 |
| 192 | if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd) || |
| 193 | !rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md)) { |
| 194 | goto err; |
| 195 | } |
| 196 |
| 197 | // Finally create string with pss parameter encoding. |
| 198 | if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) { |
| 199 | goto err; |
| 200 | } |
| 201 |
| 202 | if (!X509_ALGOR_set0(algor, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, |
| 203 | os)) { |
| 204 | goto err; |
| 205 | } |
| 206 | os = NULL; |
| 207 | ret = 1; |
| 208 |
| 209 | err: |
| 210 | RSA_PSS_PARAMS_free(pss); |
| 211 | ASN1_STRING_free(os); |
| 212 | return ret; |
| 213 | } |
| 214 |
| 215 | int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, |
| 216 | EVP_PKEY *pkey) { |
| 217 | assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss); |
| 218 |
| 219 | // Decode PSS parameters |
| 220 | int ret = 0; |
| 221 | RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg); |
| 222 |
| 223 | { |
| 224 | if (pss == NULL) { |
| 225 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 226 | goto err; |
| 227 | } |
| 228 |
| 229 | const EVP_MD *mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm); |
| 230 | const EVP_MD *md = rsa_algor_to_md(pss->hashAlgorithm); |
| 231 | if (mgf1md == NULL || md == NULL) { |
| 232 | goto err; |
| 233 | } |
| 234 |
| 235 | // We require the MGF-1 and signing hashes to match. |
| 236 | if (mgf1md != md) { |
| 237 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 238 | goto err; |
| 239 | } |
| 240 |
| 241 | // We require the salt length be the hash length. The DEFAULT value is 20, |
| 242 | // but this does not match any supported salt length. |
| 243 | uint64_t salt_len = 0; |
| 244 | if (pss->saltLength == NULL || |
| 245 | !ASN1_INTEGER_get_uint64(&salt_len, pss->saltLength) || |
| 246 | salt_len != EVP_MD_size(md)) { |
| 247 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 248 | goto err; |
| 249 | } |
| 250 | assert(salt_len <= INT_MAX); |
| 251 |
| 252 | // The trailer field must be 1 (0xbc). This value is DEFAULT, so the |
| 253 | // structure is required to omit it in DER. Although a syntax error, we also |
| 254 | // tolerate an explicitly-encoded value. See the certificates in |
| 255 | // cl/362617931. |
| 256 | if (pss->trailerField != NULL && ASN1_INTEGER_get(pss->trailerField) != 1) { |
| 257 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS); |
| 258 | goto err; |
| 259 | } |
| 260 |
| 261 | EVP_PKEY_CTX *pctx; |
| 262 | if (!EVP_DigestVerifyInit(ctx, &pctx, md, NULL, pkey) || |
| 263 | !EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 264 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, (int)salt_len) || |
| 265 | !EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf1md)) { |
| 266 | goto err; |
| 267 | } |
| 268 |
| 269 | ret = 1; |
| 270 | } |
| 271 |
| 272 | err: |
| 273 | RSA_PSS_PARAMS_free(pss); |
| 274 | return ret; |
| 275 | } |
| 276 |
| 277 | int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent, |
| 278 | ASN1_PCTX *pctx) { |
| 279 | assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss); |
| 280 |
| 281 | int rv = 0; |
| 282 | X509_ALGOR *maskHash = NULL; |
| 283 | RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg); |
| 284 | if (!pss) { |
| 285 | if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0) { |
| 286 | goto err; |
| 287 | } |
| 288 | rv = 1; |
| 289 | goto err; |
| 290 | } |
| 291 |
| 292 | if (BIO_puts(bp, "\n") <= 0 || // |
| 293 | !BIO_indent(bp, indent, 128) || // |
| 294 | BIO_puts(bp, "Hash Algorithm: ") <= 0) { |
| 295 | goto err; |
| 296 | } |
| 297 |
| 298 | if (pss->hashAlgorithm) { |
| 299 | if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0) { |
| 300 | goto err; |
| 301 | } |
| 302 | } else if (BIO_puts(bp, "sha1 (default)") <= 0) { |
| 303 | goto err; |
| 304 | } |
| 305 |
| 306 | if (BIO_puts(bp, "\n") <= 0 || // |
| 307 | !BIO_indent(bp, indent, 128) || // |
| 308 | BIO_puts(bp, "Mask Algorithm: ") <= 0) { |
| 309 | goto err; |
| 310 | } |
| 311 |
| 312 | if (pss->maskGenAlgorithm) { |
| 313 | maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm); |
| 314 | if (maskHash == NULL) { |
| 315 | if (BIO_puts(bp, "INVALID") <= 0) { |
| 316 | goto err; |
| 317 | } |
| 318 | } else { |
| 319 | if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0 || |
| 320 | BIO_puts(bp, " with ") <= 0 || |
| 321 | i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) { |
| 322 | goto err; |
| 323 | } |
| 324 | } |
| 325 | } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) { |
| 326 | goto err; |
| 327 | } |
| 328 | BIO_puts(bp, "\n"); |
| 329 |
| 330 | if (!BIO_indent(bp, indent, 128) || // |
| 331 | BIO_puts(bp, "Salt Length: 0x") <= 0) { |
| 332 | goto err; |
| 333 | } |
| 334 |
| 335 | if (pss->saltLength) { |
| 336 | if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0) { |
| 337 | goto err; |
| 338 | } |
| 339 | } else if (BIO_puts(bp, "14 (default)") <= 0) { |
| 340 | goto err; |
| 341 | } |
| 342 | BIO_puts(bp, "\n"); |
| 343 |
| 344 | if (!BIO_indent(bp, indent, 128) || // |
| 345 | BIO_puts(bp, "Trailer Field: 0x") <= 0) { |
| 346 | goto err; |
| 347 | } |
| 348 |
| 349 | if (pss->trailerField) { |
| 350 | if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0) { |
| 351 | goto err; |
| 352 | } |
| 353 | } else if (BIO_puts(bp, "BC (default)") <= 0) { |
| 354 | goto err; |
| 355 | } |
| 356 | BIO_puts(bp, "\n"); |
| 357 |
| 358 | rv = 1; |
| 359 |
| 360 | err: |
| 361 | RSA_PSS_PARAMS_free(pss); |
| 362 | X509_ALGOR_free(maskHash); |
| 363 | return rv; |
| 364 | } |
| 365 |