pem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Privacy Enhanced Mail (PEM) decoding
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  29. #include "mbedtls/pem.h"
  30. #include "mbedtls/base64.h"
  31. #include "mbedtls/des.h"
  32. #include "mbedtls/aes.h"
  33. #include "mbedtls/md5.h"
  34. #include "mbedtls/cipher.h"
  35. #include <string.h>
  36. #if defined(MBEDTLS_PLATFORM_C)
  37. #include "mbedtls/platform.h"
  38. #else
  39. #include <stdlib.h>
  40. #define mbedtls_calloc calloc
  41. #define mbedtls_free free
  42. #endif
  43. #if defined(MBEDTLS_PEM_PARSE_C)
  44. /* Implementation that should never be optimized out by the compiler */
  45. static void mbedtls_zeroize( void *v, size_t n ) {
  46. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  47. }
  48. void mbedtls_pem_init( mbedtls_pem_context *ctx )
  49. {
  50. memset( ctx, 0, sizeof( mbedtls_pem_context ) );
  51. }
  52. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  53. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  54. /*
  55. * Read a 16-byte hex string and convert it to binary
  56. */
  57. static int pem_get_iv( const unsigned char *s, unsigned char *iv,
  58. size_t iv_len )
  59. {
  60. size_t i, j, k;
  61. memset( iv, 0, iv_len );
  62. for( i = 0; i < iv_len * 2; i++, s++ )
  63. {
  64. if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
  65. if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
  66. if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
  67. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  68. k = ( ( i & 1 ) != 0 ) ? j : j << 4;
  69. iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
  70. }
  71. return( 0 );
  72. }
  73. static void pem_pbkdf1( unsigned char *key, size_t keylen,
  74. unsigned char *iv,
  75. const unsigned char *pwd, size_t pwdlen )
  76. {
  77. mbedtls_md5_context md5_ctx;
  78. unsigned char md5sum[16];
  79. size_t use_len;
  80. mbedtls_md5_init( &md5_ctx );
  81. /*
  82. * key[ 0..15] = MD5(pwd || IV)
  83. */
  84. mbedtls_md5_starts( &md5_ctx );
  85. mbedtls_md5_update( &md5_ctx, pwd, pwdlen );
  86. mbedtls_md5_update( &md5_ctx, iv, 8 );
  87. mbedtls_md5_finish( &md5_ctx, md5sum );
  88. if( keylen <= 16 )
  89. {
  90. memcpy( key, md5sum, keylen );
  91. mbedtls_md5_free( &md5_ctx );
  92. mbedtls_zeroize( md5sum, 16 );
  93. return;
  94. }
  95. memcpy( key, md5sum, 16 );
  96. /*
  97. * key[16..23] = MD5(key[ 0..15] || pwd || IV])
  98. */
  99. mbedtls_md5_starts( &md5_ctx );
  100. mbedtls_md5_update( &md5_ctx, md5sum, 16 );
  101. mbedtls_md5_update( &md5_ctx, pwd, pwdlen );
  102. mbedtls_md5_update( &md5_ctx, iv, 8 );
  103. mbedtls_md5_finish( &md5_ctx, md5sum );
  104. use_len = 16;
  105. if( keylen < 32 )
  106. use_len = keylen - 16;
  107. memcpy( key + 16, md5sum, use_len );
  108. mbedtls_md5_free( &md5_ctx );
  109. mbedtls_zeroize( md5sum, 16 );
  110. }
  111. #if defined(MBEDTLS_DES_C)
  112. /*
  113. * Decrypt with DES-CBC, using PBKDF1 for key derivation
  114. */
  115. static void 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. mbedtls_des_init( &des_ctx );
  122. pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen );
  123. mbedtls_des_setkey_dec( &des_ctx, des_key );
  124. mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
  125. des_iv, buf, buf );
  126. mbedtls_des_free( &des_ctx );
  127. mbedtls_zeroize( des_key, 8 );
  128. }
  129. /*
  130. * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
  131. */
  132. static void pem_des3_decrypt( unsigned char des3_iv[8],
  133. unsigned char *buf, size_t buflen,
  134. const unsigned char *pwd, size_t pwdlen )
  135. {
  136. mbedtls_des3_context des3_ctx;
  137. unsigned char des3_key[24];
  138. mbedtls_des3_init( &des3_ctx );
  139. pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen );
  140. mbedtls_des3_set3key_dec( &des3_ctx, des3_key );
  141. mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
  142. des3_iv, buf, buf );
  143. mbedtls_des3_free( &des3_ctx );
  144. mbedtls_zeroize( des3_key, 24 );
  145. }
  146. #endif /* MBEDTLS_DES_C */
  147. #if defined(MBEDTLS_AES_C)
  148. /*
  149. * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
  150. */
  151. static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
  152. unsigned char *buf, size_t buflen,
  153. const unsigned char *pwd, size_t pwdlen )
  154. {
  155. mbedtls_aes_context aes_ctx;
  156. unsigned char aes_key[32];
  157. mbedtls_aes_init( &aes_ctx );
  158. pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen );
  159. mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 );
  160. mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
  161. aes_iv, buf, buf );
  162. mbedtls_aes_free( &aes_ctx );
  163. mbedtls_zeroize( aes_key, keylen );
  164. }
  165. #endif /* MBEDTLS_AES_C */
  166. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  167. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  168. int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
  169. const unsigned char *data, const unsigned char *pwd,
  170. size_t pwdlen, size_t *use_len )
  171. {
  172. int ret, enc;
  173. size_t len;
  174. unsigned char *buf;
  175. const unsigned char *s1, *s2, *end;
  176. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  177. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  178. unsigned char pem_iv[16];
  179. mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
  180. #else
  181. ((void) pwd);
  182. ((void) pwdlen);
  183. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  184. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  185. if( ctx == NULL )
  186. return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
  187. s1 = (unsigned char *) strstr( (const char *) data, header );
  188. if( s1 == NULL )
  189. return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  190. s2 = (unsigned char *) strstr( (const char *) data, footer );
  191. if( s2 == NULL || s2 <= s1 )
  192. return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  193. s1 += strlen( header );
  194. if( *s1 == ' ' ) s1++;
  195. if( *s1 == '\r' ) s1++;
  196. if( *s1 == '\n' ) s1++;
  197. else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  198. end = s2;
  199. end += strlen( footer );
  200. if( *end == ' ' ) end++;
  201. if( *end == '\r' ) end++;
  202. if( *end == '\n' ) end++;
  203. *use_len = end - data;
  204. enc = 0;
  205. if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
  206. {
  207. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  208. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  209. enc++;
  210. s1 += 22;
  211. if( *s1 == '\r' ) s1++;
  212. if( *s1 == '\n' ) s1++;
  213. else return( MBEDTLS_ERR_PEM_INVALID_DATA );
  214. #if defined(MBEDTLS_DES_C)
  215. if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
  216. {
  217. enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
  218. s1 += 23;
  219. if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
  220. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  221. s1 += 16;
  222. }
  223. else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
  224. {
  225. enc_alg = MBEDTLS_CIPHER_DES_CBC;
  226. s1 += 18;
  227. if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
  228. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  229. s1 += 16;
  230. }
  231. #endif /* MBEDTLS_DES_C */
  232. #if defined(MBEDTLS_AES_C)
  233. if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
  234. {
  235. if( s2 - s1 < 22 )
  236. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  237. else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
  238. enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
  239. else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
  240. enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
  241. else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
  242. enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
  243. else
  244. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  245. s1 += 22;
  246. if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
  247. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  248. s1 += 32;
  249. }
  250. #endif /* MBEDTLS_AES_C */
  251. if( enc_alg == MBEDTLS_CIPHER_NONE )
  252. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  253. if( *s1 == '\r' ) s1++;
  254. if( *s1 == '\n' ) s1++;
  255. else return( MBEDTLS_ERR_PEM_INVALID_DATA );
  256. #else
  257. return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
  258. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  259. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  260. }
  261. if( s1 >= s2 )
  262. return( MBEDTLS_ERR_PEM_INVALID_DATA );
  263. ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
  264. if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
  265. return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
  266. if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
  267. return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
  268. if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
  269. {
  270. mbedtls_free( buf );
  271. return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
  272. }
  273. if( enc != 0 )
  274. {
  275. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  276. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  277. if( pwd == NULL )
  278. {
  279. mbedtls_free( buf );
  280. return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
  281. }
  282. #if defined(MBEDTLS_DES_C)
  283. if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
  284. pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
  285. else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
  286. pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
  287. #endif /* MBEDTLS_DES_C */
  288. #if defined(MBEDTLS_AES_C)
  289. if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
  290. pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
  291. else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
  292. pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
  293. else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
  294. pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
  295. #endif /* MBEDTLS_AES_C */
  296. /*
  297. * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
  298. * length bytes (allow 4 to be sure) in all known use cases.
  299. *
  300. * Use that as heurisitic to try detecting password mismatchs.
  301. */
  302. if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
  303. {
  304. mbedtls_free( buf );
  305. return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
  306. }
  307. #else
  308. mbedtls_free( buf );
  309. return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
  310. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  311. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  312. }
  313. ctx->buf = buf;
  314. ctx->buflen = len;
  315. return( 0 );
  316. }
  317. void mbedtls_pem_free( mbedtls_pem_context *ctx )
  318. {
  319. mbedtls_free( ctx->buf );
  320. mbedtls_free( ctx->info );
  321. mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) );
  322. }
  323. #endif /* MBEDTLS_PEM_PARSE_C */
  324. #if defined(MBEDTLS_PEM_WRITE_C)
  325. int mbedtls_pem_write_buffer( const char *header, const char *footer,
  326. const unsigned char *der_data, size_t der_len,
  327. unsigned char *buf, size_t buf_len, size_t *olen )
  328. {
  329. int ret;
  330. unsigned char *encode_buf, *c, *p = buf;
  331. size_t len = 0, use_len, add_len = 0;
  332. mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
  333. add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
  334. if( use_len + add_len > buf_len )
  335. {
  336. *olen = use_len + add_len;
  337. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  338. }
  339. if( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL )
  340. return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
  341. if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
  342. der_len ) ) != 0 )
  343. {
  344. mbedtls_free( encode_buf );
  345. return( ret );
  346. }
  347. memcpy( p, header, strlen( header ) );
  348. p += strlen( header );
  349. c = encode_buf;
  350. while( use_len )
  351. {
  352. len = ( use_len > 64 ) ? 64 : use_len;
  353. memcpy( p, c, len );
  354. use_len -= len;
  355. p += len;
  356. c += len;
  357. *p++ = '\n';
  358. }
  359. memcpy( p, footer, strlen( footer ) );
  360. p += strlen( footer );
  361. *p++ = '\0';
  362. *olen = p - buf;
  363. mbedtls_free( encode_buf );
  364. return( 0 );
  365. }
  366. #endif /* MBEDTLS_PEM_WRITE_C */
  367. #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */