pem2der.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Convert PEM to DER
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define mbedtls_free free
  32. #define mbedtls_calloc calloc
  33. #define mbedtls_printf printf
  34. #define mbedtls_exit exit
  35. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  36. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  37. #endif /* MBEDTLS_PLATFORM_C */
  38. #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
  39. #include "mbedtls/error.h"
  40. #include "mbedtls/base64.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #endif
  45. #define DFL_FILENAME "file.pem"
  46. #define DFL_OUTPUT_FILENAME "file.der"
  47. #define USAGE \
  48. "\n usage: pem2der param=<>...\n" \
  49. "\n acceptable parameters:\n" \
  50. " filename=%%s default: file.pem\n" \
  51. " output_file=%%s default: file.der\n" \
  52. "\n"
  53. #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
  54. int main( void )
  55. {
  56. mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
  57. return( 0 );
  58. }
  59. #else
  60. /*
  61. * global options
  62. */
  63. struct options
  64. {
  65. const char *filename; /* filename of the input file */
  66. const char *output_file; /* where to store the output */
  67. } opt;
  68. int convert_pem_to_der( const unsigned char *input, size_t ilen,
  69. unsigned char *output, size_t *olen )
  70. {
  71. int ret;
  72. const unsigned char *s1, *s2, *end = input + ilen;
  73. size_t len = 0;
  74. s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
  75. if( s1 == NULL )
  76. return( -1 );
  77. s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
  78. if( s2 == NULL )
  79. return( -1 );
  80. s1 += 10;
  81. while( s1 < end && *s1 != '-' )
  82. s1++;
  83. while( s1 < end && *s1 == '-' )
  84. s1++;
  85. if( *s1 == '\r' ) s1++;
  86. if( *s1 == '\n' ) s1++;
  87. if( s2 <= s1 || s2 > end )
  88. return( -1 );
  89. ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
  90. if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
  91. return( ret );
  92. if( len > *olen )
  93. return( -1 );
  94. if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
  95. s2 - s1 ) ) != 0 )
  96. {
  97. return( ret );
  98. }
  99. *olen = len;
  100. return( 0 );
  101. }
  102. /*
  103. * Load all data from a file into a given buffer.
  104. */
  105. static int load_file( const char *path, unsigned char **buf, size_t *n )
  106. {
  107. FILE *f;
  108. long size;
  109. if( ( f = fopen( path, "rb" ) ) == NULL )
  110. return( -1 );
  111. fseek( f, 0, SEEK_END );
  112. if( ( size = ftell( f ) ) == -1 )
  113. {
  114. fclose( f );
  115. return( -1 );
  116. }
  117. fseek( f, 0, SEEK_SET );
  118. *n = (size_t) size;
  119. if( *n + 1 == 0 ||
  120. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  121. {
  122. fclose( f );
  123. return( -1 );
  124. }
  125. if( fread( *buf, 1, *n, f ) != *n )
  126. {
  127. fclose( f );
  128. free( *buf );
  129. *buf = NULL;
  130. return( -1 );
  131. }
  132. fclose( f );
  133. (*buf)[*n] = '\0';
  134. return( 0 );
  135. }
  136. /*
  137. * Write buffer to a file
  138. */
  139. static int write_file( const char *path, unsigned char *buf, size_t n )
  140. {
  141. FILE *f;
  142. if( ( f = fopen( path, "wb" ) ) == NULL )
  143. return( -1 );
  144. if( fwrite( buf, 1, n, f ) != n )
  145. {
  146. fclose( f );
  147. return( -1 );
  148. }
  149. fclose( f );
  150. return( 0 );
  151. }
  152. int main( int argc, char *argv[] )
  153. {
  154. int ret = 1;
  155. int exit_code = MBEDTLS_EXIT_FAILURE;
  156. unsigned char *pem_buffer = NULL;
  157. unsigned char der_buffer[4096];
  158. char buf[1024];
  159. size_t pem_size, der_size = sizeof(der_buffer);
  160. int i;
  161. char *p, *q;
  162. /*
  163. * Set to sane values
  164. */
  165. memset( buf, 0, sizeof(buf) );
  166. memset( der_buffer, 0, sizeof(der_buffer) );
  167. if( argc == 0 )
  168. {
  169. usage:
  170. mbedtls_printf( USAGE );
  171. goto exit;
  172. }
  173. opt.filename = DFL_FILENAME;
  174. opt.output_file = DFL_OUTPUT_FILENAME;
  175. for( i = 1; i < argc; i++ )
  176. {
  177. p = argv[i];
  178. if( ( q = strchr( p, '=' ) ) == NULL )
  179. goto usage;
  180. *q++ = '\0';
  181. if( strcmp( p, "filename" ) == 0 )
  182. opt.filename = q;
  183. else if( strcmp( p, "output_file" ) == 0 )
  184. opt.output_file = q;
  185. else
  186. goto usage;
  187. }
  188. /*
  189. * 1.1. Load the PEM file
  190. */
  191. mbedtls_printf( "\n . Loading the PEM file ..." );
  192. fflush( stdout );
  193. ret = load_file( opt.filename, &pem_buffer, &pem_size );
  194. if( ret != 0 )
  195. {
  196. #ifdef MBEDTLS_ERROR_C
  197. mbedtls_strerror( ret, buf, 1024 );
  198. #endif
  199. mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
  200. goto exit;
  201. }
  202. mbedtls_printf( " ok\n" );
  203. /*
  204. * 1.2. Convert from PEM to DER
  205. */
  206. mbedtls_printf( " . Converting from PEM to DER ..." );
  207. fflush( stdout );
  208. if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
  209. {
  210. #ifdef MBEDTLS_ERROR_C
  211. mbedtls_strerror( ret, buf, 1024 );
  212. #endif
  213. mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
  214. goto exit;
  215. }
  216. mbedtls_printf( " ok\n" );
  217. /*
  218. * 1.3. Write the DER file
  219. */
  220. mbedtls_printf( " . Writing the DER file ..." );
  221. fflush( stdout );
  222. ret = write_file( opt.output_file, der_buffer, der_size );
  223. if( ret != 0 )
  224. {
  225. #ifdef MBEDTLS_ERROR_C
  226. mbedtls_strerror( ret, buf, 1024 );
  227. #endif
  228. mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
  229. goto exit;
  230. }
  231. mbedtls_printf( " ok\n" );
  232. exit_code = MBEDTLS_EXIT_SUCCESS;
  233. exit:
  234. free( pem_buffer );
  235. #if defined(_WIN32)
  236. mbedtls_printf( " + Press Enter to exit this program.\n" );
  237. fflush( stdout ); getchar();
  238. #endif
  239. return( exit_code );
  240. }
  241. #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */