pem2der.c 7.6 KB

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