1
0

asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* asn1.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef WOLFSSL_USER_SETTINGS
  25. #include <wolfssl/options.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #include <wolfssl/wolfcrypt/asn_public.h>
  29. #include <wolfssl/wolfcrypt/coding.h>
  30. #include <wolfssl/wolfcrypt/error-crypt.h>
  31. #include <stdio.h>
  32. #if defined(WOLFSSL_ASN_PRINT) && !defined(NO_FILESYSTEM)
  33. /* Increment allocated data by this much. */
  34. #define DATA_INC_LEN 256
  35. /* File format is DER/BER. */
  36. #define FORMAT_DER 0
  37. /* File format is BASE64. */
  38. #define FORMAT_BASE64 1
  39. /* File format is PEM. */
  40. #define FORMAT_PEM 2
  41. /* ASN.1 print options. */
  42. static Asn1PrintOptions opts;
  43. /* ASN.1 parsing state. */
  44. static Asn1 asn1;
  45. /* Read the contents of a file into a dynamically allocated buffer.
  46. *
  47. * Uses realloc as input may be stdin.
  48. *
  49. * @param [in] fp File pointer to read from.
  50. * @param [out] pdata Pointer to data.
  51. * @param [out] plen Pointer to length.
  52. * @return 0 on success.
  53. * @return 1 on failure.
  54. */
  55. static int asn1App_ReadFile(FILE* fp, unsigned char** pdata, word32* plen)
  56. {
  57. int ret = 0;
  58. word32 len = 0;
  59. size_t read_len;
  60. /* Allocate a minimum amount. */
  61. unsigned char* data = (unsigned char*)malloc(DATA_INC_LEN);
  62. if (data != NULL) {
  63. /* Read more data. */
  64. while ((read_len = fread(data + len, 1, DATA_INC_LEN, fp)) != 0) {
  65. unsigned char* p;
  66. /* Add read data amount to length. */
  67. len += (word32)read_len;
  68. /* Stop if we are at end-of-file. */
  69. if (feof(fp)) {
  70. break;
  71. }
  72. /* Make space for more data to be added to buffer. */
  73. p = (unsigned char*)realloc(data, len + DATA_INC_LEN);
  74. if (p == NULL) {
  75. /* Reallocation failed - free current buffer. */
  76. free(data);
  77. data = NULL;
  78. break;
  79. }
  80. /* Set data to new pointer. */
  81. data = p;
  82. }
  83. /* Done with file. */
  84. fclose(fp);
  85. }
  86. if (data != NULL) {
  87. /* Return data and length. */
  88. *pdata = data;
  89. *plen = len;
  90. }
  91. else {
  92. /* Failed to allocate data. */
  93. ret = MEMORY_E;
  94. }
  95. return ret;
  96. }
  97. /* Print ASN.1 of a file containing BER/DER data.
  98. *
  99. * @param [in] fp File pointer to read from.
  100. * @return 0 on success.
  101. * @return 1 on failure.
  102. */
  103. static int PrintDer(FILE* fp)
  104. {
  105. int ret = 0;
  106. word32 len = 0;
  107. unsigned char* data = NULL;
  108. /* Load DER/BER file. */
  109. if (asn1App_ReadFile(fp, &data, &len) != 0) {
  110. ret = 1;
  111. }
  112. if ((ret == 0) && (data != NULL)) {
  113. /* Print DER/BER. */
  114. ret = wc_Asn1_PrintAll(&asn1, &opts, data, len);
  115. /* Dispose of buffer. */
  116. free(data);
  117. }
  118. return ret;
  119. }
  120. /* Print ASN.1 of a file containing Base64 encoding of BER/DER data.
  121. *
  122. * @param [in] fp File pointer to read from.
  123. * @return 0 on success.
  124. * @return 1 on failure.
  125. */
  126. static int PrintBase64(FILE* fp)
  127. {
  128. int ret = 0;
  129. word32 len = 0;
  130. unsigned char* data = NULL;
  131. /* Load Base64 encoded file. */
  132. if (asn1App_ReadFile(fp, &data, &len) != 0) {
  133. ret = 1;
  134. }
  135. if ((ret == 0) && (data != NULL)) {
  136. /* Decode Base64. */
  137. if (Base64_Decode(data, len, data, &len) != 0) {
  138. fprintf(stderr, "Invalid Base64 encoding\n");
  139. ret = 1;
  140. }
  141. if (ret == 0) {
  142. /* Print DER/BER. */
  143. ret = wc_Asn1_PrintAll(&asn1, &opts, data, len);
  144. }
  145. /* Dispose of buffer. */
  146. free(data);
  147. }
  148. return ret;
  149. }
  150. /* Find the next PEM block.
  151. *
  152. * @param [in] data PEM data.
  153. * @param [in] offset Offset into data to start looking.
  154. * @param [in] len Length of PEM data.
  155. * @param [out] start Start of Base64 encoding.
  156. * @param [out] end End of Base64 encoding.
  157. */
  158. static int FindPem(unsigned char* data, word32 offset, word32 len,
  159. word32* start, word32* end)
  160. {
  161. int ret = 0;
  162. word32 i;
  163. word32 j;
  164. /* Find header. */
  165. for (i = offset; i < len; i++) {
  166. if ((data[i] == '-') &&
  167. (strncmp((char*)data + i, "-----BEGIN", 10) == 0)) {
  168. break;
  169. }
  170. }
  171. if (i == len) {
  172. /* Got to end without finding PEM header. */
  173. fprintf(stderr, "No PEM header found\n");
  174. ret = 1;
  175. }
  176. if (ret == 0) {
  177. /* Confirm header. */
  178. for (i += 10; i < len; i++) {
  179. if ((data[i] == '-') &&
  180. (strncmp((char*)data + i, "-----", 5) == 0)) {
  181. break;
  182. }
  183. }
  184. if (i == len) {
  185. /* Got to end without finding rest of PEM header. */
  186. fprintf(stderr, "Invalid PEM header\n");
  187. ret = 1;
  188. }
  189. }
  190. if (ret == 0) {
  191. /* Find footer. */
  192. i += 6;
  193. for (j = i + 1; j < len; j++) {
  194. if ((data[j] == '-') &&
  195. (strncmp((char*)data + j, "-----END", 8) == 0)) {
  196. break;
  197. }
  198. }
  199. if (j == len) {
  200. /* Got to end without finding PEM footer. */
  201. fprintf(stderr, "No PEM footer found\n");
  202. ret = 1;
  203. }
  204. }
  205. if (ret == 0) {
  206. /* Return start and end indices. */
  207. *start = i;
  208. *end = j;
  209. }
  210. return ret;
  211. }
  212. /* Print ASN.1 of file containing PEM.
  213. *
  214. * Only one block is printed.
  215. *
  216. * @param [in] fp File pointer to read from.
  217. * @param [in] pem_skip Number of PEM blocks to skip.
  218. * @return 0 on success.
  219. * @return 1 on failure.
  220. */
  221. static int PrintPem(FILE* fp, int pem_skip)
  222. {
  223. int ret = 0;
  224. unsigned char* data = NULL;
  225. word32 len = 0;
  226. /* Load PEM file. */
  227. if (asn1App_ReadFile(fp, &data, &len) != 0) {
  228. ret = 1;
  229. }
  230. if ((ret == 0) && (data != NULL)) {
  231. word32 i = 0;
  232. word32 j = 0;
  233. /* Find PEM blocks and skip number requested. */
  234. do {
  235. /* Find start and end of PEM Base64 data. */
  236. ret = FindPem(data, j, len, &i, &j);
  237. } while ((ret == 0) && ((pem_skip--) != 0));
  238. /* Decode data between header and footer. */
  239. if ((ret == 0) && (Base64_Decode(data + i, j - i, data, &len) != 0)) {
  240. fprintf(stderr, "Invalid Base64 encoding\n");
  241. ret = 1;
  242. }
  243. if (ret == 0) {
  244. /* Print DER/BER. */
  245. ret = wc_Asn1_PrintAll(&asn1, &opts, data, len);
  246. }
  247. /* Dispose of buffer. */
  248. free(data);
  249. }
  250. return ret;
  251. }
  252. /* Usage lines to show. */
  253. const char* usage[] = {
  254. "asn1 [OPTION]... [FILE]",
  255. "Display a human-readable version of a DER/BER encoding.",
  256. "",
  257. "Options:",
  258. " -?, --help display this help and exit",
  259. " -b, --branch draw branches before tag name",
  260. " -B, --base64 file contents are Base64 encoded",
  261. " -d, --dump show all ASN.1 item data as a hex dump",
  262. " -h, --headers show all ASN.1 item headers as a hex dump",
  263. " -i, --indent indent tag name with depth",
  264. " -l, --length LEN display length bytes of data",
  265. " -n, --no-text do not show data as text",
  266. " -N, --no-dump-text do not show data as a hex dump text",
  267. " -o, --offset OFFSET start decoding from offset",
  268. " -O, --oid show wolfSSL OID value in text",
  269. " -p, --pem file contents are PEM",
  270. " -s, --skip-pem NUM number of PEM blocks to skip",
  271. };
  272. /* Number of usage lines. */
  273. #define USAGE_SZ ((int)(sizeof(usage) / sizeof(*usage)))
  274. /* Print out usage lines.
  275. */
  276. static void Usage(void)
  277. {
  278. int i;
  279. for (i = 0; i < USAGE_SZ; i++) {
  280. printf("%s\n", usage[i]);
  281. }
  282. }
  283. /* Main entry of ASN.1 printing program.
  284. *
  285. * @param [in] argc Count of command line arguments.
  286. * @param [in] argv Command line arguments.
  287. * @return 0 on success.
  288. * @return 1 on failure.
  289. */
  290. int main(int argc, char* argv[])
  291. {
  292. int ret = 0;
  293. /* Default to reading STDIN. */
  294. FILE* fp = stdin;
  295. int file_format = FORMAT_DER;
  296. word32 indent = 0;
  297. int pem_skip = 0;
  298. /* Reset options. */
  299. (void)wc_Asn1PrintOptions_Init(&opts);
  300. /* Skip over program name. */
  301. argc--;
  302. argv++;
  303. while (argc > 0) {
  304. /* Show branches instead of indenting. */
  305. if ((strcmp(argv[0], "-b") == 0) ||
  306. (strcmp(argv[0], "--branch") == 0)) {
  307. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_DRAW_BRANCH, 1);
  308. }
  309. /* File is Base64 encoded data. */
  310. else if ((strcmp(argv[0], "-b64") == 0) ||
  311. (strcmp(argv[0], "--base64") == 0)) {
  312. file_format = FORMAT_BASE64;
  313. }
  314. /* Dump all ASN.1 item data. */
  315. else if ((strcmp(argv[0], "-d") == 0) ||
  316. (strcmp(argv[0], "--dump") == 0)) {
  317. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_SHOW_DATA, 1);
  318. }
  319. /* Dump ASN.1 item headers. */
  320. else if ((strcmp(argv[0], "-h") == 0) ||
  321. (strcmp(argv[0], "--headers") == 0)) {
  322. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_SHOW_HEADER_DATA, 1);
  323. }
  324. /* Indent to text to indicate depth. */
  325. else if ((strcmp(argv[0], "-i") == 0) ||
  326. (strcmp(argv[0], "--indent") == 0)) {
  327. indent++;
  328. if (indent > 15) {
  329. }
  330. }
  331. /* Only parse the specified length of DER/BER data. */
  332. else if ((strcmp(argv[0], "-l") == 0) ||
  333. (strcmp(argv[0], "--length") == 0)) {
  334. if (argc == 1) {
  335. printf("Missing length value\n");
  336. return 1;
  337. }
  338. argc--;
  339. argv++;
  340. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_LENGTH,
  341. (word32)atoi(argv[0]));
  342. }
  343. /* Do not show text representations of ASN.1 item data. */
  344. else if ((strcmp(argv[0], "-n") == 0) ||
  345. (strcmp(argv[0], "--no-text") == 0)) {
  346. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_SHOW_NO_TEXT, 1);
  347. }
  348. /* Do not show hex dump text representations of ASN.1 item data. */
  349. else if ((strcmp(argv[0], "-N") == 0) ||
  350. (strcmp(argv[0], "--no-dump-text") == 0)) {
  351. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_SHOW_NO_DUMP_TEXT, 1);
  352. }
  353. /* Offset into DER/BER to start decoding from. */
  354. else if ((strcmp(argv[0], "-o") == 0) ||
  355. (strcmp(argv[0], "--offset") == 0)) {
  356. if (argc == 1) {
  357. fprintf(stderr, "Missing offset value\n");
  358. return 1;
  359. }
  360. argc--;
  361. argv++;
  362. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_OFFSET,
  363. (word32)atoi(argv[0]));
  364. }
  365. /* Show wolfSSL OID value for all OBJECT_IDs. */
  366. else if ((strcmp(argv[0], "-O") == 0) ||
  367. (strcmp(argv[0], "--oid") == 0)) {
  368. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_SHOW_OID, 1);
  369. }
  370. /* File contains PEM blocks. */
  371. else if ((strcmp(argv[0], "-p") == 0) ||
  372. (strcmp(argv[0], "--pem") == 0)) {
  373. file_format = FORMAT_PEM;
  374. }
  375. /* Skip a number of PEM blocks. */
  376. else if ((strcmp(argv[0], "-s") == 0) ||
  377. (strcmp(argv[0], "--skip-pem") == 0)) {
  378. if (argc == 1) {
  379. fprintf(stderr, "Missing number of PEM blocks to skip\n");
  380. return 1;
  381. }
  382. argc--;
  383. argv++;
  384. pem_skip = atoi(argv[0]);
  385. if ((pem_skip < 0) || (pem_skip > 15)) {
  386. fprintf(stderr, "Skip value out of range: %d\n", pem_skip);
  387. return 1;
  388. }
  389. }
  390. /* Display help/usage. */
  391. else if ((strcmp(argv[0], "-?") == 0) ||
  392. (strcmp(argv[0], "--help") == 0)) {
  393. Usage();
  394. return 0;
  395. }
  396. /* Unknown option detection. */
  397. else if (argv[0][0] == '-') {
  398. fprintf(stderr, "Bad option: %s\n", argv[0]);
  399. Usage();
  400. return 1;
  401. }
  402. else {
  403. /* Name of file to read. */
  404. fp = fopen(argv[0], "r");
  405. if (fp == NULL) {
  406. fprintf(stderr, "File not able to be read: %s\n", argv[0]);
  407. return 1;
  408. }
  409. }
  410. /* Move on to next command line argument. */
  411. argc--;
  412. argv++;
  413. }
  414. wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_INDENT, indent);
  415. (void)wc_Asn1_Init(&asn1);
  416. (void)wc_Asn1_SetFile(&asn1, stdout);
  417. /* Process file based on type. */
  418. if (file_format == FORMAT_DER) {
  419. ret = PrintDer(fp);
  420. }
  421. else if (file_format == FORMAT_BASE64) {
  422. ret = PrintBase64(fp);
  423. }
  424. else if (file_format == FORMAT_PEM) {
  425. ret = PrintPem(fp, pem_skip);
  426. }
  427. if (ret != 0) {
  428. fprintf(stderr, "%s\n", wc_GetErrorString(ret));
  429. }
  430. return (ret == 0) ? 0 : 1;
  431. }
  432. #else
  433. /* Main entry of ASN.1 printing program.
  434. *
  435. * @param [in] argc Count of command line arguments.
  436. * @param [in] argv Command line arguments.
  437. * @return 0 on success.
  438. * @return 1 on failure.
  439. */
  440. int main(int argc, char* argv[])
  441. {
  442. (void)argc;
  443. (void)argv;
  444. fprintf(stderr, "ASN.1 Parsing and Printing or file system not compiled"
  445. " in.\n");
  446. return 0;
  447. }
  448. #endif /* WOLFSSL_ASN_PRINT && !defined(NO_FILESYSTEM)*/