/src/boringssl/crypto/x509/a_verify.cc
Line | Count | Source |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
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/x509.h> |
16 | | |
17 | | #include <stdio.h> |
18 | | #include <sys/types.h> |
19 | | |
20 | | #include <openssl/asn1.h> |
21 | | #include <openssl/bn.h> |
22 | | #include <openssl/digest.h> |
23 | | #include <openssl/err.h> |
24 | | #include <openssl/evp.h> |
25 | | #include <openssl/mem.h> |
26 | | #include <openssl/obj.h> |
27 | | #include <openssl/span.h> |
28 | | |
29 | | #include "internal.h" |
30 | | |
31 | | |
32 | | using namespace bssl; |
33 | | |
34 | | int bssl::x509_verify_signature(const X509_ALGOR *sigalg, |
35 | | const ASN1_BIT_STRING *signature, |
36 | 0 | Span<const uint8_t> in, EVP_PKEY *pkey) { |
37 | 0 | if (signature->type == V_ASN1_BIT_STRING && |
38 | 0 | ASN1_BIT_STRING_unused_bits(signature) != 0) { |
39 | 0 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_BIT_STRING_BITS_LEFT); |
40 | 0 | return 0; |
41 | 0 | } |
42 | 0 | Span<const uint8_t> signature_bytes(ASN1_STRING_get0_data(signature), |
43 | 0 | ASN1_STRING_length(signature)); |
44 | 0 | return x509_verify_signature_bytes(sigalg, signature_bytes, in, pkey); |
45 | 0 | } |
46 | | |
47 | | int bssl::x509_verify_signature_bytes(const X509_ALGOR *sigalg, |
48 | | Span<const uint8_t> signature, |
49 | 0 | Span<const uint8_t> in, EVP_PKEY *pkey) { |
50 | 0 | if (!pkey) { |
51 | 0 | OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); |
52 | 0 | return 0; |
53 | 0 | } |
54 | | |
55 | 0 | ScopedEVP_MD_CTX ctx; |
56 | 0 | if (!x509_digest_verify_init(ctx.get(), sigalg, pkey)) { |
57 | 0 | return 0; |
58 | 0 | } |
59 | 0 | if (!EVP_DigestVerify(ctx.get(), signature.data(), signature.size(), |
60 | 0 | in.data(), in.size())) { |
61 | 0 | OPENSSL_PUT_ERROR(X509, ERR_R_EVP_LIB); |
62 | 0 | return 0; |
63 | 0 | } |
64 | 0 | return 1; |
65 | 0 | } |
66 | | |
67 | | int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *sigalg, |
68 | | const ASN1_BIT_STRING *signature, void *asn, |
69 | 0 | EVP_PKEY *pkey) { |
70 | 0 | uint8_t *in = nullptr; |
71 | 0 | int in_len = ASN1_item_i2d(reinterpret_cast<ASN1_VALUE *>(asn), &in, it); |
72 | 0 | if (in_len < 0) { |
73 | 0 | return 0; |
74 | 0 | } |
75 | 0 | UniquePtr<uint8_t> free_in(in); |
76 | 0 | return x509_verify_signature(sigalg, signature, Span(in, in_len), pkey); |
77 | 0 | } |