Coverage Report

Created: 2025-06-11 06:40

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