pem.c 15 KB

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