print_c_code.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* print_c_code.c - Output the C code that used to be provided in openssl dhparam -C
  2. * Reads full output from openssl in stdin, prints C code to stdout.
  3. *
  4. * Sample output from openssl 3:
  5. [root@localhost nrpe]# openssl dhparam -text 2048
  6. Generating DH parameters, 2048 bit long safe prime
  7. ................................................................................ ................................................................................ ..............................................................+.......+......... .................................................+.............................. ................................................................................ ................................................................................ ...............................................+.....+.......................... .................................+.............................................+ .............................................+.................................. .................................................+.............................. ................................................................................ ................................................................................ ...................+............................................................ ......................................................................+......... ................................................................+............... ................................................................................ ..................+...........................+...............................+. ........................................................+....................... ................................................................................ ................................................................................ .............................+.................................................. ................................................................................ ..........+..................................................................... ................................................................................ ...............+................................................................ .......................................................................+....+... ................................................................................ .............................................................+..+............... ................................................................................ ........+....................................................................... ............................................................+................... ..............+........................................................+........ ...................................................+............................ .+..............+.................+...........................................+. ................................................................................ ......................................................+......................... ................................................................................ ................................................................................ ......+................................................................+........ ...............................................+..............+................. ....................................+..............................+............ ................................................................................ .........................................+............................+......... ................................................................................ .............................................................+.................. .............................................+.................................. .......++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*+ +*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++* ++*++*++*++*++*++*++*++*++*++*++*++*++*
  8. DH Parameters: (2048 bit)
  9. P:
  10. 00:d4:81:57:06:2e:dc:2c:c0:0e:7f:20:5c:07:6a:
  11. 22:06:b5:fc:f1:83:bb:99:31:38:03:a7:ef:98:b6:
  12. 75:32:33:e2:55:08:d4:46:a3:f1:94:85:de:da:2f:
  13. e7:49:8d:69:f8:28:73:57:7a:58:99:61:f5:37:76:
  14. f0:45:68:9e:cc:67:77:b4:4d:08:ec:3b:71:a2:62:
  15. e6:26:d8:2c:2d:61:1e:45:20:5b:1b:bc:19:de:ee:
  16. 99:a0:c5:ad:2a:59:bf:e8:26:95:56:71:0e:f0:d8:
  17. 3b:3b:6d:91:5a:c8:f4:3d:1a:02:75:76:42:cf:63:
  18. c7:3c:07:3b:0c:c0:98:e9:a9:ab:bc:d6:a3:a1:cb:
  19. 85:79:ff:37:3c:f8:3a:a0:84:b3:a7:68:cd:3d:f4:
  20. a6:d8:c7:7b:d5:f4:11:33:8f:ba:2c:67:15:65:38:
  21. 23:10:67:2d:fc:4c:c0:bc:b5:43:70:67:86:b9:83:
  22. 5a:42:a7:18:11:7f:32:4c:3d:e2:08:2f:0d:59:ae:
  23. 1f:8c:73:72:16:00:d3:e1:e7:38:a3:24:b5:e0:25:
  24. 0e:c2:41:b8:09:82:8d:05:c8:9c:61:d8:61:f5:19:
  25. 93:f7:b1:02:44:20:bc:7e:2f:3f:e0:c8:d2:5a:50:
  26. cc:7f:b5:96:8b:83:b7:5b:03:8a:52:a1:69:4e:b4:
  27. 8a:5f
  28. G: 2 (0x2)
  29. -----BEGIN DH PARAMETERS-----
  30. MIIBCAKCAQEA1IFXBi7cLMAOfyBcB2oiBrX88YO7mTE4A6fvmLZ1MjPiVQjURqPx
  31. lIXe2i/nSY1p+ChzV3pYmWH1N3bwRWiezGd3tE0I7DtxomLmJtgsLWEeRSBbG7wZ
  32. 3u6ZoMWtKlm/6CaVVnEO8Ng7O22RWsj0PRoCdXZCz2PHPAc7DMCY6amrvNajocuF
  33. ef83PPg6oISzp2jNPfSm2Md71fQRM4+6LGcVZTgjEGct/EzAvLVDcGeGuYNaQqcY
  34. EX8yTD3iCC8NWa4fjHNyFgDT4ec4oyS14CUOwkG4CYKNBcicYdhh9RmT97ECRCC8
  35. fi8/4MjSWlDMf7WWi4O3WwOKUqFpTrSKXwIBAg==
  36. -----END DH PARAMETERS-----
  37. [root@localhost nrpe]#
  38. *********
  39. * Correct C output will look like
  40. #ifndef HEADER_DH_H
  41. #include <openssl/dh.h>
  42. #endif
  43. DH *get_dh2048()
  44. {
  45. static unsigned char dh2048_p[]={
  46. 0x84,0xC5,0x67,0x9B,0x9E,0xAD,0x8C,0x80,0xAF,0x35,0x81,0x83,
  47. 0xD7,0x46,0x08,0x8B,0x5E,0xF2,0x90,0xBC,0xF3,0xC2,0x48,0x13,
  48. 0x48,0x47,0xA4,0x2D,0x6E,0x2F,0x5C,0xF4,0x75,0x11,0xE9,0x3F,
  49. 0x5E,0x2E,0x17,0x41,0x9A,0xC9,0x26,0x48,0xE9,0xDA,0x27,0x28,
  50. 0xBD,0x31,0x9F,0xB3,0x02,0xD6,0x7A,0x3D,0x64,0x4F,0x0F,0x56,
  51. 0x24,0xE5,0x8B,0xC6,0x83,0x35,0x3B,0x0D,0x24,0x3E,0xF4,0x60,
  52. 0x72,0x3A,0xE4,0xD1,0x7F,0x32,0xDC,0x26,0xB7,0x5C,0x1D,0x4D,
  53. 0x60,0x57,0x64,0x26,0xC3,0xC6,0x7B,0xE9,0x02,0xAF,0xDA,0x63,
  54. 0xE9,0x48,0x89,0x30,0xBA,0x70,0xF2,0x42,0xF9,0x77,0x69,0x84,
  55. 0xCE,0x0B,0x72,0x7E,0x86,0xC7,0xC5,0x63,0xC0,0xD7,0x3E,0x9D,
  56. 0x0C,0x88,0x88,0x91,0x66,0x9B,0xD3,0x62,0x16,0xC2,0x46,0x2B,
  57. 0x08,0xBF,0x3B,0xA9,0xAA,0x4C,0xBF,0x2D,0xB5,0xC0,0xC5,0x26,
  58. 0xF6,0xDB,0x83,0xDD,0x42,0x8E,0x57,0x68,0xE7,0x93,0x0E,0x3F,
  59. 0xAB,0x95,0x45,0x03,0x15,0x87,0x02,0x2F,0x18,0xBB,0x71,0xB9,
  60. 0x8E,0x3C,0x67,0xCE,0x63,0x85,0x04,0xE1,0x55,0xA8,0x06,0x30,
  61. 0x52,0x03,0x33,0x4F,0x4A,0x34,0x61,0x0F,0x4F,0xE5,0x93,0xD0,
  62. 0x83,0x33,0x9B,0xF1,0x9A,0x87,0xEC,0x9A,0xC4,0xB5,0x51,0x7B,
  63. 0x2F,0x7D,0xBB,0x95,0x33,0x46,0xF7,0x2D,0xBD,0x90,0x93,0x7A,
  64. 0xA0,0x99,0x24,0xE1,0x5B,0x24,0x2D,0x91,0x9B,0x58,0xA4,0xE1,
  65. 0xF6,0xB2,0x76,0x20,0x1B,0xB7,0x00,0x0C,0x8D,0xF0,0x8C,0x90,
  66. 0x44,0xFF,0x35,0x40,0xFE,0x0F,0xCC,0x34,0x74,0x82,0xCB,0x38,
  67. 0x52,0x09,0x83,0x63,
  68. };
  69. static unsigned char dh2048_g[]={
  70. 0x02,
  71. };
  72. DH *dh;
  73. if ((dh=DH_new()) == NULL) return(NULL);
  74. dh->p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);
  75. dh->g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
  76. if ((dh->p == NULL) || (dh->g == NULL))
  77. { DH_free(dh); return(NULL); }
  78. return(dh);
  79. }
  80. */
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <string.h>
  84. int main() {
  85. char line[1024];
  86. int result;
  87. unsigned int term = 0;
  88. FILE *fp = fdopen(0, "r");
  89. if (fp == NULL) {
  90. return 1;
  91. }
  92. int found_dh_parameters = 0;
  93. int found_primes = 0;
  94. while (!found_primes) {
  95. fgets(line, 1023, fp);
  96. if (!found_dh_parameters) {
  97. // Didn't find DH Parameters section header yet
  98. // See if this line is the "DH Parameters line"
  99. char *forward = strchr(line, 'D');
  100. if (forward == NULL) {
  101. forward = strchr(line, 'd');
  102. }
  103. if (forward == NULL) {
  104. continue;
  105. }
  106. // Note: fgets always NULL-terminates its string.
  107. found_dh_parameters = !strncasecmp("DH Parameters:", forward, strlen("DH Parameters"));
  108. continue;
  109. }
  110. // Otherwise, see if this is the prime/polynomial header.
  111. char *forward = strchr(line, 'P');
  112. if (forward == NULL) {
  113. forward = strchr(line, 'p');
  114. }
  115. if (forward == NULL) {
  116. continue;
  117. }
  118. found_primes = strchr(forward, ':') != NULL;
  119. }
  120. // Print the first part of the C code:
  121. printf("#ifndef HEADER_DH_H\n"
  122. "#define OPENSSL_API_COMPAT 10002\n"
  123. "#define OPENSSL_NO_DEPRECATED\n"
  124. "#include <openssl/dh.h>\n"
  125. "#endif\n"
  126. "DH *get_dh2048()\n"
  127. "{\n"
  128. "\tstatic unsigned char dh2048_p[]={");
  129. int terms_written = 0;
  130. while (1) {
  131. result = fscanf(fp, " %x : ", &term);
  132. if (result == 0) {
  133. // All done
  134. break;
  135. }
  136. if (term == 0 && terms_written == 0) {
  137. continue;
  138. }
  139. if (terms_written % 15 == 0) {
  140. printf("\n\t\t");
  141. }
  142. printf("0x%02X,", term);
  143. terms_written += 1;
  144. }
  145. printf("\n\t};\n"
  146. "\tstatic unsigned char dh2048_g[]={\n"
  147. "\t\t0x02,\n"
  148. "\t};\n"
  149. "\tDH *dh;\n"
  150. "\n"
  151. "\tif ((dh=DH_new()) == NULL) return(NULL);\n"
  152. "\tBIGNUM *p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);\n"
  153. "\tBIGNUM *g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);\n"
  154. "\tif ((p == NULL) || (g == NULL))\n"
  155. "\t\t{ DH_free(dh); return(NULL); }\n"
  156. "\tint result = DH_set0_pqg(dh, p, NULL, g);\n"
  157. "\tif (result == 0)"
  158. "\t\t{ DH_free(dh); return(NULL); }\n"
  159. "\treturn(dh);\n"
  160. "}\n");
  161. return 0;
  162. }