/src/boringssl/fuzz/der_roundtrip.cc
Line | Count | Source |
1 | | // Copyright 2022 The BoringSSL Authors |
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 <stdlib.h> |
16 | | #include <string.h> |
17 | | |
18 | | #include <openssl/bytestring.h> |
19 | | #include <openssl/ecdsa.h> |
20 | | #include <openssl/mem.h> |
21 | | |
22 | | |
23 | 1.19k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) { |
24 | 1.19k | CBS cbs, body; |
25 | 1.19k | CBS_ASN1_TAG tag; |
26 | 1.19k | CBS_init(&cbs, buf, len); |
27 | 1.19k | if (CBS_get_any_asn1(&cbs, &body, &tag)) { |
28 | | // DER has a unique encoding, so any parsed input should round-trip |
29 | | // correctly. |
30 | 763 | size_t consumed = len - CBS_len(&cbs); |
31 | 763 | bssl::ScopedCBB cbb; |
32 | 763 | if (!CBB_init(cbb.get(), consumed) || |
33 | 763 | !CBB_add_asn1_element(cbb.get(), tag, CBS_data(&body), |
34 | 763 | CBS_len(&body)) || |
35 | 763 | CBB_len(cbb.get()) != consumed || |
36 | 763 | memcmp(CBB_data(cbb.get()), buf, consumed) != 0) { |
37 | 0 | abort(); |
38 | 0 | } |
39 | 763 | } |
40 | | |
41 | 1.19k | ECDSA_SIG *sig = ECDSA_SIG_from_bytes(buf, len); |
42 | 1.19k | if (sig != NULL) { |
43 | 359 | uint8_t *enc; |
44 | 359 | size_t enc_len; |
45 | 359 | if (!ECDSA_SIG_to_bytes(&enc, &enc_len, sig) || |
46 | 359 | enc_len != len || |
47 | 359 | memcmp(buf, enc, len) != 0) { |
48 | 0 | abort(); |
49 | 0 | } |
50 | 359 | OPENSSL_free(enc); |
51 | 359 | ECDSA_SIG_free(sig); |
52 | 359 | } |
53 | | |
54 | 1.19k | return 0; |
55 | 1.19k | } |