1 /* 2 * Privacy Enhanced Mail (PEM) decoding 3 * 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 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 * This file is part of mbed TLS (https://tls.mbed.org) 47 */ 48 49 #if !defined(MBEDTLS_CONFIG_FILE) 50 #include "mbedtls/config.h" 51 #else 52 #include MBEDTLS_CONFIG_FILE 53 #endif 54 55 #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C) 56 57 #include "mbedtls/pem.h" 58 #include "mbedtls/base64.h" 59 #include "mbedtls/des.h" 60 #include "mbedtls/aes.h" 61 #include "mbedtls/md5.h" 62 #include "mbedtls/cipher.h" 63 64 #include <string.h> 65 66 #if defined(MBEDTLS_PLATFORM_C) 67 #include "mbedtls/platform.h" 68 #else 69 #include <stdlib.h> 70 #define mbedtls_calloc calloc 71 #define mbedtls_free free 72 #endif 73 74 #if defined(MBEDTLS_PEM_PARSE_C) 75 /* Implementation that should never be optimized out by the compiler */ 76 static void mbedtls_zeroize( void *v, size_t n ) { 77 volatile unsigned char *p = v; while( n-- ) *p++ = 0; 78 } 79 80 void mbedtls_pem_init( mbedtls_pem_context *ctx ) 81 { 82 memset( ctx, 0, sizeof( mbedtls_pem_context ) ); 83 } 84 85 #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \ 86 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) ) 87 /* 88 * Read a 16-byte hex string and convert it to binary 89 */ 90 static int pem_get_iv( const unsigned char *s, unsigned char *iv, 91 size_t iv_len ) 92 { 93 size_t i, j, k; 94 95 memset( iv, 0, iv_len ); 96 97 for( i = 0; i < iv_len * 2; i++, s++ ) 98 { 99 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else 100 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else 101 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else 102 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); 103 104 k = ( ( i & 1 ) != 0 ) ? j : j << 4; 105 106 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k ); 107 } 108 109 return( 0 ); 110 } 111 112 static int pem_pbkdf1( unsigned char *key, size_t keylen, 113 unsigned char *iv, 114 const unsigned char *pwd, size_t pwdlen ) 115 { 116 mbedtls_md5_context md5_ctx; 117 unsigned char md5sum[16]; 118 size_t use_len; 119 int ret; 120 121 mbedtls_md5_init( &md5_ctx ); 122 123 /* 124 * key[ 0..15] = MD5(pwd || IV) 125 */ 126 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 ) 127 goto exit; 128 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 ) 129 goto exit; 130 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 ) 131 goto exit; 132 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 ) 133 goto exit; 134 135 if( keylen <= 16 ) 136 { 137 memcpy( key, md5sum, keylen ); 138 goto exit; 139 } 140 141 memcpy( key, md5sum, 16 ); 142 143 /* 144 * key[16..23] = MD5(key[ 0..15] || pwd || IV]) 145 */ 146 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 ) 147 goto exit; 148 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 ) 149 goto exit; 150 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 ) 151 goto exit; 152 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 ) 153 goto exit; 154 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 ) 155 goto exit; 156 157 use_len = 16; 158 if( keylen < 32 ) 159 use_len = keylen - 16; 160 161 memcpy( key + 16, md5sum, use_len ); 162 163 exit: 164 mbedtls_md5_free( &md5_ctx ); 165 mbedtls_zeroize( md5sum, 16 ); 166 167 return( ret ); 168 } 169 170 #if defined(MBEDTLS_DES_C) 171 /* 172 * Decrypt with DES-CBC, using PBKDF1 for key derivation 173 */ 174 static int pem_des_decrypt( unsigned char des_iv[8], 175 unsigned char *buf, size_t buflen, 176 const unsigned char *pwd, size_t pwdlen ) 177 { 178 mbedtls_des_context des_ctx; 179 unsigned char des_key[8]; 180 int ret; 181 182 mbedtls_des_init( &des_ctx ); 183 184 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 ) 185 goto exit; 186 187 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 ) 188 goto exit; 189 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen, 190 des_iv, buf, buf ); 191 192 exit: 193 mbedtls_des_free( &des_ctx ); 194 mbedtls_zeroize( des_key, 8 ); 195 196 return( ret ); 197 } 198 199 /* 200 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation 201 */ 202 static int pem_des3_decrypt( unsigned char des3_iv[8], 203 unsigned char *buf, size_t buflen, 204 const unsigned char *pwd, size_t pwdlen ) 205 { 206 mbedtls_des3_context des3_ctx; 207 unsigned char des3_key[24]; 208 int ret; 209 210 mbedtls_des3_init( &des3_ctx ); 211 212 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 ) 213 goto exit; 214 215 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 ) 216 goto exit; 217 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen, 218 des3_iv, buf, buf ); 219 220 exit: 221 mbedtls_des3_free( &des3_ctx ); 222 mbedtls_zeroize( des3_key, 24 ); 223 224 return( ret ); 225 } 226 #endif /* MBEDTLS_DES_C */ 227 228 #if defined(MBEDTLS_AES_C) 229 /* 230 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation 231 */ 232 static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen, 233 unsigned char *buf, size_t buflen, 234 const unsigned char *pwd, size_t pwdlen ) 235 { 236 mbedtls_aes_context aes_ctx; 237 unsigned char aes_key[32]; 238 int ret; 239 240 mbedtls_aes_init( &aes_ctx ); 241 242 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 ) 243 goto exit; 244 245 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 ) 246 goto exit; 247 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen, 248 aes_iv, buf, buf ); 249 250 exit: 251 mbedtls_aes_free( &aes_ctx ); 252 mbedtls_zeroize( aes_key, keylen ); 253 254 return( ret ); 255 } 256 #endif /* MBEDTLS_AES_C */ 257 258 #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && 259 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ 260 261 int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer, 262 const unsigned char *data, const unsigned char *pwd, 263 size_t pwdlen, size_t *use_len ) 264 { 265 int ret, enc; 266 size_t len; 267 unsigned char *buf; 268 const unsigned char *s1, *s2, *end; 269 #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \ 270 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) ) 271 unsigned char pem_iv[16]; 272 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE; 273 #else 274 ((void) pwd); 275 ((void) pwdlen); 276 #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && 277 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ 278 279 if( ctx == NULL ) 280 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA ); 281 282 s1 = (unsigned char *) strstr( (const char *) data, header ); 283 284 if( s1 == NULL ) 285 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); 286 287 s2 = (unsigned char *) strstr( (const char *) data, footer ); 288 289 if( s2 == NULL || s2 <= s1 ) 290 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); 291 292 s1 += strlen( header ); 293 if( *s1 == ' ' ) s1++; 294 if( *s1 == '\r' ) s1++; 295 if( *s1 == '\n' ) s1++; 296 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); 297 298 end = s2; 299 end += strlen( footer ); 300 if( *end == ' ' ) end++; 301 if( *end == '\r' ) end++; 302 if( *end == '\n' ) end++; 303 *use_len = end - data; 304 305 enc = 0; 306 307 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) 308 { 309 #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \ 310 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) ) 311 enc++; 312 313 s1 += 22; 314 if( *s1 == '\r' ) s1++; 315 if( *s1 == '\n' ) s1++; 316 else return( MBEDTLS_ERR_PEM_INVALID_DATA ); 317 318 319 #if defined(MBEDTLS_DES_C) 320 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 ) 321 { 322 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC; 323 324 s1 += 23; 325 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 ) 326 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); 327 328 s1 += 16; 329 } 330 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 ) 331 { 332 enc_alg = MBEDTLS_CIPHER_DES_CBC; 333 334 s1 += 18; 335 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 ) 336 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); 337 338 s1 += 16; 339 } 340 #endif /* MBEDTLS_DES_C */ 341 342 #if defined(MBEDTLS_AES_C) 343 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) 344 { 345 if( s2 - s1 < 22 ) 346 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); 347 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) 348 enc_alg = MBEDTLS_CIPHER_AES_128_CBC; 349 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 ) 350 enc_alg = MBEDTLS_CIPHER_AES_192_CBC; 351 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 ) 352 enc_alg = MBEDTLS_CIPHER_AES_256_CBC; 353 else 354 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); 355 356 s1 += 22; 357 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 ) 358 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); 359 360 s1 += 32; 361 } 362 #endif /* MBEDTLS_AES_C */ 363 364 if( enc_alg == MBEDTLS_CIPHER_NONE ) 365 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); 366 367 if( *s1 == '\r' ) s1++; 368 if( *s1 == '\n' ) s1++; 369 else return( MBEDTLS_ERR_PEM_INVALID_DATA ); 370 #else 371 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE ); 372 #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && 373 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ 374 } 375 376 if( s1 >= s2 ) 377 return( MBEDTLS_ERR_PEM_INVALID_DATA ); 378 379 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 ); 380 381 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER ) 382 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret ); 383 384 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL ) 385 return( MBEDTLS_ERR_PEM_ALLOC_FAILED ); 386 387 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 ) 388 { 389 mbedtls_zeroize( buf, len ); 390 mbedtls_free( buf ); 391 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret ); 392 } 393 394 if( enc != 0 ) 395 { 396 #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \ 397 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) ) 398 if( pwd == NULL ) 399 { 400 mbedtls_zeroize( buf, len ); 401 mbedtls_free( buf ); 402 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED ); 403 } 404 405 ret = 0; 406 407 #if defined(MBEDTLS_DES_C) 408 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC ) 409 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen ); 410 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC ) 411 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen ); 412 #endif /* MBEDTLS_DES_C */ 413 414 #if defined(MBEDTLS_AES_C) 415 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC ) 416 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen ); 417 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC ) 418 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen ); 419 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC ) 420 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen ); 421 #endif /* MBEDTLS_AES_C */ 422 423 if( ret != 0 ) 424 { 425 mbedtls_free( buf ); 426 return( ret ); 427 } 428 429 /* 430 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3 431 * length bytes (allow 4 to be sure) in all known use cases. 432 * 433 * Use that as heurisitic to try detecting password mismatchs. 434 */ 435 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 ) 436 { 437 mbedtls_zeroize( buf, len ); 438 mbedtls_free( buf ); 439 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH ); 440 } 441 #else 442 mbedtls_zeroize( buf, len ); 443 mbedtls_free( buf ); 444 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE ); 445 #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && 446 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ 447 } 448 449 ctx->buf = buf; 450 ctx->buflen = len; 451 452 return( 0 ); 453 } 454 455 void mbedtls_pem_free( mbedtls_pem_context *ctx ) 456 { 457 if( ctx->buf != NULL ) 458 mbedtls_zeroize( ctx->buf, ctx->buflen ); 459 mbedtls_free( ctx->buf ); 460 mbedtls_free( ctx->info ); 461 462 mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) ); 463 } 464 #endif /* MBEDTLS_PEM_PARSE_C */ 465 466 #if defined(MBEDTLS_PEM_WRITE_C) 467 int mbedtls_pem_write_buffer( const char *header, const char *footer, 468 const unsigned char *der_data, size_t der_len, 469 unsigned char *buf, size_t buf_len, size_t *olen ) 470 { 471 int ret; 472 unsigned char *encode_buf = NULL, *c, *p = buf; 473 size_t len = 0, use_len, add_len = 0; 474 475 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len ); 476 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1; 477 478 if( use_len + add_len > buf_len ) 479 { 480 *olen = use_len + add_len; 481 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); 482 } 483 484 if( use_len != 0 && 485 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) ) 486 return( MBEDTLS_ERR_PEM_ALLOC_FAILED ); 487 488 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data, 489 der_len ) ) != 0 ) 490 { 491 mbedtls_free( encode_buf ); 492 return( ret ); 493 } 494 495 memcpy( p, header, strlen( header ) ); 496 p += strlen( header ); 497 c = encode_buf; 498 499 while( use_len ) 500 { 501 len = ( use_len > 64 ) ? 64 : use_len; 502 memcpy( p, c, len ); 503 use_len -= len; 504 p += len; 505 c += len; 506 *p++ = '\n'; 507 } 508 509 memcpy( p, footer, strlen( footer ) ); 510 p += strlen( footer ); 511 512 *p++ = '\0'; 513 *olen = p - buf; 514 515 mbedtls_free( encode_buf ); 516 return( 0 ); 517 } 518 #endif /* MBEDTLS_PEM_WRITE_C */ 519 #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */ 520