pkcs12.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * PKCS#12 Personal Information Exchange Syntax
  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. /*
  47. * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
  48. *
  49. * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
  50. * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
  51. */
  52. #if !defined(MBEDTLS_CONFIG_FILE)
  53. #include "mbedtls/config.h"
  54. #else
  55. #include MBEDTLS_CONFIG_FILE
  56. #endif
  57. #if defined(MBEDTLS_PKCS12_C)
  58. #include "mbedtls/pkcs12.h"
  59. #include "mbedtls/asn1.h"
  60. #include "mbedtls/cipher.h"
  61. #include "mbedtls/platform_util.h"
  62. #include <string.h>
  63. #if defined(MBEDTLS_ARC4_C)
  64. #include "mbedtls/arc4.h"
  65. #endif
  66. #if defined(MBEDTLS_DES_C)
  67. #include "mbedtls/des.h"
  68. #endif
  69. #if defined(MBEDTLS_ASN1_PARSE_C)
  70. static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
  71. mbedtls_asn1_buf *salt, int *iterations )
  72. {
  73. int ret;
  74. unsigned char **p = &params->p;
  75. const unsigned char *end = params->p + params->len;
  76. /*
  77. * pkcs-12PbeParams ::= SEQUENCE {
  78. * salt OCTET STRING,
  79. * iterations INTEGER
  80. * }
  81. *
  82. */
  83. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  84. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  85. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  86. if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  87. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  88. salt->p = *p;
  89. *p += salt->len;
  90. if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
  91. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  92. if( *p != end )
  93. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  94. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  95. return( 0 );
  96. }
  97. #define PKCS12_MAX_PWDLEN 128
  98. static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
  99. const unsigned char *pwd, size_t pwdlen,
  100. unsigned char *key, size_t keylen,
  101. unsigned char *iv, size_t ivlen )
  102. {
  103. int ret, iterations = 0;
  104. mbedtls_asn1_buf salt;
  105. size_t i;
  106. unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
  107. if( pwdlen > PKCS12_MAX_PWDLEN )
  108. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  109. memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
  110. memset( &unipwd, 0, sizeof(unipwd) );
  111. if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
  112. &iterations ) ) != 0 )
  113. return( ret );
  114. for( i = 0; i < pwdlen; i++ )
  115. unipwd[i * 2 + 1] = pwd[i];
  116. if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
  117. salt.p, salt.len, md_type,
  118. MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
  119. {
  120. return( ret );
  121. }
  122. if( iv == NULL || ivlen == 0 )
  123. return( 0 );
  124. if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
  125. salt.p, salt.len, md_type,
  126. MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
  127. {
  128. return( ret );
  129. }
  130. return( 0 );
  131. }
  132. #undef PKCS12_MAX_PWDLEN
  133. int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
  134. const unsigned char *pwd, size_t pwdlen,
  135. const unsigned char *data, size_t len,
  136. unsigned char *output )
  137. {
  138. #if !defined(MBEDTLS_ARC4_C)
  139. ((void) pbe_params);
  140. ((void) mode);
  141. ((void) pwd);
  142. ((void) pwdlen);
  143. ((void) data);
  144. ((void) len);
  145. ((void) output);
  146. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  147. #else
  148. int ret;
  149. unsigned char key[16];
  150. mbedtls_arc4_context ctx;
  151. ((void) mode);
  152. mbedtls_arc4_init( &ctx );
  153. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
  154. pwd, pwdlen,
  155. key, 16, NULL, 0 ) ) != 0 )
  156. {
  157. return( ret );
  158. }
  159. mbedtls_arc4_setup( &ctx, key, 16 );
  160. if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
  161. goto exit;
  162. exit:
  163. mbedtls_platform_zeroize( key, sizeof( key ) );
  164. mbedtls_arc4_free( &ctx );
  165. return( ret );
  166. #endif /* MBEDTLS_ARC4_C */
  167. }
  168. int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
  169. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  170. const unsigned char *pwd, size_t pwdlen,
  171. const unsigned char *data, size_t len,
  172. unsigned char *output )
  173. {
  174. int ret, keylen = 0;
  175. unsigned char key[32];
  176. unsigned char iv[16];
  177. const mbedtls_cipher_info_t *cipher_info;
  178. mbedtls_cipher_context_t cipher_ctx;
  179. size_t olen = 0;
  180. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  181. if( cipher_info == NULL )
  182. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  183. keylen = cipher_info->key_bitlen / 8;
  184. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
  185. key, keylen,
  186. iv, cipher_info->iv_size ) ) != 0 )
  187. {
  188. return( ret );
  189. }
  190. mbedtls_cipher_init( &cipher_ctx );
  191. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
  192. goto exit;
  193. if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
  194. goto exit;
  195. if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
  196. goto exit;
  197. if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
  198. goto exit;
  199. if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
  200. output, &olen ) ) != 0 )
  201. {
  202. goto exit;
  203. }
  204. if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
  205. ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
  206. exit:
  207. mbedtls_platform_zeroize( key, sizeof( key ) );
  208. mbedtls_platform_zeroize( iv, sizeof( iv ) );
  209. mbedtls_cipher_free( &cipher_ctx );
  210. return( ret );
  211. }
  212. #endif /* MBEDTLS_ASN1_PARSE_C */
  213. static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
  214. const unsigned char *filler, size_t fill_len )
  215. {
  216. unsigned char *p = data;
  217. size_t use_len;
  218. while( data_len > 0 )
  219. {
  220. use_len = ( data_len > fill_len ) ? fill_len : data_len;
  221. memcpy( p, filler, use_len );
  222. p += use_len;
  223. data_len -= use_len;
  224. }
  225. }
  226. int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
  227. const unsigned char *pwd, size_t pwdlen,
  228. const unsigned char *salt, size_t saltlen,
  229. mbedtls_md_type_t md_type, int id, int iterations )
  230. {
  231. int ret;
  232. unsigned int j;
  233. unsigned char diversifier[128];
  234. unsigned char salt_block[128], pwd_block[128], hash_block[128];
  235. unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
  236. unsigned char *p;
  237. unsigned char c;
  238. size_t hlen, use_len, v, i;
  239. const mbedtls_md_info_t *md_info;
  240. mbedtls_md_context_t md_ctx;
  241. // This version only allows max of 64 bytes of password or salt
  242. if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
  243. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  244. md_info = mbedtls_md_info_from_type( md_type );
  245. if( md_info == NULL )
  246. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  247. mbedtls_md_init( &md_ctx );
  248. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  249. return( ret );
  250. hlen = mbedtls_md_get_size( md_info );
  251. if( hlen <= 32 )
  252. v = 64;
  253. else
  254. v = 128;
  255. memset( diversifier, (unsigned char) id, v );
  256. pkcs12_fill_buffer( salt_block, v, salt, saltlen );
  257. pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
  258. p = data;
  259. while( datalen > 0 )
  260. {
  261. // Calculate hash( diversifier || salt_block || pwd_block )
  262. if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
  263. goto exit;
  264. if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
  265. goto exit;
  266. if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
  267. goto exit;
  268. if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
  269. goto exit;
  270. if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
  271. goto exit;
  272. // Perform remaining ( iterations - 1 ) recursive hash calculations
  273. for( i = 1; i < (size_t) iterations; i++ )
  274. {
  275. if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
  276. goto exit;
  277. }
  278. use_len = ( datalen > hlen ) ? hlen : datalen;
  279. memcpy( p, hash_output, use_len );
  280. datalen -= use_len;
  281. p += use_len;
  282. if( datalen == 0 )
  283. break;
  284. // Concatenating copies of hash_output into hash_block (B)
  285. pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
  286. // B += 1
  287. for( i = v; i > 0; i-- )
  288. if( ++hash_block[i - 1] != 0 )
  289. break;
  290. // salt_block += B
  291. c = 0;
  292. for( i = v; i > 0; i-- )
  293. {
  294. j = salt_block[i - 1] + hash_block[i - 1] + c;
  295. c = (unsigned char) (j >> 8);
  296. salt_block[i - 1] = j & 0xFF;
  297. }
  298. // pwd_block += B
  299. c = 0;
  300. for( i = v; i > 0; i-- )
  301. {
  302. j = pwd_block[i - 1] + hash_block[i - 1] + c;
  303. c = (unsigned char) (j >> 8);
  304. pwd_block[i - 1] = j & 0xFF;
  305. }
  306. }
  307. ret = 0;
  308. exit:
  309. mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) );
  310. mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) );
  311. mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) );
  312. mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) );
  313. mbedtls_md_free( &md_ctx );
  314. return( ret );
  315. }
  316. #endif /* MBEDTLS_PKCS12_C */