David Benjamin | 505bbf9 | 2022-05-25 15:51:12 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2022, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 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 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) { |
| 24 | CBS cbs, body; |
David Benjamin | a1dffbf | 2022-10-25 16:29:43 -0400 | [diff] [blame] | 25 | CBS_ASN1_TAG tag; |
David Benjamin | 505bbf9 | 2022-05-25 15:51:12 -0400 | [diff] [blame] | 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 | CBB body_cbb; |
| 33 | if (!CBB_init(cbb.get(), consumed) || |
| 34 | !CBB_add_asn1(cbb.get(), &body_cbb, tag) || |
| 35 | !CBB_add_bytes(&body_cbb, CBS_data(&body), CBS_len(&body)) || |
| 36 | !CBB_flush(cbb.get()) || |
| 37 | CBB_len(cbb.get()) != consumed || |
| 38 | memcmp(CBB_data(cbb.get()), buf, consumed) != 0) { |
| 39 | abort(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | ECDSA_SIG *sig = ECDSA_SIG_from_bytes(buf, len); |
| 44 | if (sig != NULL) { |
| 45 | uint8_t *enc; |
| 46 | size_t enc_len; |
| 47 | if (!ECDSA_SIG_to_bytes(&enc, &enc_len, sig) || |
| 48 | enc_len != len || |
| 49 | memcmp(buf, enc, len) != 0) { |
| 50 | abort(); |
| 51 | } |
| 52 | OPENSSL_free(enc); |
| 53 | ECDSA_SIG_free(sig); |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |