pem2der.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Convert PEM to DER
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
  22. #include "mbedtls/error.h"
  23. #include "mbedtls/base64.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #endif
  28. #define DFL_FILENAME "file.pem"
  29. #define DFL_OUTPUT_FILENAME "file.der"
  30. #define USAGE \
  31. "\n usage: pem2der param=<>...\n" \
  32. "\n acceptable parameters:\n" \
  33. " filename=%%s default: file.pem\n" \
  34. " output_file=%%s default: file.der\n" \
  35. "\n"
  36. #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
  37. int main(void)
  38. {
  39. mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
  40. mbedtls_exit(0);
  41. }
  42. #else
  43. /*
  44. * global options
  45. */
  46. struct options {
  47. const char *filename; /* filename of the input file */
  48. const char *output_file; /* where to store the output */
  49. } opt;
  50. int convert_pem_to_der(const unsigned char *input, size_t ilen,
  51. unsigned char *output, size_t *olen)
  52. {
  53. int ret;
  54. const unsigned char *s1, *s2, *end = input + ilen;
  55. size_t len = 0;
  56. s1 = (unsigned char *) strstr((const char *) input, "-----BEGIN");
  57. if (s1 == NULL) {
  58. return -1;
  59. }
  60. s2 = (unsigned char *) strstr((const char *) input, "-----END");
  61. if (s2 == NULL) {
  62. return -1;
  63. }
  64. s1 += 10;
  65. while (s1 < end && *s1 != '-') {
  66. s1++;
  67. }
  68. while (s1 < end && *s1 == '-') {
  69. s1++;
  70. }
  71. if (*s1 == '\r') {
  72. s1++;
  73. }
  74. if (*s1 == '\n') {
  75. s1++;
  76. }
  77. if (s2 <= s1 || s2 > end) {
  78. return -1;
  79. }
  80. ret = mbedtls_base64_decode(NULL, 0, &len, (const unsigned char *) s1, s2 - s1);
  81. if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
  82. return ret;
  83. }
  84. if (len > *olen) {
  85. return -1;
  86. }
  87. if ((ret = mbedtls_base64_decode(output, len, &len, (const unsigned char *) s1,
  88. s2 - s1)) != 0) {
  89. return ret;
  90. }
  91. *olen = len;
  92. return 0;
  93. }
  94. /*
  95. * Load all data from a file into a given buffer.
  96. */
  97. static int load_file(const char *path, unsigned char **buf, size_t *n)
  98. {
  99. FILE *f;
  100. long size;
  101. if ((f = fopen(path, "rb")) == NULL) {
  102. return -1;
  103. }
  104. fseek(f, 0, SEEK_END);
  105. if ((size = ftell(f)) == -1) {
  106. fclose(f);
  107. return -1;
  108. }
  109. fseek(f, 0, SEEK_SET);
  110. *n = (size_t) size;
  111. if (*n + 1 == 0 ||
  112. (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
  113. fclose(f);
  114. return -1;
  115. }
  116. if (fread(*buf, 1, *n, f) != *n) {
  117. fclose(f);
  118. free(*buf);
  119. *buf = NULL;
  120. return -1;
  121. }
  122. fclose(f);
  123. (*buf)[*n] = '\0';
  124. return 0;
  125. }
  126. /*
  127. * Write buffer to a file
  128. */
  129. static int write_file(const char *path, unsigned char *buf, size_t n)
  130. {
  131. FILE *f;
  132. if ((f = fopen(path, "wb")) == NULL) {
  133. return -1;
  134. }
  135. if (fwrite(buf, 1, n, f) != n) {
  136. fclose(f);
  137. return -1;
  138. }
  139. fclose(f);
  140. return 0;
  141. }
  142. int main(int argc, char *argv[])
  143. {
  144. int ret = 1;
  145. int exit_code = MBEDTLS_EXIT_FAILURE;
  146. unsigned char *pem_buffer = NULL;
  147. unsigned char der_buffer[4096];
  148. char buf[1024];
  149. size_t pem_size, der_size = sizeof(der_buffer);
  150. int i;
  151. char *p, *q;
  152. /*
  153. * Set to sane values
  154. */
  155. memset(buf, 0, sizeof(buf));
  156. memset(der_buffer, 0, sizeof(der_buffer));
  157. if (argc < 2) {
  158. usage:
  159. mbedtls_printf(USAGE);
  160. goto exit;
  161. }
  162. opt.filename = DFL_FILENAME;
  163. opt.output_file = DFL_OUTPUT_FILENAME;
  164. for (i = 1; i < argc; i++) {
  165. p = argv[i];
  166. if ((q = strchr(p, '=')) == NULL) {
  167. goto usage;
  168. }
  169. *q++ = '\0';
  170. if (strcmp(p, "filename") == 0) {
  171. opt.filename = q;
  172. } else if (strcmp(p, "output_file") == 0) {
  173. opt.output_file = q;
  174. } else {
  175. goto usage;
  176. }
  177. }
  178. /*
  179. * 1.1. Load the PEM file
  180. */
  181. mbedtls_printf("\n . Loading the PEM file ...");
  182. fflush(stdout);
  183. ret = load_file(opt.filename, &pem_buffer, &pem_size);
  184. if (ret != 0) {
  185. #ifdef MBEDTLS_ERROR_C
  186. mbedtls_strerror(ret, buf, 1024);
  187. #endif
  188. mbedtls_printf(" failed\n ! load_file returned %d - %s\n\n", ret, buf);
  189. goto exit;
  190. }
  191. mbedtls_printf(" ok\n");
  192. /*
  193. * 1.2. Convert from PEM to DER
  194. */
  195. mbedtls_printf(" . Converting from PEM to DER ...");
  196. fflush(stdout);
  197. if ((ret = convert_pem_to_der(pem_buffer, pem_size, der_buffer, &der_size)) != 0) {
  198. #ifdef MBEDTLS_ERROR_C
  199. mbedtls_strerror(ret, buf, 1024);
  200. #endif
  201. mbedtls_printf(" failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf);
  202. goto exit;
  203. }
  204. mbedtls_printf(" ok\n");
  205. /*
  206. * 1.3. Write the DER file
  207. */
  208. mbedtls_printf(" . Writing the DER file ...");
  209. fflush(stdout);
  210. ret = write_file(opt.output_file, der_buffer, der_size);
  211. if (ret != 0) {
  212. #ifdef MBEDTLS_ERROR_C
  213. mbedtls_strerror(ret, buf, 1024);
  214. #endif
  215. mbedtls_printf(" failed\n ! write_file returned %d - %s\n\n", ret, buf);
  216. goto exit;
  217. }
  218. mbedtls_printf(" ok\n");
  219. exit_code = MBEDTLS_EXIT_SUCCESS;
  220. exit:
  221. free(pem_buffer);
  222. mbedtls_exit(exit_code);
  223. }
  224. #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */