Coverage Report

Created: 2026-10-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/x509/algorithm.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
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
26
using namespace bssl;
27
28
// TODO(crbug.com/42290422): Rewrite this logic to recognize signature
29
// algorithms without pulling in the OID table. We can enumerate every supported
30
// signature algorithm into a small enum and convert them to/from `EVP_PKEY_CTX`
31
// and `X509_ALGOR`.
32
33
namespace {
34
35
// Restrict the digests that are allowed in X509 certificates
36
0
int x509_digest_nid_ok(const int digest_nid) {
37
0
  switch (digest_nid) {
38
0
    case NID_md4:
39
0
    case NID_md5:
40
0
      return 0;
41
0
  }
42
0
  return 1;
43
0
}
44
45
}  // namespace
46
47
0
int bssl::x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
48
0
  EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx);
49
0
  if (pkey == nullptr) {
50
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
51
0
    return 0;
52
0
  }
53
54
0
  if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA ||
55
0
      EVP_PKEY_id(pkey) == EVP_PKEY_RSA_PSS) {
56
0
    int pad_mode;
57
0
    if (!EVP_PKEY_CTX_get_rsa_padding(ctx->pctx, &pad_mode)) {
58
0
      return 0;
59
0
    }
60
    // RSA-PSS has special signature algorithm logic.
61
0
    if (pad_mode == RSA_PKCS1_PSS_PADDING) {
62
0
      return x509_rsa_ctx_to_pss(ctx, algor);
63
0
    }
64
0
  }
65
66
  // Check for signing algorithms with an internal hash.
67
0
  ASN1_OBJECT *algo_obj = nullptr;
68
0
  switch (EVP_PKEY_id(pkey)) {
69
0
    case EVP_PKEY_ED25519:
70
0
      algo_obj = OBJ_nid2obj(NID_ED25519);
71
0
      break;
72
0
    case EVP_PKEY_ML_DSA_44:
73
0
      algo_obj = OBJ_nid2obj(NID_ML_DSA_44);
74
0
      break;
75
0
    case EVP_PKEY_ML_DSA_65:
76
0
      algo_obj = OBJ_nid2obj(NID_ML_DSA_65);
77
0
      break;
78
0
    case EVP_PKEY_ML_DSA_87:
79
0
      algo_obj = OBJ_nid2obj(NID_ML_DSA_87);
80
0
      break;
81
0
  }
82
0
  if (algo_obj != nullptr) {
83
0
    return X509_ALGOR_set0(algor, algo_obj, V_ASN1_UNDEF, nullptr);
84
0
  }
85
86
  // Default behavior: look up the OID for the algorithm/hash pair and encode
87
  // that.
88
0
  const EVP_MD *digest = EVP_MD_CTX_get0_md(ctx);
89
0
  if (digest == nullptr) {
90
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
91
0
    return 0;
92
0
  }
93
94
0
  const int digest_nid = EVP_MD_type(digest);
95
0
  int sign_nid;
96
0
  if (!x509_digest_nid_ok(digest_nid) ||
97
0
      !OBJ_find_sigid_by_algs(&sign_nid, digest_nid, EVP_PKEY_id(pkey))) {
98
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
99
0
    return 0;
100
0
  }
101
102
  // RSA signature algorithms include an explicit NULL parameter. Others omit
103
  // it.
104
0
  int paramtype =
105
0
      (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) ? V_ASN1_NULL : V_ASN1_UNDEF;
106
0
  return X509_ALGOR_set0(algor, OBJ_nid2obj(sign_nid), paramtype, nullptr);
107
0
}
108
109
int bssl::x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
110
0
                                  const EVP_PKEY *pkey) {
111
  // Convert the signature OID into digest and public key OIDs.
112
0
  int sigalg_nid = OBJ_obj2nid(sigalg->algorithm);
113
0
  int digest_nid, pkey_nid;
114
0
  if (!OBJ_find_sigid_algs(sigalg_nid, &digest_nid, &pkey_nid)) {
115
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
116
0
    return 0;
117
0
  }
118
119
  // Check the public key OID matches the public key type.
120
0
  const bool pkey_matches =
121
0
      pkey_nid == EVP_PKEY_id(pkey) ||
122
0
      (sigalg_nid == NID_rsassaPss && EVP_PKEY_id(pkey) == EVP_PKEY_RSA_PSS);
123
0
  if (!pkey_matches) {
124
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
125
0
    return 0;
126
0
  }
127
128
  // Check for permitted digest algorithms
129
0
  if (!x509_digest_nid_ok(digest_nid)) {
130
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
131
0
    return 0;
132
0
  }
133
134
  // NID_undef signals that there are custom parameters to set.
135
0
  if (digest_nid == NID_undef) {
136
0
    if (sigalg_nid == NID_rsassaPss) {
137
0
      return x509_rsa_pss_to_ctx(ctx, sigalg, pkey);
138
0
    }
139
0
    if (sigalg_nid == NID_ED25519 || sigalg_nid == NID_ML_DSA_44 ||
140
0
        sigalg_nid == NID_ML_DSA_65 || sigalg_nid == NID_ML_DSA_87) {
141
      // These algorithms require that parameters be absent (Ed25519: RFC 8410
142
      // section 3, ML-DSA: RFC 9881 section 2).
143
0
      if (sigalg->parameter != nullptr) {
144
0
        OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PARAMETER);
145
0
        return 0;
146
0
      }
147
0
      return EVP_DigestVerifyInit(ctx, nullptr, nullptr, nullptr, pkey);
148
0
    }
149
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
150
0
    return 0;
151
0
  }
152
153
  // The parameter should be an explicit NULL for RSA and omitted for ECDSA. For
154
  // compatibility, we allow either for both algorithms. See b/167375496.
155
  //
156
  // TODO(davidben): Chromium's verifier allows both forms for RSA, but enforces
157
  // ECDSA more strictly. Align with Chromium and add a flag for b/167375496.
158
0
  if (sigalg->parameter != nullptr && sigalg->parameter->type != V_ASN1_NULL) {
159
0
    OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PARAMETER);
160
0
    return 0;
161
0
  }
162
163
  // Otherwise, initialize with the digest from the OID.
164
0
  const EVP_MD *digest = EVP_get_digestbynid(digest_nid);
165
0
  if (digest == nullptr) {
166
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
167
0
    return 0;
168
0
  }
169
170
0
  return EVP_DigestVerifyInit(ctx, nullptr, digest, nullptr, pkey);
171
0
}