2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <sys/types.h>
14 #include "internal/cryptlib.h"
16 #include <openssl/bn.h>
17 #include <openssl/x509.h>
18 #include <openssl/objects.h>
19 #include <openssl/buffer.h>
20 #include <openssl/evp.h>
21 #include "internal/asn1_int.h"
22 #include "internal/evp_int.h"
26 int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
27 char *data, EVP_PKEY *pkey)
29 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
31 unsigned char *p, *buf_in = NULL;
35 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE);
38 i = OBJ_obj2nid(a->algorithm);
39 type = EVP_get_digestbyname(OBJ_nid2sn(i));
41 ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
45 if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
46 ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
50 inl = i2d(data, NULL);
51 buf_in = OPENSSL_malloc((unsigned int)inl);
53 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE);
59 ret = EVP_VerifyInit_ex(ctx, type, NULL)
60 && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl);
62 OPENSSL_clear_free(buf_in, (unsigned int)inl);
65 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
70 if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data,
71 (unsigned int)signature->length, pkey) <= 0) {
72 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
84 int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a,
85 ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey)
87 EVP_MD_CTX *ctx = NULL;
88 unsigned char *buf_in = NULL;
89 int ret = -1, inl = 0;
94 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_PASSED_NULL_PARAMETER);
98 if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
99 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
103 ctx = EVP_MD_CTX_new();
105 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
109 /* Convert signature OID into digest and public key OIDs */
110 if (!OBJ_find_sigid_algs(OBJ_obj2nid(a->algorithm), &mdnid, &pknid)) {
111 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
114 if (mdnid == NID_undef) {
115 if (!pkey->ameth || !pkey->ameth->item_verify) {
116 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
117 ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
120 ret = pkey->ameth->item_verify(ctx, it, asn, a, signature, pkey);
122 * Return value of 2 means carry on, anything else means we exit
123 * straight away: either a fatal error of the underlying verification
124 * routine handles all verification.
131 type = EVP_get_digestbynid(mdnid);
133 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
134 ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
138 /* Check public key OID matches public key type */
139 if (EVP_PKEY_type(pknid) != pkey->ameth->pkey_id) {
140 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
144 if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
145 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
152 inl = ASN1_item_i2d(asn, &buf_in, it);
154 if (buf_in == NULL) {
155 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
159 ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length,
162 ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
167 OPENSSL_clear_free(buf_in, (unsigned int)inl);
168 EVP_MD_CTX_free(ctx);