From 72aef0ab1118cbc441bf91b966266857ffad1c87 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 31 Jul 2018 15:57:02 -0700 Subject: [PATCH] Added handy script for converting DER file to C array. Example: `./scripts/dertoc.pl ./certs/server-cert.der server_cert_der_2048 server-cert.c`. --- scripts/dertoc.pl | 71 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/include.am | 2 ++ 2 files changed, 73 insertions(+) create mode 100755 scripts/dertoc.pl diff --git a/scripts/dertoc.pl b/scripts/dertoc.pl new file mode 100755 index 000000000..c02d7d3f3 --- /dev/null +++ b/scripts/dertoc.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +# dertoc.pl +# version 1.0 +# Updated 07/31/2018 +# +# Copyright (C) 2006-2018 wolfSSL Inc. +# + +use strict; +use warnings; + +my $num_args = $#ARGV + 1; +if ($num_args != 3 ) { + print "usage: ./scripts/dertoc.pl ./certs/server-cert.der server_cert_der_2048 dertoc.c\n"; + exit; +} + +my $inFile = $ARGV[0]; +my $outName = $ARGV[1]; +my $outputFile = $ARGV[2]; + +# open our output file, "+>" creates and/or truncates +open OUT_FILE, "+>", $outputFile or die $!; + +print OUT_FILE "/* $outputFile */\n\n"; + +print OUT_FILE "static const unsigned char $outName\[] =\n"; +print OUT_FILE "{\n"; +file_to_hex($inFile); +print OUT_FILE "};\n"; +print OUT_FILE "static const int sizeof_$outName = sizeof($outName);\n\n"; + +# close file +close OUT_FILE or die $!; + + + +# print file as hex, comma-separated, as needed by C buffer +sub file_to_hex { + my $fileName = $_[0]; + + open my $fp, "<", $fileName or die $!; + binmode($fp); + + my $fileLen = -s $fileName; + my $byte; + + for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++) + { + if ($j == 1) { + print OUT_FILE "\t"; + } + read($fp, $byte, 1) or die "Error reading $fileName"; + my $output = sprintf("0x%02X", ord($byte)); + print OUT_FILE $output; + + if ($i != ($fileLen - 1)) { + print OUT_FILE ", "; + } + + if ($j == 10) { + $j = 0; + print OUT_FILE "\n"; + } + } + + print OUT_FILE "\n"; + + close($fp); +} diff --git a/scripts/include.am b/scripts/include.am index 33c00cd90..93508b8cf 100644 --- a/scripts/include.am +++ b/scripts/include.am @@ -75,3 +75,5 @@ EXTRA_DIST += scripts/testsuite.pcap \ # leave openssl.test as extra until non bash works EXTRA_DIST += scripts/openssl.test + +EXTRA_DIST += scripts/dertoc.pl