| 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/digest.h> |
| 16 |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/blake2.h> |
| 20 | #include <openssl/bytestring.h> |
| 21 | #include <openssl/md4.h> |
| 22 | #include <openssl/md5.h> |
| 23 | #include <openssl/nid.h> |
| 24 | #include <openssl/obj.h> |
| 25 |
| 26 | #include "../asn1/internal.h" |
| 27 | #include "../fipsmodule/digest/internal.h" |
| 28 | #include "../internal.h" |
| 29 |
| 30 |
| 31 | struct nid_to_digest { |
| 32 | int nid; |
| 33 | const EVP_MD *(*md_func)(void); |
| 34 | const char *short_name; |
| 35 | const char *long_name; |
| 36 | }; |
| 37 |
| 38 | static const struct nid_to_digest nid_to_digest_mapping[] = { |
| 39 | {NID_md4, EVP_md4, SN_md4, LN_md4}, |
| 40 | {NID_md5, EVP_md5, SN_md5, LN_md5}, |
| 41 | {NID_sha1, EVP_sha1, SN_sha1, LN_sha1}, |
| 42 | {NID_sha224, EVP_sha224, SN_sha224, LN_sha224}, |
| 43 | {NID_sha256, EVP_sha256, SN_sha256, LN_sha256}, |
| 44 | {NID_sha384, EVP_sha384, SN_sha384, LN_sha384}, |
| 45 | {NID_sha512, EVP_sha512, SN_sha512, LN_sha512}, |
| 46 | {NID_sha512_256, EVP_sha512_256, SN_sha512_256, LN_sha512_256}, |
| 47 | {NID_md5_sha1, EVP_md5_sha1, SN_md5_sha1, LN_md5_sha1}, |
| 48 | // As a remnant of signing |EVP_MD|s, OpenSSL returned the corresponding |
| 49 | // hash function when given a signature OID. To avoid unintended lax parsing |
| 50 | // of hash OIDs, this is no longer supported for lookup by OID or NID. |
| 51 | // Node.js, however, exposes |EVP_get_digestbyname|'s full behavior to |
| 52 | // consumers so we retain it there. |
| 53 | {NID_undef, EVP_sha1, SN_dsaWithSHA, LN_dsaWithSHA}, |
| 54 | {NID_undef, EVP_sha1, SN_dsaWithSHA1, LN_dsaWithSHA1}, |
| 55 | {NID_undef, EVP_sha1, SN_ecdsa_with_SHA1, NULL}, |
| 56 | {NID_undef, EVP_md5, SN_md5WithRSAEncryption, LN_md5WithRSAEncryption}, |
| 57 | {NID_undef, EVP_sha1, SN_sha1WithRSAEncryption, LN_sha1WithRSAEncryption}, |
| 58 | {NID_undef, EVP_sha224, SN_sha224WithRSAEncryption, |
| 59 | LN_sha224WithRSAEncryption}, |
| 60 | {NID_undef, EVP_sha256, SN_sha256WithRSAEncryption, |
| 61 | LN_sha256WithRSAEncryption}, |
| 62 | {NID_undef, EVP_sha384, SN_sha384WithRSAEncryption, |
| 63 | LN_sha384WithRSAEncryption}, |
| 64 | {NID_undef, EVP_sha512, SN_sha512WithRSAEncryption, |
| 65 | LN_sha512WithRSAEncryption}, |
| 66 | }; |
| 67 |
| 68 | const EVP_MD *EVP_get_digestbynid(int nid) { |
| 69 | if (nid == NID_undef) { |
| 70 | // Skip the |NID_undef| entries in |nid_to_digest_mapping|. |
| 71 | return NULL; |
| 72 | } |
| 73 |
| 74 | for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(nid_to_digest_mapping); i++) { |
| 75 | if (nid_to_digest_mapping[i].nid == nid) { |
| 76 | return nid_to_digest_mapping[i].md_func(); |
| 77 | } |
| 78 | } |
| 79 |
| 80 | return NULL; |
| 81 | } |
| 82 |
| 83 | static const struct { |
| 84 | uint8_t oid[9]; |
| 85 | uint8_t oid_len; |
| 86 | int nid; |
| 87 | } kMDOIDs[] = { |
| 88 | // 1.2.840.113549.2.4 |
| 89 | {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x04}, 8, NID_md4}, |
| 90 | // 1.2.840.113549.2.5 |
| 91 | {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05}, 8, NID_md5}, |
| 92 | // 1.3.14.3.2.26 |
| 93 | {{0x2b, 0x0e, 0x03, 0x02, 0x1a}, 5, NID_sha1}, |
| 94 | // 2.16.840.1.101.3.4.2.1 |
| 95 | {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}, 9, NID_sha256}, |
| 96 | // 2.16.840.1.101.3.4.2.2 |
| 97 | {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02}, 9, NID_sha384}, |
| 98 | // 2.16.840.1.101.3.4.2.3 |
| 99 | {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}, 9, NID_sha512}, |
| 100 | // 2.16.840.1.101.3.4.2.4 |
| 101 | {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}, 9, NID_sha224}, |
| 102 | }; |
| 103 |
| 104 | static const EVP_MD *cbs_to_md(const CBS *cbs) { |
| 105 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMDOIDs); i++) { |
| 106 | if (CBS_len(cbs) == kMDOIDs[i].oid_len && |
| 107 | OPENSSL_memcmp(CBS_data(cbs), kMDOIDs[i].oid, kMDOIDs[i].oid_len) == |
| 108 | 0) { |
| 109 | return EVP_get_digestbynid(kMDOIDs[i].nid); |
| 110 | } |
| 111 | } |
| 112 |
| 113 | return NULL; |
| 114 | } |
| 115 |
| 116 | const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj) { |
| 117 | // Handle objects with no corresponding OID. Note we don't use |OBJ_obj2nid| |
| 118 | // here to avoid pulling in the OID table. |
| 119 | if (obj->nid != NID_undef) { |
| 120 | return EVP_get_digestbynid(obj->nid); |
| 121 | } |
| 122 |
| 123 | CBS cbs; |
| 124 | CBS_init(&cbs, OBJ_get0_data(obj), OBJ_length(obj)); |
| 125 | return cbs_to_md(&cbs); |
| 126 | } |
| 127 |
| 128 | const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs) { |
| 129 | CBS algorithm, oid; |
| 130 | if (!CBS_get_asn1(cbs, &algorithm, CBS_ASN1_SEQUENCE) || |
| 131 | !CBS_get_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) { |
| 132 | OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_DECODE_ERROR); |
| 133 | return NULL; |
| 134 | } |
| 135 |
| 136 | const EVP_MD *ret = cbs_to_md(&oid); |
| 137 | if (ret == NULL) { |
| 138 | OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_UNKNOWN_HASH); |
| 139 | return NULL; |
| 140 | } |
| 141 |
| 142 | // The parameters, if present, must be NULL. Historically, whether the NULL |
| 143 | // was included or omitted was not well-specified. When parsing an |
| 144 | // AlgorithmIdentifier, we allow both. (Note this code is not used when |
| 145 | // verifying RSASSA-PKCS1-v1_5 signatures.) |
| 146 | if (CBS_len(&algorithm) > 0) { |
| 147 | CBS param; |
| 148 | if (!CBS_get_asn1(&algorithm, ¶m, CBS_ASN1_NULL) || |
| 149 | CBS_len(¶m) != 0 || // |
| 150 | CBS_len(&algorithm) != 0) { |
| 151 | OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_DECODE_ERROR); |
| 152 | return NULL; |
| 153 | } |
| 154 | } |
| 155 |
| 156 | return ret; |
| 157 | } |
| 158 |
| 159 | int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) { |
| 160 | CBB algorithm, oid, null; |
| 161 | if (!CBB_add_asn1(cbb, &algorithm, CBS_ASN1_SEQUENCE) || |
| 162 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) { |
| 163 | return 0; |
| 164 | } |
| 165 |
| 166 | int found = 0; |
| 167 | int nid = EVP_MD_type(md); |
| 168 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMDOIDs); i++) { |
| 169 | if (nid == kMDOIDs[i].nid) { |
| 170 | if (!CBB_add_bytes(&oid, kMDOIDs[i].oid, kMDOIDs[i].oid_len)) { |
| 171 | return 0; |
| 172 | } |
| 173 | found = 1; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 |
| 178 | if (!found) { |
| 179 | OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_UNKNOWN_HASH); |
| 180 | return 0; |
| 181 | } |
| 182 |
| 183 | // TODO(crbug.com/boringssl/710): Is this correct? See RFC 4055, section 2.1. |
| 184 | if (!CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) || // |
| 185 | !CBB_flush(cbb)) { |
| 186 | return 0; |
| 187 | } |
| 188 |
| 189 | return 1; |
| 190 | } |
| 191 |
| 192 | const EVP_MD *EVP_get_digestbyname(const char *name) { |
| 193 | for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(nid_to_digest_mapping); i++) { |
| 194 | const char *short_name = nid_to_digest_mapping[i].short_name; |
| 195 | const char *long_name = nid_to_digest_mapping[i].long_name; |
| 196 | if ((short_name && strcmp(short_name, name) == 0) || |
| 197 | (long_name && strcmp(long_name, name) == 0)) { |
| 198 | return nid_to_digest_mapping[i].md_func(); |
| 199 | } |
| 200 | } |
| 201 |
| 202 | return NULL; |
| 203 | } |
| 204 |
| 205 | static void blake2b256_init(EVP_MD_CTX *ctx) { |
| 206 | BLAKE2B256_Init(reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data)); |
| 207 | } |
| 208 |
| 209 | static void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 210 | BLAKE2B256_Update(reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data), data, len); |
| 211 | } |
| 212 |
| 213 | static void blake2b256_final(EVP_MD_CTX *ctx, uint8_t *md) { |
| 214 | BLAKE2B256_Final(md, reinterpret_cast<BLAKE2B_CTX *>(ctx->md_data)); |
| 215 | } |
| 216 |
| 217 | static const EVP_MD evp_md_blake2b256 = { |
| 218 | NID_undef, BLAKE2B256_DIGEST_LENGTH, 0, |
| 219 | blake2b256_init, blake2b256_update, blake2b256_final, |
| 220 | BLAKE2B_CBLOCK, sizeof(BLAKE2B_CTX), |
| 221 | }; |
| 222 |
| 223 | const EVP_MD *EVP_blake2b256(void) { return &evp_md_blake2b256; } |
| 224 |
| 225 |
| 226 | static void md4_init(EVP_MD_CTX *ctx) { |
| 227 | BSSL_CHECK(MD4_Init(reinterpret_cast<MD4_CTX *>(ctx->md_data))); |
| 228 | } |
| 229 |
| 230 | static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 231 | BSSL_CHECK( |
| 232 | MD4_Update(reinterpret_cast<MD4_CTX *>(ctx->md_data), data, count)); |
| 233 | } |
| 234 |
| 235 | static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) { |
| 236 | BSSL_CHECK(MD4_Final(out, reinterpret_cast<MD4_CTX *>(ctx->md_data))); |
| 237 | } |
| 238 |
| 239 | static const EVP_MD evp_md_md4 = { |
| 240 | NID_md4, // |
| 241 | MD4_DIGEST_LENGTH, // |
| 242 | 0, |
| 243 | md4_init, |
| 244 | md4_update, |
| 245 | md4_final, |
| 246 | 64, |
| 247 | sizeof(MD4_CTX), |
| 248 | }; |
| 249 |
| 250 | const EVP_MD *EVP_md4(void) { return &evp_md_md4; } |
| 251 |
| 252 | static void md5_init(EVP_MD_CTX *ctx) { |
| 253 | BSSL_CHECK(MD5_Init(reinterpret_cast<MD5_CTX *>(ctx->md_data))); |
| 254 | } |
| 255 |
| 256 | static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 257 | BSSL_CHECK( |
| 258 | MD5_Update(reinterpret_cast<MD5_CTX *>(ctx->md_data), data, count)); |
| 259 | } |
| 260 |
| 261 | static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) { |
| 262 | BSSL_CHECK(MD5_Final(out, reinterpret_cast<MD5_CTX *>(ctx->md_data))); |
| 263 | } |
| 264 |
| 265 | static const EVP_MD evp_md_md5 = { |
| 266 | NID_md5, MD5_DIGEST_LENGTH, 0, md5_init, |
| 267 | md5_update, md5_final, 64, sizeof(MD5_CTX), |
| 268 | }; |
| 269 |
| 270 | const EVP_MD *EVP_md5(void) { return &evp_md_md5; } |
| 271 |
| 272 | typedef struct { |
| 273 | MD5_CTX md5; |
| 274 | SHA_CTX sha1; |
| 275 | } MD5_SHA1_CTX; |
| 276 |
| 277 | static void md5_sha1_init(EVP_MD_CTX *md_ctx) { |
| 278 | MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data); |
| 279 | BSSL_CHECK(MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1)); |
| 280 | } |
| 281 |
| 282 | static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data, |
| 283 | size_t count) { |
| 284 | MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data); |
| 285 | BSSL_CHECK(MD5_Update(&ctx->md5, data, count) && |
| 286 | SHA1_Update(&ctx->sha1, data, count)); |
| 287 | } |
| 288 |
| 289 | static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) { |
| 290 | MD5_SHA1_CTX *ctx = reinterpret_cast<MD5_SHA1_CTX *>(md_ctx->md_data); |
| 291 | BSSL_CHECK(MD5_Final(out, &ctx->md5) && |
| 292 | SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1)); |
| 293 | } |
| 294 |
| 295 | const EVP_MD evp_md_md5_sha1 = { |
| 296 | NID_md5_sha1, |
| 297 | MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, |
| 298 | 0, |
| 299 | md5_sha1_init, |
| 300 | md5_sha1_update, |
| 301 | md5_sha1_final, |
| 302 | 64, |
| 303 | sizeof(MD5_SHA1_CTX), |
| 304 | }; |
| 305 |
| 306 | const EVP_MD *EVP_md5_sha1(void) { return &evp_md_md5_sha1; } |
| 307 |