pem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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
  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_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  27. #include "mbedtls/pem.h"
  28. #include "mbedtls/base64.h"
  29. #include "mbedtls/des.h"
  30. #include "mbedtls/aes.h"
  31. #include "mbedtls/md5.h"
  32. #include "mbedtls/cipher.h"
  33. #include "mbedtls/platform_util.h"
  34. #include <string.h>
  35. #if defined(MBEDTLS_PLATFORM_C)
  36. #include "mbedtls/platform.h"
  37. #else
  38. #include <stdlib.h>
  39. #define mbedtls_calloc calloc
  40. #define mbedtls_free free
  41. #endif
  42. #if defined(MBEDTLS_PEM_PARSE_C)
  43. void mbedtls_pem_init( mbedtls_pem_context *ctx )
  44. {
  45. memset( ctx, 0, sizeof( mbedtls_pem_context ) );
  46. }
  47. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  48. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  49. /*
  50. * Read a 16-byte hex string and convert it to binary
  51. */
  52. static int pem_get_iv( const unsigned char *s, unsigned char *iv,
  53. size_t iv_len )
  54. {
  55. size_t i, j, k;
  56. memset( iv, 0, iv_len );
  57. for( i = 0; i < iv_len * 2; i++, s++ )
  58. {
  59. if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
  60. if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
  61. if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
  62. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  63. k = ( ( i & 1 ) != 0 ) ? j : j << 4;
  64. iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
  65. }
  66. return( 0 );
  67. }
  68. static int pem_pbkdf1( unsigned char *key, size_t keylen,
  69. unsigned char *iv,
  70. const unsigned char *pwd, size_t pwdlen )
  71. {
  72. mbedtls_md5_context md5_ctx;
  73. unsigned char md5sum[16];
  74. size_t use_len;
  75. int ret;
  76. mbedtls_md5_init( &md5_ctx );
  77. /*
  78. * key[ 0..15] = MD5(pwd || IV)
  79. */
  80. if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
  81. goto exit;
  82. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
  83. goto exit;
  84. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
  85. goto exit;
  86. if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
  87. goto exit;
  88. if( keylen <= 16 )
  89. {
  90. memcpy( key, md5sum, keylen );
  91. goto exit;
  92. }
  93. memcpy( key, md5sum, 16 );
  94. /*
  95. * key[16..23] = MD5(key[ 0..15] || pwd || IV])
  96. */
  97. if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
  98. goto exit;
  99. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 )
  100. goto exit;
  101. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
  102. goto exit;
  103. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
  104. goto exit;
  105. if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
  106. goto exit;
  107. use_len = 16;
  108. if( keylen < 32 )
  109. use_len = keylen - 16;
  110. memcpy( key + 16, md5sum, use_len );
  111. exit:
  112. mbedtls_md5_free( &md5_ctx );
  113. mbedtls_platform_zeroize( md5sum, 16 );
  114. return( ret );
  115. }
  116. #if defined(MBEDTLS_DES_C)
  117. /*
  118. * Decrypt with DES-CBC, using PBKDF1 for key derivation
  119. */
  120. static int pem_des_decrypt( unsigned char des_iv[8],
  121. unsigned char *buf, size_t buflen,
  122. const unsigned char *pwd, size_t pwdlen )
  123. {
  124. mbedtls_des_context des_ctx;
  125. unsigned char des_key[8];
  126. int ret;
  127. mbedtls_des_init( &des_ctx );
  128. if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
  129. goto exit;
  130. if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
  131. goto exit;
  132. ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
  133. des_iv, buf, buf );
  134. exit:
  135. mbedtls_des_free( &des_ctx );
  136. mbedtls_platform_zeroize( des_key, 8 );
  137. return( ret );
  138. }
  139. /*
  140. * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
  141. */
  142. static int pem_des3_decrypt( unsigned char des3_iv[8],
  143. unsigned char *buf, size_t buflen,
  144. const unsigned char *pwd, size_t pwdlen )
  145. {
  146. mbedtls_des3_context des3_ctx;
  147. unsigned char des3_key[24];
  148. int ret;
  149. mbedtls_des3_init( &des3_ctx );
  150. if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
  151. goto exit;
  152. if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
  153. goto exit;
  154. ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
  155. des3_iv, buf, buf );
  156. exit:
  157. mbedtls_des3_free( &des3_ctx );
  158. mbedtls_platform_zeroize( des3_key, 24 );
  159. return( ret );
  160. }
  161. #endif /* MBEDTLS_DES_C */
  162. #if defined(MBEDTLS_AES_C)
  163. /*
  164. * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
  165. */
  166. static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
  167. unsigned char *buf, size_t buflen,
  168. const unsigned char *pwd, size_t pwdlen )
  169. {
  170. mbedtls_aes_context aes_ctx;
  171. unsigned char aes_key[32];
  172. int ret;
  173. mbedtls_aes_init( &aes_ctx );
  174. if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
  175. goto exit;
  176. if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
  177. goto exit;
  178. ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
  179. aes_iv, buf, buf );
  180. exit:
  181. mbedtls_aes_free( &aes_ctx );
  182. mbedtls_platform_zeroize( aes_key, keylen );
  183. return( ret );
  184. }
  185. #endif /* MBEDTLS_AES_C */
  186. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  187. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  188. int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
  189. const unsigned char *data, const unsigned char *pwd,
  190. size_t pwdlen, size_t *use_len )
  191. {
  192. int ret, enc;
  193. size_t len;
  194. unsigned char *buf;
  195. const unsigned char *s1, *s2, *end;
  196. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  197. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  198. unsigned char pem_iv[16];
  199. mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
  200. #else
  201. ((void) pwd);
  202. ((void) pwdlen);
  203. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  204. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  205. if( ctx == NULL )
  206. return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
  207. s1 = (unsigned char *) strstr( (const char *) data, header );
  208. if( s1 == NULL )
  209. return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  210. s2 = (unsigned char *) strstr( (const char *) data, footer );
  211. if( s2 == NULL || s2 <= s1 )
  212. return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  213. s1 += strlen( header );
  214. if( *s1 == ' ' ) s1++;
  215. if( *s1 == '\r' ) s1++;
  216. if( *s1 == '\n' ) s1++;
  217. else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  218. end = s2;
  219. end += strlen( footer );
  220. if( *end == ' ' ) end++;
  221. if( *end == '\r' ) end++;
  222. if( *end == '\n' ) end++;
  223. *use_len = end - data;
  224. enc = 0;
  225. if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
  226. {
  227. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  228. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  229. enc++;
  230. s1 += 22;
  231. if( *s1 == '\r' ) s1++;
  232. if( *s1 == '\n' ) s1++;
  233. else return( MBEDTLS_ERR_PEM_INVALID_DATA );
  234. #if defined(MBEDTLS_DES_C)
  235. if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
  236. {
  237. enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
  238. s1 += 23;
  239. if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
  240. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  241. s1 += 16;
  242. }
  243. else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
  244. {
  245. enc_alg = MBEDTLS_CIPHER_DES_CBC;
  246. s1 += 18;
  247. if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
  248. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  249. s1 += 16;
  250. }
  251. #endif /* MBEDTLS_DES_C */
  252. #if defined(MBEDTLS_AES_C)
  253. if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
  254. {
  255. if( s2 - s1 < 22 )
  256. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  257. else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
  258. enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
  259. else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
  260. enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
  261. else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
  262. enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
  263. else
  264. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  265. s1 += 22;
  266. if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
  267. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  268. s1 += 32;
  269. }
  270. #endif /* MBEDTLS_AES_C */
  271. if( enc_alg == MBEDTLS_CIPHER_NONE )
  272. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  273. if( *s1 == '\r' ) s1++;
  274. if( *s1 == '\n' ) s1++;
  275. else return( MBEDTLS_ERR_PEM_INVALID_DATA );
  276. #else
  277. return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
  278. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  279. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  280. }
  281. if( s1 >= s2 )
  282. return( MBEDTLS_ERR_PEM_INVALID_DATA );
  283. ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
  284. if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
  285. return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
  286. if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
  287. return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
  288. if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
  289. {
  290. mbedtls_platform_zeroize( buf, len );
  291. mbedtls_free( buf );
  292. return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
  293. }
  294. if( enc != 0 )
  295. {
  296. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  297. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  298. if( pwd == NULL )
  299. {
  300. mbedtls_platform_zeroize( buf, len );
  301. mbedtls_free( buf );
  302. return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
  303. }
  304. ret = 0;
  305. #if defined(MBEDTLS_DES_C)
  306. if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
  307. ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
  308. else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
  309. ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
  310. #endif /* MBEDTLS_DES_C */
  311. #if defined(MBEDTLS_AES_C)
  312. if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
  313. ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
  314. else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
  315. ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
  316. else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
  317. ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
  318. #endif /* MBEDTLS_AES_C */
  319. if( ret != 0 )
  320. {
  321. mbedtls_free( buf );
  322. return( ret );
  323. }
  324. /*
  325. * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
  326. * length bytes (allow 4 to be sure) in all known use cases.
  327. *
  328. * Use that as a heuristic to try to detect password mismatches.
  329. */
  330. if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
  331. {
  332. mbedtls_platform_zeroize( buf, len );
  333. mbedtls_free( buf );
  334. return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
  335. }
  336. #else
  337. mbedtls_platform_zeroize( buf, len );
  338. mbedtls_free( buf );
  339. return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
  340. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  341. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  342. }
  343. ctx->buf = buf;
  344. ctx->buflen = len;
  345. return( 0 );
  346. }
  347. void mbedtls_pem_free( mbedtls_pem_context *ctx )
  348. {
  349. if ( ctx->buf != NULL )
  350. {
  351. mbedtls_platform_zeroize( ctx->buf, ctx->buflen );
  352. mbedtls_free( ctx->buf );
  353. }
  354. mbedtls_free( ctx->info );
  355. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) );
  356. }
  357. #endif /* MBEDTLS_PEM_PARSE_C */
  358. #if defined(MBEDTLS_PEM_WRITE_C)
  359. int mbedtls_pem_write_buffer( const char *header, const char *footer,
  360. const unsigned char *der_data, size_t der_len,
  361. unsigned char *buf, size_t buf_len, size_t *olen )
  362. {
  363. int ret;
  364. unsigned char *encode_buf = NULL, *c, *p = buf;
  365. size_t len = 0, use_len, add_len = 0;
  366. mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
  367. add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
  368. if( use_len + add_len > buf_len )
  369. {
  370. *olen = use_len + add_len;
  371. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  372. }
  373. if( use_len != 0 &&
  374. ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
  375. return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
  376. if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
  377. der_len ) ) != 0 )
  378. {
  379. mbedtls_free( encode_buf );
  380. return( ret );
  381. }
  382. memcpy( p, header, strlen( header ) );
  383. p += strlen( header );
  384. c = encode_buf;
  385. while( use_len )
  386. {
  387. len = ( use_len > 64 ) ? 64 : use_len;
  388. memcpy( p, c, len );
  389. use_len -= len;
  390. p += len;
  391. c += len;
  392. *p++ = '\n';
  393. }
  394. memcpy( p, footer, strlen( footer ) );
  395. p += strlen( footer );
  396. *p++ = '\0';
  397. *olen = p - buf;
  398. mbedtls_free( encode_buf );
  399. return( 0 );
  400. }
  401. #endif /* MBEDTLS_PEM_WRITE_C */
  402. #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */