xref: /aosp_15_r20/external/cronet/net/data/ssl/scripts/generate-fuzzer-cert-include.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2017 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Generate C/C++ source from certificate.
6
7Usage:
8  generate-spdy-session-fuzzer-includes.py input_filename output_filename
9
10Load the PEM block from `input_filename` certificate, perform base64 decoding
11and hex encoding, and save it in `output_filename` so that it can be directly
12included from C/C++ source as a char array.
13"""
14
15import base64
16import re
17import sys
18import textwrap
19
20input_filename = sys.argv[1]
21output_filename = sys.argv[2]
22
23# Load PEM block.
24with open(input_filename, 'r') as f:
25  match = re.search(
26      r"-----BEGIN CERTIFICATE-----\n(.+)-----END CERTIFICATE-----\n", f.read(),
27      re.DOTALL)
28text = match.group(1)
29
30# Perform Base64 decoding.
31data = base64.b64decode(text)
32
33# Hex format data.
34hex_encoded = ", ".join("0x{:02x}".format(c) for c in bytearray(data))
35
36# Write into |output_filename| wrapped at 80 columns.
37with open(output_filename, 'w') as f:
38  f.write(textwrap.fill(hex_encoded, 80))
39  f.write("\n")
40