pem.c 14 KB

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