Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Convert PEM to DER |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 26 | #include "polarssl/config.h" |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 32 | #include "polarssl/error.h" |
| 33 | #include "polarssl/base64.h" |
| 34 | |
| 35 | #define DFL_FILENAME "file.pem" |
| 36 | #define DFL_OUTPUT_FILENAME "file.der" |
| 37 | |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 38 | #if !defined(POLARSSL_BASE64_C) || !defined(POLARSSL_FS_IO) |
| 39 | int main( int argc, char *argv[] ) |
| 40 | { |
| 41 | ((void) argc); |
| 42 | ((void) argv); |
| 43 | |
| 44 | printf("POLARSSL_BASE64_C and/or POLARSSL_FS_IO not defined.\n"); |
| 45 | return( 0 ); |
| 46 | } |
| 47 | #else |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 48 | /* |
| 49 | * global options |
| 50 | */ |
| 51 | struct options |
| 52 | { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame^] | 53 | const char *filename; /* filename of the input file */ |
| 54 | const char *output_file; /* where to store the output */ |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 55 | } opt; |
| 56 | |
| 57 | int convert_pem_to_der( const unsigned char *input, size_t ilen, |
| 58 | unsigned char *output, size_t *olen ) |
| 59 | { |
| 60 | int ret; |
| 61 | const unsigned char *s1, *s2, *end = input + ilen; |
| 62 | size_t len = 0; |
| 63 | |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame^] | 64 | s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 65 | if( s1 == NULL ) |
| 66 | return( -1 ); |
| 67 | |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame^] | 68 | s2 = (unsigned char *) strstr( (const char *) input, "-----END" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 69 | if( s2 == NULL ) |
| 70 | return( -1 ); |
| 71 | |
| 72 | s1 += 10; |
| 73 | while( s1 < end && *s1 != '-' ) |
| 74 | s1++; |
| 75 | while( s1 < end && *s1 == '-' ) |
| 76 | s1++; |
| 77 | if( *s1 == '\r' ) s1++; |
| 78 | if( *s1 == '\n' ) s1++; |
| 79 | |
| 80 | if( s2 <= s1 || s2 > end ) |
| 81 | return( -1 ); |
| 82 | |
| 83 | ret = base64_decode( NULL, &len, (const unsigned char *) s1, s2 - s1 ); |
| 84 | if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER ) |
| 85 | return( ret ); |
| 86 | |
| 87 | if( len > *olen ) |
| 88 | return( -1 ); |
| 89 | |
| 90 | if( ( ret = base64_decode( output, &len, (const unsigned char *) s1, |
| 91 | s2 - s1 ) ) != 0 ) |
| 92 | { |
| 93 | return( ret ); |
| 94 | } |
| 95 | |
| 96 | *olen = len; |
| 97 | |
| 98 | return( 0 ); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Load all data from a file into a given buffer. |
| 103 | */ |
| 104 | static int load_file( const char *path, unsigned char **buf, size_t *n ) |
| 105 | { |
| 106 | FILE *f; |
| 107 | long size; |
| 108 | |
| 109 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
| 110 | return( -1 ); |
| 111 | |
| 112 | fseek( f, 0, SEEK_END ); |
| 113 | if( ( size = ftell( f ) ) == -1 ) |
| 114 | { |
| 115 | fclose( f ); |
| 116 | return( -1 ); |
| 117 | } |
| 118 | fseek( f, 0, SEEK_SET ); |
| 119 | |
| 120 | *n = (size_t) size; |
| 121 | |
| 122 | if( *n + 1 == 0 || |
| 123 | ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL ) |
| 124 | { |
| 125 | fclose( f ); |
| 126 | return( -1 ); |
| 127 | } |
| 128 | |
| 129 | if( fread( *buf, 1, *n, f ) != *n ) |
| 130 | { |
| 131 | fclose( f ); |
| 132 | free( *buf ); |
| 133 | return( -1 ); |
| 134 | } |
| 135 | |
| 136 | fclose( f ); |
| 137 | |
| 138 | (*buf)[*n] = '\0'; |
| 139 | |
| 140 | return( 0 ); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Write buffer to a file |
| 145 | */ |
| 146 | static int write_file( const char *path, unsigned char *buf, size_t n ) |
| 147 | { |
| 148 | FILE *f; |
| 149 | |
| 150 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
| 151 | return( -1 ); |
| 152 | |
| 153 | if( fwrite( buf, 1, n, f ) != n ) |
| 154 | { |
| 155 | fclose( f ); |
| 156 | return( -1 ); |
| 157 | } |
| 158 | |
| 159 | fclose( f ); |
| 160 | return( 0 ); |
| 161 | } |
| 162 | |
| 163 | #define USAGE \ |
| 164 | "\n usage: pem2der param=<>...\n" \ |
| 165 | "\n acceptable parameters:\n" \ |
| 166 | " filename=%%s default: file.pem\n" \ |
| 167 | " output_file=%%s default: file.der\n" \ |
| 168 | "\n" |
| 169 | |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 170 | int main( int argc, char *argv[] ) |
| 171 | { |
| 172 | int ret = 0; |
| 173 | unsigned char *pem_buffer = NULL; |
| 174 | unsigned char der_buffer[4096]; |
| 175 | char buf[1024]; |
| 176 | size_t pem_size, der_size = sizeof(der_buffer); |
| 177 | int i, j, n; |
| 178 | char *p, *q; |
| 179 | |
| 180 | /* |
| 181 | * Set to sane values |
| 182 | */ |
| 183 | memset( buf, 0, sizeof(buf) ); |
| 184 | memset( der_buffer, 0, sizeof(der_buffer) ); |
| 185 | |
| 186 | if( argc == 0 ) |
| 187 | { |
| 188 | usage: |
| 189 | printf( USAGE ); |
| 190 | goto exit; |
| 191 | } |
| 192 | |
| 193 | opt.filename = DFL_FILENAME; |
| 194 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 195 | |
| 196 | for( i = 1; i < argc; i++ ) |
| 197 | { |
| 198 | |
| 199 | p = argv[i]; |
| 200 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 201 | goto usage; |
| 202 | *q++ = '\0'; |
| 203 | |
| 204 | n = strlen( p ); |
| 205 | for( j = 0; j < n; j++ ) |
| 206 | { |
| 207 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 208 | argv[i][j] |= 0x20; |
| 209 | } |
| 210 | |
| 211 | if( strcmp( p, "filename" ) == 0 ) |
| 212 | opt.filename = q; |
| 213 | else if( strcmp( p, "output_file" ) == 0 ) |
| 214 | opt.output_file = q; |
| 215 | else |
| 216 | goto usage; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * 1.1. Load the PEM file |
| 221 | */ |
| 222 | printf( "\n . Loading the PEM file ..." ); |
| 223 | fflush( stdout ); |
| 224 | |
| 225 | ret = load_file( opt.filename, &pem_buffer, &pem_size ); |
| 226 | |
| 227 | if( ret != 0 ) |
| 228 | { |
| 229 | #ifdef POLARSSL_ERROR_C |
| 230 | error_strerror( ret, buf, 1024 ); |
| 231 | #endif |
| 232 | printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf ); |
| 233 | goto exit; |
| 234 | } |
| 235 | |
| 236 | printf( " ok\n" ); |
| 237 | |
| 238 | /* |
| 239 | * 1.2. Convert from PEM to DER |
| 240 | */ |
| 241 | printf( " . Converting from PEM to DER ..." ); |
| 242 | fflush( stdout ); |
| 243 | |
| 244 | if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 ) |
| 245 | { |
| 246 | #ifdef POLARSSL_ERROR_C |
| 247 | error_strerror( ret, buf, 1024 ); |
| 248 | #endif |
| 249 | printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf ); |
| 250 | goto exit; |
| 251 | } |
| 252 | |
| 253 | printf( " ok\n" ); |
| 254 | |
| 255 | /* |
| 256 | * 1.3. Write the DER file |
| 257 | */ |
| 258 | printf( " . Writing the DER file ..." ); |
| 259 | fflush( stdout ); |
| 260 | |
| 261 | ret = write_file( opt.output_file, der_buffer, der_size ); |
| 262 | |
| 263 | if( ret != 0 ) |
| 264 | { |
| 265 | #ifdef POLARSSL_ERROR_C |
| 266 | error_strerror( ret, buf, 1024 ); |
| 267 | #endif |
| 268 | printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf ); |
| 269 | goto exit; |
| 270 | } |
| 271 | |
| 272 | printf( " ok\n" ); |
| 273 | |
| 274 | exit: |
| 275 | free( pem_buffer ); |
| 276 | |
| 277 | #if defined(_WIN32) |
| 278 | printf( " + Press Enter to exit this program.\n" ); |
| 279 | fflush( stdout ); getchar(); |
| 280 | #endif |
| 281 | |
| 282 | return( ret ); |
| 283 | } |
| 284 | #endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */ |