pem2der.c 6.4 KB

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