Coverage Report

Created: 2023-06-07 07:11

/src/fuzz_pkcs8.cc
Line
Count
Source
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
29
5.97k
DEFINE_PROTO_FUZZER(const asn1_pdu::PDU& asn1) {
30
5.97k
  asn1_pdu::ASN1PDUToDER converter;
31
5.97k
  std::vector<uint8_t> encoded = converter.PDUToDER(asn1);
32
5.97k
  const uint8_t* buf = encoded.data();
33
5.97k
  size_t len = encoded.size();
34
35
5.97k
  CBS cbs;
36
5.97k
  CBS_init(&cbs, buf, len);
37
5.97k
  bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
38
5.97k
  if (pkey == NULL) {
39
5.00k
    return;
40
5.00k
  }
41
42
978
  uint8_t* der;
43
978
  size_t der_len;
44
978
  bssl::ScopedCBB cbb;
45
978
  if (CBB_init(cbb.get(), 0) &&
46
978
      EVP_marshal_private_key(cbb.get(), pkey.get()) &&
47
978
      CBB_finish(cbb.get(), &der, &der_len)) {
48
978
    OPENSSL_free(der);
49
978
  }
50
978
  ERR_clear_error();
51
978
}