pem.c 13 KB

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