| 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 <openssl/asn1.h> |
| 18 | #include <openssl/digest.h> |
| 19 | #include <openssl/err.h> |
| 20 | #include <openssl/evp.h> |
| 21 | #include <openssl/obj.h> |
| 22 |
| 23 | #include "internal.h" |
| 24 |
| 25 | // Restrict the digests that are allowed in X509 certificates |
| 26 | static int x509_digest_nid_ok(const int digest_nid) { |
| 27 | switch (digest_nid) { |
| 28 | case NID_md4: |
| 29 | case NID_md5: |
| 30 | return 0; |
| 31 | } |
| 32 | return 1; |
| 33 | } |
| 34 |
| 35 | int x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor) { |
| 36 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx); |
| 37 | if (pkey == NULL) { |
| 38 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
| 39 | return 0; |
| 40 | } |
| 41 |
| 42 | if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { |
| 43 | int pad_mode; |
| 44 | if (!EVP_PKEY_CTX_get_rsa_padding(ctx->pctx, &pad_mode)) { |
| 45 | return 0; |
| 46 | } |
| 47 | // RSA-PSS has special signature algorithm logic. |
| 48 | if (pad_mode == RSA_PKCS1_PSS_PADDING) { |
| 49 | return x509_rsa_ctx_to_pss(ctx, algor); |
| 50 | } |
| 51 | } |
| 52 |
| 53 | if (EVP_PKEY_id(pkey) == EVP_PKEY_ED25519) { |
| 54 | return X509_ALGOR_set0(algor, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL); |
| 55 | } |
| 56 |
| 57 | // Default behavior: look up the OID for the algorithm/hash pair and encode |
| 58 | // that. |
| 59 | const EVP_MD *digest = EVP_MD_CTX_get0_md(ctx); |
| 60 | if (digest == NULL) { |
| 61 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
| 62 | return 0; |
| 63 | } |
| 64 |
| 65 | const int digest_nid = EVP_MD_type(digest); |
| 66 | int sign_nid; |
| 67 | if (!x509_digest_nid_ok(digest_nid) || |
| 68 | !OBJ_find_sigid_by_algs(&sign_nid, digest_nid, EVP_PKEY_id(pkey))) { |
| 69 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
| 70 | return 0; |
| 71 | } |
| 72 |
| 73 | // RSA signature algorithms include an explicit NULL parameter. Others omit |
| 74 | // it. |
| 75 | int paramtype = |
| 76 | (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) ? V_ASN1_NULL : V_ASN1_UNDEF; |
| 77 | return X509_ALGOR_set0(algor, OBJ_nid2obj(sign_nid), paramtype, NULL); |
| 78 | } |
| 79 |
| 80 | int x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, |
| 81 | EVP_PKEY *pkey) { |
| 82 | // Convert the signature OID into digest and public key OIDs. |
| 83 | int sigalg_nid = OBJ_obj2nid(sigalg->algorithm); |
| 84 | int digest_nid, pkey_nid; |
| 85 | if (!OBJ_find_sigid_algs(sigalg_nid, &digest_nid, &pkey_nid)) { |
| 86 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
| 87 | return 0; |
| 88 | } |
| 89 |
| 90 | // Check the public key OID matches the public key type. |
| 91 | if (pkey_nid != EVP_PKEY_id(pkey)) { |
| 92 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE); |
| 93 | return 0; |
| 94 | } |
| 95 |
| 96 | // Check for permitted digest algorithms |
| 97 | if (!x509_digest_nid_ok(digest_nid)) { |
| 98 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
| 99 | return 0; |
| 100 | } |
| 101 |
| 102 | // NID_undef signals that there are custom parameters to set. |
| 103 | if (digest_nid == NID_undef) { |
| 104 | if (sigalg_nid == NID_rsassaPss) { |
| 105 | return x509_rsa_pss_to_ctx(ctx, sigalg, pkey); |
| 106 | } |
| 107 | if (sigalg_nid == NID_ED25519) { |
| 108 | if (sigalg->parameter != NULL) { |
| 109 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PARAMETER); |
| 110 | return 0; |
| 111 | } |
| 112 | return EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey); |
| 113 | } |
| 114 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
| 115 | return 0; |
| 116 | } |
| 117 |
| 118 | // The parameter should be an explicit NULL for RSA and omitted for ECDSA. For |
| 119 | // compatibility, we allow either for both algorithms. See b/167375496. |
| 120 | // |
| 121 | // TODO(davidben): Chromium's verifier allows both forms for RSA, but enforces |
| 122 | // ECDSA more strictly. Align with Chromium and add a flag for b/167375496. |
| 123 | if (sigalg->parameter != NULL && sigalg->parameter->type != V_ASN1_NULL) { |
| 124 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PARAMETER); |
| 125 | return 0; |
| 126 | } |
| 127 |
| 128 | // Otherwise, initialize with the digest from the OID. |
| 129 | const EVP_MD *digest = EVP_get_digestbynid(digest_nid); |
| 130 | if (digest == NULL) { |
| 131 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM); |
| 132 | return 0; |
| 133 | } |
| 134 |
| 135 | return EVP_DigestVerifyInit(ctx, NULL, digest, NULL, pkey); |
| 136 | } |
| 137 |