1
0

gencertbuf.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/perl
  2. # gencertbuf.pl
  3. # version 1.1
  4. # Updated 07/01/2014
  5. #
  6. # Copyright (C) 2006-2015 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 = "./wolfssl/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 WOLFSSL_CERTS_TEST_H\n";
  41. print OUT_FILE "#define WOLFSSL_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 "static const unsigned char $sname\[] =\n";
  49. print OUT_FILE "{\n";
  50. file_to_hex($fname);
  51. print OUT_FILE "};\n";
  52. print OUT_FILE "static 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 "static const unsigned char $sname\[] =\n";
  61. print OUT_FILE "{\n";
  62. file_to_hex($fname);
  63. print OUT_FILE "};\n";
  64. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  65. }
  66. print OUT_FILE "#endif /* USE_CERT_BUFFERS_1024 */\n\n";
  67. print OUT_FILE "/* dh1024 p */
  68. static const unsigned char dh_p[] =
  69. {
  70. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  71. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  72. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  73. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  74. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  75. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  76. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  77. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  78. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  79. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  80. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  81. };
  82. /* dh1024 g */
  83. static const unsigned char dh_g[] =
  84. {
  85. 0x02,
  86. };\n\n\n";
  87. print OUT_FILE "#endif /* WOLFSSL_CERTS_TEST_H */\n\n";
  88. # close certs_test.h file
  89. close OUT_FILE or die $!;
  90. # print file as hex, comma-separated, as needed by C buffer
  91. sub file_to_hex {
  92. my $fileName = $_[0];
  93. open my $fp, "<", $fileName or die $!;
  94. binmode($fp);
  95. my $fileLen = -s $fileName;
  96. my $byte;
  97. for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++)
  98. {
  99. if ($j == 1) {
  100. print OUT_FILE "\t";
  101. }
  102. read($fp, $byte, 1) or die "Error reading $fileName";
  103. my $output = sprintf("0x%02X", ord($byte));
  104. print OUT_FILE $output;
  105. if ($i != ($fileLen - 1)) {
  106. print OUT_FILE ", ";
  107. }
  108. if ($j == 10) {
  109. $j = 0;
  110. print OUT_FILE "\n";
  111. }
  112. }
  113. print OUT_FILE "\n";
  114. close($fp);
  115. }