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
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t len)23 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
24 CBS cbs, body;
25 CBS_ASN1_TAG tag;
26 CBS_init(&cbs, buf, len);
27 if (CBS_get_any_asn1(&cbs, &body, &tag)) {
28 // DER has a unique encoding, so any parsed input should round-trip
29 // correctly.
30 size_t consumed = len - CBS_len(&cbs);
31 bssl::ScopedCBB cbb;
32 if (!CBB_init(cbb.get(), consumed) ||
33 !CBB_add_asn1_element(cbb.get(), tag, CBS_data(&body),
34 CBS_len(&body)) ||
35 CBB_len(cbb.get()) != consumed ||
36 memcmp(CBB_data(cbb.get()), buf, consumed) != 0) {
37 abort();
38 }
39 }
40
41 ECDSA_SIG *sig = ECDSA_SIG_from_bytes(buf, len);
42 if (sig != NULL) {
43 uint8_t *enc;
44 size_t enc_len;
45 if (!ECDSA_SIG_to_bytes(&enc, &enc_len, sig) ||
46 enc_len != len ||
47 memcmp(buf, enc, len) != 0) {
48 abort();
49 }
50 OPENSSL_free(enc);
51 ECDSA_SIG_free(sig);
52 }
53
54 return 0;
55 }
56