/src/openssl/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 "internal/asn1_int.h" |
22 | | #include "internal/evp_int.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 | 0 |
|
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 | 0 |
|
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 | 0 |
|
50 | 0 | inl = i2d(data, NULL); |
51 | 0 | buf_in = OPENSSL_malloc((unsigned int)inl); |
52 | 0 | if (buf_in == NULL) { |
53 | 0 | ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE); |
54 | 0 | goto err; |
55 | 0 | } |
56 | 0 | p = buf_in; |
57 | 0 |
|
58 | 0 | i2d(data, &p); |
59 | 0 | ret = EVP_VerifyInit_ex(ctx, type, NULL) |
60 | 0 | && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl); |
61 | 0 |
|
62 | 0 | OPENSSL_clear_free(buf_in, (unsigned int)inl); |
63 | 0 |
|
64 | 0 | if (!ret) { |
65 | 0 | ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB); |
66 | 0 | goto err; |
67 | 0 | } |
68 | 0 | ret = -1; |
69 | 0 |
|
70 | 0 | if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data, |
71 | 0 | (unsigned int)signature->length, pkey) <= 0) { |
72 | 0 | ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB); |
73 | 0 | ret = 0; |
74 | 0 | goto err; |
75 | 0 | } |
76 | 0 | ret = 1; |
77 | 0 | err: |
78 | 0 | EVP_MD_CTX_free(ctx); |
79 | 0 | return ret; |
80 | 0 | } |
81 | | |
82 | | #endif |
83 | | |
84 | | int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a, |
85 | | ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey) |
86 | 0 | { |
87 | 0 | EVP_MD_CTX *ctx = NULL; |
88 | 0 | unsigned char *buf_in = NULL; |
89 | 0 | int ret = -1, inl = 0; |
90 | 0 |
|
91 | 0 | int mdnid, pknid; |
92 | 0 |
|
93 | 0 | if (!pkey) { |
94 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_PASSED_NULL_PARAMETER); |
95 | 0 | return -1; |
96 | 0 | } |
97 | 0 |
|
98 | 0 | if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) { |
99 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT); |
100 | 0 | return -1; |
101 | 0 | } |
102 | 0 |
|
103 | 0 | ctx = EVP_MD_CTX_new(); |
104 | 0 | if (ctx == NULL) { |
105 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE); |
106 | 0 | goto err; |
107 | 0 | } |
108 | 0 |
|
109 | 0 | /* Convert signature OID into digest and public key OIDs */ |
110 | 0 | if (!OBJ_find_sigid_algs(OBJ_obj2nid(a->algorithm), &mdnid, &pknid)) { |
111 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
112 | 0 | goto err; |
113 | 0 | } |
114 | 0 | if (mdnid == NID_undef) { |
115 | 0 | if (!pkey->ameth || !pkey->ameth->item_verify) { |
116 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, |
117 | 0 | ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
118 | 0 | goto err; |
119 | 0 | } |
120 | 0 | ret = pkey->ameth->item_verify(ctx, it, asn, a, signature, pkey); |
121 | 0 | /* |
122 | 0 | * Return value of 2 means carry on, anything else means we exit |
123 | 0 | * straight away: either a fatal error of the underlying verification |
124 | 0 | * routine handles all verification. |
125 | 0 | */ |
126 | 0 | if (ret != 2) |
127 | 0 | goto err; |
128 | 0 | ret = -1; |
129 | 0 | } else { |
130 | 0 | const EVP_MD *type; |
131 | 0 | type = EVP_get_digestbynid(mdnid); |
132 | 0 | if (type == NULL) { |
133 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, |
134 | 0 | ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM); |
135 | 0 | goto err; |
136 | 0 | } |
137 | 0 |
|
138 | 0 | /* Check public key OID matches public key type */ |
139 | 0 | if (EVP_PKEY_type(pknid) != pkey->ameth->pkey_id) { |
140 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_WRONG_PUBLIC_KEY_TYPE); |
141 | 0 | goto err; |
142 | 0 | } |
143 | 0 |
|
144 | 0 | if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) { |
145 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB); |
146 | 0 | ret = 0; |
147 | 0 | goto err; |
148 | 0 | } |
149 | 0 |
|
150 | 0 | } |
151 | 0 |
|
152 | 0 | inl = ASN1_item_i2d(asn, &buf_in, it); |
153 | 0 |
|
154 | 0 | if (buf_in == NULL) { |
155 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE); |
156 | 0 | goto err; |
157 | 0 | } |
158 | 0 |
|
159 | 0 | ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length, |
160 | 0 | buf_in, inl); |
161 | 0 | if (ret <= 0) { |
162 | 0 | ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB); |
163 | 0 | goto err; |
164 | 0 | } |
165 | 0 | ret = 1; |
166 | 0 | err: |
167 | 0 | OPENSSL_clear_free(buf_in, (unsigned int)inl); |
168 | 0 | EVP_MD_CTX_free(ctx); |
169 | 0 | return ret; |
170 | 0 | } |