gencertbuf.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/perl
  2. # gencertbuf.pl
  3. # version 1.1
  4. # Updated 07/01/2014
  5. #
  6. # Copyright (C) 2006-2014 wolfSSL Inc.
  7. #
  8. use strict;
  9. use warnings;
  10. # ---- SCRIPT SETTINGS -------------------------------------------------------
  11. # output C header file to write cert/key buffers to
  12. my $outputFile = "./cyassl/certs_test.h";
  13. # 1024-bit certs/keys to be converted
  14. # Used with USE_CERT_BUFFERS_1024 define.
  15. my @fileList_1024 = (
  16. [ "./certs/1024/client-key.der", "client_key_der_1024" ],
  17. [ "./certs/1024/client-cert.der", "client_cert_der_1024" ],
  18. [ "./certs/1024/dh1024.der", "dh_key_der_1024" ],
  19. [ "./certs/1024/dsa1024.der", "dsa_key_der_1024" ],
  20. [ "./certs/1024/rsa1024.der", "rsa_key_der_1024" ]
  21. );
  22. # 2048-bit certs/keys to be converted
  23. # Used with USE_CERT_BUFFERS_2048 define.
  24. my @fileList_2048 = (
  25. [ "./certs/client-key.der", "client_key_der_2048" ],
  26. [ "./certs/client-cert.der", "client_cert_der_2048" ],
  27. [ "./certs/dh2048.der", "dh_key_der_2048" ],
  28. [ "./certs/dsa2048.der", "dsa_key_der_2048" ],
  29. [ "./certs/rsa2048.der", "rsa_key_der_2048" ],
  30. [ "./certs/ca-cert.der", "ca_cert_der_2048" ],
  31. [ "./certs/server-key.der", "server_key_der_2048" ],
  32. [ "./certs/server-cert.der", "server_cert_der_2048" ]
  33. );
  34. # ----------------------------------------------------------------------------
  35. my $num_1024 = @fileList_1024;
  36. my $num_2048 = @fileList_2048;
  37. # open our output file, "+>" creates and/or truncates
  38. open OUT_FILE, "+>", $outputFile or die $!;
  39. print OUT_FILE "/* certs_test.h */\n\n";
  40. print OUT_FILE "#ifndef CYASSL_CERTS_TEST_H\n";
  41. print OUT_FILE "#define CYASSL_CERTS_TEST_H\n\n";
  42. # convert and print 1024-bit cert/keys
  43. print OUT_FILE "#ifdef USE_CERT_BUFFERS_1024\n\n";
  44. for (my $i = 0; $i < $num_1024; $i++) {
  45. my $fname = $fileList_1024[$i][0];
  46. my $sname = $fileList_1024[$i][1];
  47. print OUT_FILE "/* $fname, 1024-bit */\n";
  48. print OUT_FILE "const unsigned char $sname\[] =\n";
  49. print OUT_FILE "{\n";
  50. file_to_hex($fname);
  51. print OUT_FILE "};\n";
  52. print OUT_FILE "const int sizeof_$sname = sizeof($sname);\n\n";
  53. }
  54. # convert and print 2048-bit certs/keys
  55. print OUT_FILE "#elif defined(USE_CERT_BUFFERS_2048)\n\n";
  56. for (my $i = 0; $i < $num_2048; $i++) {
  57. my $fname = $fileList_2048[$i][0];
  58. my $sname = $fileList_2048[$i][1];
  59. print OUT_FILE "/* $fname, 2048-bit */\n";
  60. print OUT_FILE "const unsigned char $sname\[] =\n";
  61. print OUT_FILE "{\n";
  62. file_to_hex($fname);
  63. print OUT_FILE "};\n";
  64. print OUT_FILE "const int sizeof_$sname = sizeof($sname);\n\n";
  65. }
  66. print OUT_FILE "#endif /* USE_CERT_BUFFERS_1024 */\n\n";
  67. print OUT_FILE "#endif /* CYASSL_CERTS_TEST_H */\n\n";
  68. # close certs_test.h file
  69. close OUT_FILE or die $!;
  70. # print file as hex, comma-separated, as needed by C buffer
  71. sub file_to_hex {
  72. my $fileName = $_[0];
  73. open my $fp, "<", $fileName or die $!;
  74. binmode($fp);
  75. my $fileLen = -s $fileName;
  76. my $byte;
  77. for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++)
  78. {
  79. if ($j == 1) {
  80. print OUT_FILE "\t";
  81. }
  82. read($fp, $byte, 1) or die "Error reading $fileName";
  83. my $output = sprintf("0x%02X", ord($byte));
  84. print OUT_FILE $output;
  85. if ($i != ($fileLen - 1)) {
  86. print OUT_FILE ", ";
  87. }
  88. if ($j == 10) {
  89. $j = 0;
  90. print OUT_FILE "\n";
  91. }
  92. }
  93. print OUT_FILE "\n";
  94. close($fp);
  95. }