pem2der.c 6.6 KB

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