| 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 | 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 | 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 | } |
| 58 |