pemtest.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/pem.h>
  13. #include <gmssl/hex.h>
  14. #include <gmssl/error.h>
  15. static const char *pem_unix_style =
  16. "-----BEGIN CERTIFICATE-----\n"
  17. "MIIBszCCAVegAwIBAgIIaeL+wBcKxnswDAYIKoEcz1UBg3UFADAuMQswCQYDVQQG\n"
  18. "EwJDTjEOMAwGA1UECgwFTlJDQUMxDzANBgNVBAMMBlJPT1RDQTAeFw0xMjA3MTQw\n"
  19. "MzExNTlaFw00MjA3MDcwMzExNTlaMC4xCzAJBgNVBAYTAkNOMQ4wDAYDVQQKDAVO\n"
  20. "UkNBQzEPMA0GA1UEAwwGUk9PVENBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE\n"
  21. "MPCca6pmgcchsTf2UnBeL9rtp4nw+itk1Kzrmbnqo05lUwkwlWK+4OIrtFdAqnRT\n"
  22. "V7Q9v1htkv42TsIutzd126NdMFswHwYDVR0jBBgwFoAUTDKxl9kzG8SmBcHG5Yti\n"
  23. "W/CXdlgwDAYDVR0TBAUwAwEB/zALBgNVHQ8EBAMCAQYwHQYDVR0OBBYEFEwysZfZ\n"
  24. "MxvEpgXBxuWLYlvwl3ZYMAwGCCqBHM9VAYN1BQADSAAwRQIgG1bSLeOXp3oB8H7b\n"
  25. "53W+CKOPl2PknmWEq/lMhtn25HkCIQDaHDgWxWFtnCrBjH16/W3Ezn7/U/Vjo5xI\n"
  26. "pDoiVhsLwg==\n"
  27. "-----END CERTIFICATE-----\n";
  28. static const char *pem_windows_style =
  29. "-----BEGIN CERTIFICATE-----\r\n"
  30. "MIIBszCCAVegAwIBAgIIaeL+wBcKxnswDAYIKoEcz1UBg3UFADAuMQswCQYDVQQG\r\n"
  31. "EwJDTjEOMAwGA1UECgwFTlJDQUMxDzANBgNVBAMMBlJPT1RDQTAeFw0xMjA3MTQw\r\n"
  32. "MzExNTlaFw00MjA3MDcwMzExNTlaMC4xCzAJBgNVBAYTAkNOMQ4wDAYDVQQKDAVO\r\n"
  33. "UkNBQzEPMA0GA1UEAwwGUk9PVENBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE\r\n"
  34. "MPCca6pmgcchsTf2UnBeL9rtp4nw+itk1Kzrmbnqo05lUwkwlWK+4OIrtFdAqnRT\r\n"
  35. "V7Q9v1htkv42TsIutzd126NdMFswHwYDVR0jBBgwFoAUTDKxl9kzG8SmBcHG5Yti\r\n"
  36. "W/CXdlgwDAYDVR0TBAUwAwEB/zALBgNVHQ8EBAMCAQYwHQYDVR0OBBYEFEwysZfZ\r\n"
  37. "MxvEpgXBxuWLYlvwl3ZYMAwGCCqBHM9VAYN1BQADSAAwRQIgG1bSLeOXp3oB8H7b\r\n"
  38. "53W+CKOPl2PknmWEq/lMhtn25HkCIQDaHDgWxWFtnCrBjH16/W3Ezn7/U/Vjo5xI\r\n"
  39. "pDoiVhsLwg==\r\n"
  40. "-----END CERTIFICATE-----\r\n";
  41. #define TEST_PEM_BASE64_DECODE 1
  42. static const char *pem_bin_hex =
  43. "308201B330820157A003020102020869E2FEC0170AC67B300C06082A811CCF550183750500302E310B30090603550406"
  44. "1302434E310E300C060355040A0C054E52434143310F300D06035504030C06524F4F544341301E170D31323037313430"
  45. "33313135395A170D3432303730373033313135395A302E310B300906035504061302434E310E300C060355040A0C054E"
  46. "52434143310F300D06035504030C06524F4F5443413059301306072A8648CE3D020106082A811CCF5501822D03420004"
  47. "30F09C6BAA6681C721B137F652705E2FDAEDA789F0FA2B64D4ACEB99B9EAA34E655309309562BEE0E22BB45740AA7453"
  48. "57B43DBF586D92FE364EC22EB73775DBA35D305B301F0603551D230418301680144C32B197D9331BC4A605C1C6E58B62"
  49. "5BF0977658300C0603551D13040530030101FF300B0603551D0F040403020106301D0603551D0E041604144C32B197D9"
  50. "331BC4A605C1C6E58B625BF0977658300C06082A811CCF550183750500034800304502201B56D22DE397A77A01F07EDB"
  51. "E775BE08A38F9763E49E6584ABF94C86D9F6E479022100DA1C3816C5616D9C2AC18C7D7AFD6DC4CE7EFF53F563A39C48"
  52. "A43A22561B0BC2";
  53. static int test_pem_unix_style(void)
  54. {
  55. FILE *fp;
  56. const char *text = pem_unix_style;
  57. size_t textlen = strlen(text);
  58. const char *file = "test_unix_style.pem";
  59. uint8_t buf[1024];
  60. size_t len;
  61. if (!(fp = fopen(file, "wb"))) {
  62. error_print();
  63. return -1;
  64. }
  65. fwrite(text, 1, textlen, fp);
  66. fclose(fp);
  67. if (!(fp = fopen(file, "rb"))) {
  68. error_print();
  69. return -1;
  70. }
  71. if (pem_read(fp, "CERTIFICATE", buf, &len, sizeof(buf)) != 1) {
  72. error_print();
  73. return -1;
  74. }
  75. fclose(fp);
  76. if (TEST_PEM_BASE64_DECODE) {
  77. uint8_t bin[1024];
  78. size_t binlen;
  79. hex_to_bytes(pem_bin_hex, strlen(pem_bin_hex), bin, &binlen);
  80. if (len != binlen) {
  81. error_print();
  82. return -1;
  83. }
  84. if (memcmp(buf, bin, binlen) != 0) {
  85. error_print();
  86. return -1;
  87. }
  88. }
  89. printf("%s() ok\n", __FUNCTION__);
  90. return 1;
  91. }
  92. static int test_pem_unix_style_without_last_newline(void)
  93. {
  94. FILE *fp;
  95. const char *text = pem_unix_style;
  96. size_t textlen = strlen(text) - 1; // without last '\n'
  97. const char *file = "test_unix_style_without_last_newline.pem";
  98. uint8_t buf[1024];
  99. size_t len;
  100. if (!(fp = fopen(file, "wb"))) {
  101. error_print();
  102. return -1;
  103. }
  104. fwrite(text, 1, textlen, fp);
  105. fclose(fp);
  106. if (!(fp = fopen(file, "rb"))) {
  107. error_print();
  108. return -1;
  109. }
  110. if (pem_read(fp, "CERTIFICATE", buf, &len, sizeof(buf)) != 1) {
  111. error_print();
  112. return -1;
  113. }
  114. fclose(fp);
  115. if (TEST_PEM_BASE64_DECODE) {
  116. uint8_t bin[1024];
  117. size_t binlen;
  118. hex_to_bytes(pem_bin_hex, strlen(pem_bin_hex), bin, &binlen);
  119. if (len != binlen) {
  120. error_print();
  121. return -1;
  122. }
  123. if (memcmp(buf, bin, binlen) != 0) {
  124. error_print();
  125. return -1;
  126. }
  127. }
  128. printf("%s() ok\n", __FUNCTION__);
  129. return 1;
  130. }
  131. static int test_pem_windows_style(void)
  132. {
  133. FILE *fp;
  134. const char *text = pem_windows_style;
  135. size_t textlen = strlen(text);
  136. const char *file = "test_windows_style.pem";
  137. uint8_t buf[1024];
  138. size_t len;
  139. if (!(fp = fopen(file, "wb"))) {
  140. error_print();
  141. return -1;
  142. }
  143. fwrite(text, 1, textlen, fp);
  144. fclose(fp);
  145. if (!(fp = fopen(file, "rb"))) {
  146. error_print();
  147. return -1;
  148. }
  149. if (pem_read(fp, "CERTIFICATE", buf, &len, sizeof(buf)) != 1) {
  150. error_print();
  151. return -1;
  152. }
  153. fclose(fp);
  154. if (TEST_PEM_BASE64_DECODE) {
  155. uint8_t bin[1024];
  156. size_t binlen;
  157. hex_to_bytes(pem_bin_hex, strlen(pem_bin_hex), bin, &binlen);
  158. if (len != binlen) {
  159. error_print();
  160. return -1;
  161. }
  162. if (memcmp(buf, bin, binlen) != 0) {
  163. error_print();
  164. return -1;
  165. }
  166. }
  167. printf("%s() ok\n", __FUNCTION__);
  168. return 1;
  169. }
  170. static int test_pem_windows_style_without_last_newline(void)
  171. {
  172. FILE *fp;
  173. const char *text = pem_windows_style;
  174. size_t textlen = strlen(text) - 2; // without last '\r\n'
  175. const char *file = "test_windows_style_without_last_newline.pem";
  176. uint8_t buf[1024];
  177. size_t len;
  178. if (!(fp = fopen(file, "wb"))) {
  179. error_print();
  180. return -1;
  181. }
  182. fwrite(text, 1, textlen, fp);
  183. fclose(fp);
  184. if (!(fp = fopen(file, "rb"))) {
  185. error_print();
  186. return -1;
  187. }
  188. if (pem_read(fp, "CERTIFICATE", buf, &len, sizeof(buf)) != 1) {
  189. error_print();
  190. return -1;
  191. }
  192. fclose(fp);
  193. if (TEST_PEM_BASE64_DECODE) {
  194. uint8_t bin[1024];
  195. size_t binlen;
  196. hex_to_bytes(pem_bin_hex, strlen(pem_bin_hex), bin, &binlen);
  197. if (len != binlen) {
  198. error_print();
  199. return -1;
  200. }
  201. if (memcmp(buf, bin, binlen) != 0) {
  202. error_print();
  203. return -1;
  204. }
  205. }
  206. printf("%s() ok\n", __FUNCTION__);
  207. return 1;
  208. }
  209. int main(void)
  210. {
  211. if (test_pem_unix_style() != 1) { error_print(); return 1; }
  212. if (test_pem_unix_style_without_last_newline() != 1) { error_print(); return 1; }
  213. if (test_pem_windows_style() != 1) { error_print(); return 1; }
  214. if (test_pem_windows_style_without_last_newline() != 1) { error_print(); return 1; }
  215. return 0;
  216. }