pem2der.c 6.5 KB

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