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