[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 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
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 | #include <time.h>
|
---|
| 12 |
|
---|
| 13 | #include "internal/cryptlib.h"
|
---|
| 14 |
|
---|
| 15 | #ifndef NO_SYS_TYPES_H
|
---|
| 16 | # include <sys/types.h>
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 | #include <openssl/bn.h>
|
---|
| 20 | #include <openssl/x509.h>
|
---|
| 21 | #include <openssl/objects.h>
|
---|
| 22 | #include <openssl/buffer.h>
|
---|
| 23 | #include <openssl/evp.h>
|
---|
| 24 | #include "internal/asn1_int.h"
|
---|
| 25 | #include "internal/evp_int.h"
|
---|
| 26 |
|
---|
| 27 | #ifndef NO_ASN1_OLD
|
---|
| 28 |
|
---|
| 29 | int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
|
---|
| 30 | char *data, EVP_PKEY *pkey)
|
---|
| 31 | {
|
---|
| 32 | EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
---|
| 33 | const EVP_MD *type;
|
---|
| 34 | unsigned char *p, *buf_in = NULL;
|
---|
| 35 | int ret = -1, i, inl;
|
---|
| 36 |
|
---|
| 37 | if (ctx == NULL) {
|
---|
| 38 | ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE);
|
---|
| 39 | goto err;
|
---|
| 40 | }
|
---|
| 41 | i = OBJ_obj2nid(a->algorithm);
|
---|
| 42 | type = EVP_get_digestbyname(OBJ_nid2sn(i));
|
---|
| 43 | if (type == NULL) {
|
---|
| 44 | ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
|
---|
| 45 | goto err;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
|
---|
| 49 | ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
|
---|
| 50 | goto err;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | inl = i2d(data, NULL);
|
---|
| 54 | buf_in = OPENSSL_malloc((unsigned int)inl);
|
---|
| 55 | if (buf_in == NULL) {
|
---|
| 56 | ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE);
|
---|
| 57 | goto err;
|
---|
| 58 | }
|
---|
| 59 | p = buf_in;
|
---|
| 60 |
|
---|
| 61 | i2d(data, &p);
|
---|
| 62 | ret = EVP_VerifyInit_ex(ctx, type, NULL)
|
---|
| 63 | && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl);
|
---|
| 64 |
|
---|
| 65 | OPENSSL_clear_free(buf_in, (unsigned int)inl);
|
---|
| 66 |
|
---|
| 67 | if (!ret) {
|
---|
| 68 | ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
|
---|
| 69 | goto err;
|
---|
| 70 | }
|
---|
| 71 | ret = -1;
|
---|
| 72 |
|
---|
| 73 | if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data,
|
---|
| 74 | (unsigned int)signature->length, pkey) <= 0) {
|
---|
| 75 | ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
|
---|
| 76 | ret = 0;
|
---|
| 77 | goto err;
|
---|
| 78 | }
|
---|
| 79 | ret = 1;
|
---|
| 80 | err:
|
---|
| 81 | EVP_MD_CTX_free(ctx);
|
---|
| 82 | return (ret);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | #endif
|
---|
| 86 |
|
---|
| 87 | int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a,
|
---|
| 88 | ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey)
|
---|
| 89 | {
|
---|
| 90 | EVP_MD_CTX *ctx = NULL;
|
---|
| 91 | unsigned char *buf_in = NULL;
|
---|
| 92 | int ret = -1, inl;
|
---|
| 93 |
|
---|
| 94 | int mdnid, pknid;
|
---|
| 95 |
|
---|
| 96 | if (!pkey) {
|
---|
| 97 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 98 | return -1;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
|
---|
| 102 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
|
---|
| 103 | return -1;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | ctx = EVP_MD_CTX_new();
|
---|
| 107 | if (ctx == NULL) {
|
---|
| 108 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
|
---|
| 109 | goto err;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /* Convert signature OID into digest and public key OIDs */
|
---|
| 113 | if (!OBJ_find_sigid_algs(OBJ_obj2nid(a->algorithm), &mdnid, &pknid)) {
|
---|
| 114 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
|
---|
| 115 | goto err;
|
---|
| 116 | }
|
---|
| 117 | if (mdnid == NID_undef) {
|
---|
| 118 | if (!pkey->ameth || !pkey->ameth->item_verify) {
|
---|
| 119 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
|
---|
| 120 | ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
|
---|
| 121 | goto err;
|
---|
| 122 | }
|
---|
| 123 | ret = pkey->ameth->item_verify(ctx, it, asn, a, signature, pkey);
|
---|
| 124 | /*
|
---|
| 125 | * Return value of 2 means carry on, anything else means we exit
|
---|
| 126 | * straight away: either a fatal error of the underlying verification
|
---|
| 127 | * routine handles all verification.
|
---|
| 128 | */
|
---|
| 129 | if (ret != 2)
|
---|
| 130 | goto err;
|
---|
| 131 | ret = -1;
|
---|
| 132 | } else {
|
---|
| 133 | const EVP_MD *type;
|
---|
| 134 | type = EVP_get_digestbynid(mdnid);
|
---|
| 135 | if (type == NULL) {
|
---|
| 136 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
|
---|
| 137 | ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
|
---|
| 138 | goto err;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /* Check public key OID matches public key type */
|
---|
| 142 | if (EVP_PKEY_type(pknid) != pkey->ameth->pkey_id) {
|
---|
| 143 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
|
---|
| 144 | goto err;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
|
---|
| 148 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
|
---|
| 149 | ret = 0;
|
---|
| 150 | goto err;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | inl = ASN1_item_i2d(asn, &buf_in, it);
|
---|
| 156 |
|
---|
| 157 | if (buf_in == NULL) {
|
---|
| 158 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
|
---|
| 159 | goto err;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | ret = EVP_DigestVerifyUpdate(ctx, buf_in, inl);
|
---|
| 163 |
|
---|
| 164 | OPENSSL_clear_free(buf_in, (unsigned int)inl);
|
---|
| 165 |
|
---|
| 166 | if (!ret) {
|
---|
| 167 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
|
---|
| 168 | goto err;
|
---|
| 169 | }
|
---|
| 170 | ret = -1;
|
---|
| 171 |
|
---|
| 172 | if (EVP_DigestVerifyFinal(ctx, signature->data,
|
---|
| 173 | (size_t)signature->length) <= 0) {
|
---|
| 174 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
|
---|
| 175 | ret = 0;
|
---|
| 176 | goto err;
|
---|
| 177 | }
|
---|
| 178 | ret = 1;
|
---|
| 179 | err:
|
---|
| 180 | EVP_MD_CTX_free(ctx);
|
---|
| 181 | return (ret);
|
---|
| 182 | }
|
---|