1 // Copyright 2020 Google Inc.
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 // http://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 ////////////////////////////////////////////////////////////////////////////////
16
17 // This fuzz target fuzzes the same API as
18 // https://github.com/google/boringssl/blob/master/fuzz/pkcs8.cc, but it employs
19 // libprotobuf-mutator for structure-aware fuzzing.
20
21 #include <openssl/bytestring.h>
22 #include <openssl/err.h>
23 #include <openssl/evp.h>
24 #include <openssl/mem.h>
25 #include "asn1_pdu.pb.h"
26 #include "asn1_pdu_to_der.h"
27 #include "libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h"
28
DEFINE_PROTO_FUZZER(const asn1_pdu::PDU & asn1)29 DEFINE_PROTO_FUZZER(const asn1_pdu::PDU& asn1) {
30 asn1_pdu::ASN1PDUToDER converter;
31 std::vector<uint8_t> encoded = converter.PDUToDER(asn1);
32 const uint8_t* buf = encoded.data();
33 size_t len = encoded.size();
34
35 CBS cbs;
36 CBS_init(&cbs, buf, len);
37 bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
38 if (pkey == NULL) {
39 return;
40 }
41
42 uint8_t* der;
43 size_t der_len;
44 bssl::ScopedCBB cbb;
45 if (CBB_init(cbb.get(), 0) &&
46 EVP_marshal_private_key(cbb.get(), pkey.get()) &&
47 CBB_finish(cbb.get(), &der, &der_len)) {
48 OPENSSL_free(der);
49 }
50 ERR_clear_error();
51 }