pkparse.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*
  2. * Public Key layer for parsing key files and structures
  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_PK_PARSE_C)
  27. #include "mbedtls/pk.h"
  28. #include "mbedtls/asn1.h"
  29. #include "mbedtls/oid.h"
  30. #include "mbedtls/platform_util.h"
  31. #include <string.h>
  32. #if defined(MBEDTLS_RSA_C)
  33. #include "mbedtls/rsa.h"
  34. #endif
  35. #if defined(MBEDTLS_ECP_C)
  36. #include "mbedtls/ecp.h"
  37. #endif
  38. #if defined(MBEDTLS_ECDSA_C)
  39. #include "mbedtls/ecdsa.h"
  40. #endif
  41. #if defined(MBEDTLS_PEM_PARSE_C)
  42. #include "mbedtls/pem.h"
  43. #endif
  44. #if defined(MBEDTLS_PKCS5_C)
  45. #include "mbedtls/pkcs5.h"
  46. #endif
  47. #if defined(MBEDTLS_PKCS12_C)
  48. #include "mbedtls/pkcs12.h"
  49. #endif
  50. #if defined(MBEDTLS_PLATFORM_C)
  51. #include "mbedtls/platform.h"
  52. #else
  53. #include <stdlib.h>
  54. #define mbedtls_calloc calloc
  55. #define mbedtls_free free
  56. #endif
  57. /* Parameter validation macros based on platform_util.h */
  58. #define PK_VALIDATE_RET( cond ) \
  59. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
  60. #define PK_VALIDATE( cond ) \
  61. MBEDTLS_INTERNAL_VALIDATE( cond )
  62. #if defined(MBEDTLS_FS_IO)
  63. /*
  64. * Load all data from a file into a given buffer.
  65. *
  66. * The file is expected to contain either PEM or DER encoded data.
  67. * A terminating null byte is always appended. It is included in the announced
  68. * length only if the data looks like it is PEM encoded.
  69. */
  70. int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
  71. {
  72. FILE *f;
  73. long size;
  74. PK_VALIDATE_RET( path != NULL );
  75. PK_VALIDATE_RET( buf != NULL );
  76. PK_VALIDATE_RET( n != NULL );
  77. if( ( f = fopen( path, "rb" ) ) == NULL )
  78. return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
  79. fseek( f, 0, SEEK_END );
  80. if( ( size = ftell( f ) ) == -1 )
  81. {
  82. fclose( f );
  83. return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
  84. }
  85. fseek( f, 0, SEEK_SET );
  86. *n = (size_t) size;
  87. if( *n + 1 == 0 ||
  88. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  89. {
  90. fclose( f );
  91. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  92. }
  93. if( fread( *buf, 1, *n, f ) != *n )
  94. {
  95. fclose( f );
  96. mbedtls_platform_zeroize( *buf, *n );
  97. mbedtls_free( *buf );
  98. return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
  99. }
  100. fclose( f );
  101. (*buf)[*n] = '\0';
  102. if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
  103. ++*n;
  104. return( 0 );
  105. }
  106. /*
  107. * Load and parse a private key
  108. */
  109. int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
  110. const char *path, const char *pwd )
  111. {
  112. int ret;
  113. size_t n;
  114. unsigned char *buf;
  115. PK_VALIDATE_RET( ctx != NULL );
  116. PK_VALIDATE_RET( path != NULL );
  117. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  118. return( ret );
  119. if( pwd == NULL )
  120. ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
  121. else
  122. ret = mbedtls_pk_parse_key( ctx, buf, n,
  123. (const unsigned char *) pwd, strlen( pwd ) );
  124. mbedtls_platform_zeroize( buf, n );
  125. mbedtls_free( buf );
  126. return( ret );
  127. }
  128. /*
  129. * Load and parse a public key
  130. */
  131. int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
  132. {
  133. int ret;
  134. size_t n;
  135. unsigned char *buf;
  136. PK_VALIDATE_RET( ctx != NULL );
  137. PK_VALIDATE_RET( path != NULL );
  138. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  139. return( ret );
  140. ret = mbedtls_pk_parse_public_key( ctx, buf, n );
  141. mbedtls_platform_zeroize( buf, n );
  142. mbedtls_free( buf );
  143. return( ret );
  144. }
  145. #endif /* MBEDTLS_FS_IO */
  146. #if defined(MBEDTLS_ECP_C)
  147. /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
  148. *
  149. * ECParameters ::= CHOICE {
  150. * namedCurve OBJECT IDENTIFIER
  151. * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
  152. * -- implicitCurve NULL
  153. * }
  154. */
  155. static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
  156. mbedtls_asn1_buf *params )
  157. {
  158. int ret;
  159. if ( end - *p < 1 )
  160. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  161. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  162. /* Tag may be either OID or SEQUENCE */
  163. params->tag = **p;
  164. if( params->tag != MBEDTLS_ASN1_OID
  165. #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
  166. && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
  167. #endif
  168. )
  169. {
  170. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  171. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  172. }
  173. if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
  174. {
  175. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  176. }
  177. params->p = *p;
  178. *p += params->len;
  179. if( *p != end )
  180. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  181. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  182. return( 0 );
  183. }
  184. #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
  185. /*
  186. * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
  187. * WARNING: the resulting group should only be used with
  188. * pk_group_id_from_specified(), since its base point may not be set correctly
  189. * if it was encoded compressed.
  190. *
  191. * SpecifiedECDomain ::= SEQUENCE {
  192. * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
  193. * fieldID FieldID {{FieldTypes}},
  194. * curve Curve,
  195. * base ECPoint,
  196. * order INTEGER,
  197. * cofactor INTEGER OPTIONAL,
  198. * hash HashAlgorithm OPTIONAL,
  199. * ...
  200. * }
  201. *
  202. * We only support prime-field as field type, and ignore hash and cofactor.
  203. */
  204. static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
  205. {
  206. int ret;
  207. unsigned char *p = params->p;
  208. const unsigned char * const end = params->p + params->len;
  209. const unsigned char *end_field, *end_curve;
  210. size_t len;
  211. int ver;
  212. /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
  213. if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
  214. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  215. if( ver < 1 || ver > 3 )
  216. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  217. /*
  218. * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
  219. * fieldType FIELD-ID.&id({IOSet}),
  220. * parameters FIELD-ID.&Type({IOSet}{@fieldType})
  221. * }
  222. */
  223. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  224. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  225. return( ret );
  226. end_field = p + len;
  227. /*
  228. * FIELD-ID ::= TYPE-IDENTIFIER
  229. * FieldTypes FIELD-ID ::= {
  230. * { Prime-p IDENTIFIED BY prime-field } |
  231. * { Characteristic-two IDENTIFIED BY characteristic-two-field }
  232. * }
  233. * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
  234. */
  235. if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
  236. return( ret );
  237. if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
  238. memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
  239. {
  240. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  241. }
  242. p += len;
  243. /* Prime-p ::= INTEGER -- Field of size p. */
  244. if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
  245. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  246. grp->pbits = mbedtls_mpi_bitlen( &grp->P );
  247. if( p != end_field )
  248. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  249. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  250. /*
  251. * Curve ::= SEQUENCE {
  252. * a FieldElement,
  253. * b FieldElement,
  254. * seed BIT STRING OPTIONAL
  255. * -- Shall be present if used in SpecifiedECDomain
  256. * -- with version equal to ecdpVer2 or ecdpVer3
  257. * }
  258. */
  259. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  260. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  261. return( ret );
  262. end_curve = p + len;
  263. /*
  264. * FieldElement ::= OCTET STRING
  265. * containing an integer in the case of a prime field
  266. */
  267. if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
  268. ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
  269. {
  270. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  271. }
  272. p += len;
  273. if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
  274. ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
  275. {
  276. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  277. }
  278. p += len;
  279. /* Ignore seed BIT STRING OPTIONAL */
  280. if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
  281. p += len;
  282. if( p != end_curve )
  283. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  284. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  285. /*
  286. * ECPoint ::= OCTET STRING
  287. */
  288. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  289. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  290. if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
  291. ( const unsigned char *) p, len ) ) != 0 )
  292. {
  293. /*
  294. * If we can't read the point because it's compressed, cheat by
  295. * reading only the X coordinate and the parity bit of Y.
  296. */
  297. if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
  298. ( p[0] != 0x02 && p[0] != 0x03 ) ||
  299. len != mbedtls_mpi_size( &grp->P ) + 1 ||
  300. mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
  301. mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
  302. mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
  303. {
  304. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  305. }
  306. }
  307. p += len;
  308. /*
  309. * order INTEGER
  310. */
  311. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
  312. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  313. grp->nbits = mbedtls_mpi_bitlen( &grp->N );
  314. /*
  315. * Allow optional elements by purposefully not enforcing p == end here.
  316. */
  317. return( 0 );
  318. }
  319. /*
  320. * Find the group id associated with an (almost filled) group as generated by
  321. * pk_group_from_specified(), or return an error if unknown.
  322. */
  323. static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
  324. {
  325. int ret = 0;
  326. mbedtls_ecp_group ref;
  327. const mbedtls_ecp_group_id *id;
  328. mbedtls_ecp_group_init( &ref );
  329. for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
  330. {
  331. /* Load the group associated to that id */
  332. mbedtls_ecp_group_free( &ref );
  333. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
  334. /* Compare to the group we were given, starting with easy tests */
  335. if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
  336. mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
  337. mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
  338. mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
  339. mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
  340. mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
  341. mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
  342. /* For Y we may only know the parity bit, so compare only that */
  343. mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
  344. {
  345. break;
  346. }
  347. }
  348. cleanup:
  349. mbedtls_ecp_group_free( &ref );
  350. *grp_id = *id;
  351. if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
  352. ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
  353. return( ret );
  354. }
  355. /*
  356. * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
  357. */
  358. static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
  359. mbedtls_ecp_group_id *grp_id )
  360. {
  361. int ret;
  362. mbedtls_ecp_group grp;
  363. mbedtls_ecp_group_init( &grp );
  364. if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
  365. goto cleanup;
  366. ret = pk_group_id_from_group( &grp, grp_id );
  367. cleanup:
  368. mbedtls_ecp_group_free( &grp );
  369. return( ret );
  370. }
  371. #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
  372. /*
  373. * Use EC parameters to initialise an EC group
  374. *
  375. * ECParameters ::= CHOICE {
  376. * namedCurve OBJECT IDENTIFIER
  377. * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
  378. * -- implicitCurve NULL
  379. */
  380. static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
  381. {
  382. int ret;
  383. mbedtls_ecp_group_id grp_id;
  384. if( params->tag == MBEDTLS_ASN1_OID )
  385. {
  386. if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
  387. return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
  388. }
  389. else
  390. {
  391. #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
  392. if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
  393. return( ret );
  394. #else
  395. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  396. #endif
  397. }
  398. /*
  399. * grp may already be initilialized; if so, make sure IDs match
  400. */
  401. if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
  402. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  403. if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
  404. return( ret );
  405. return( 0 );
  406. }
  407. /*
  408. * EC public key is an EC point
  409. *
  410. * The caller is responsible for clearing the structure upon failure if
  411. * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
  412. * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
  413. */
  414. static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
  415. mbedtls_ecp_keypair *key )
  416. {
  417. int ret;
  418. if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
  419. (const unsigned char *) *p, end - *p ) ) == 0 )
  420. {
  421. ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
  422. }
  423. /*
  424. * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
  425. */
  426. *p = (unsigned char *) end;
  427. return( ret );
  428. }
  429. #endif /* MBEDTLS_ECP_C */
  430. #if defined(MBEDTLS_RSA_C)
  431. /*
  432. * RSAPublicKey ::= SEQUENCE {
  433. * modulus INTEGER, -- n
  434. * publicExponent INTEGER -- e
  435. * }
  436. */
  437. static int pk_get_rsapubkey( unsigned char **p,
  438. const unsigned char *end,
  439. mbedtls_rsa_context *rsa )
  440. {
  441. int ret;
  442. size_t len;
  443. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  444. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  445. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  446. if( *p + len != end )
  447. return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
  448. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  449. /* Import N */
  450. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  451. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  452. if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
  453. NULL, 0, NULL, 0 ) ) != 0 )
  454. return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
  455. *p += len;
  456. /* Import E */
  457. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  458. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  459. if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
  460. NULL, 0, *p, len ) ) != 0 )
  461. return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
  462. *p += len;
  463. if( mbedtls_rsa_complete( rsa ) != 0 ||
  464. mbedtls_rsa_check_pubkey( rsa ) != 0 )
  465. {
  466. return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
  467. }
  468. if( *p != end )
  469. return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
  470. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  471. return( 0 );
  472. }
  473. #endif /* MBEDTLS_RSA_C */
  474. /* Get a PK algorithm identifier
  475. *
  476. * AlgorithmIdentifier ::= SEQUENCE {
  477. * algorithm OBJECT IDENTIFIER,
  478. * parameters ANY DEFINED BY algorithm OPTIONAL }
  479. */
  480. static int pk_get_pk_alg( unsigned char **p,
  481. const unsigned char *end,
  482. mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
  483. {
  484. int ret;
  485. mbedtls_asn1_buf alg_oid;
  486. memset( params, 0, sizeof(mbedtls_asn1_buf) );
  487. if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
  488. return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
  489. if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
  490. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  491. /*
  492. * No parameters with RSA (only for EC)
  493. */
  494. if( *pk_alg == MBEDTLS_PK_RSA &&
  495. ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
  496. params->len != 0 ) )
  497. {
  498. return( MBEDTLS_ERR_PK_INVALID_ALG );
  499. }
  500. return( 0 );
  501. }
  502. /*
  503. * SubjectPublicKeyInfo ::= SEQUENCE {
  504. * algorithm AlgorithmIdentifier,
  505. * subjectPublicKey BIT STRING }
  506. */
  507. int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
  508. mbedtls_pk_context *pk )
  509. {
  510. int ret;
  511. size_t len;
  512. mbedtls_asn1_buf alg_params;
  513. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  514. const mbedtls_pk_info_t *pk_info;
  515. PK_VALIDATE_RET( p != NULL );
  516. PK_VALIDATE_RET( *p != NULL );
  517. PK_VALIDATE_RET( end != NULL );
  518. PK_VALIDATE_RET( pk != NULL );
  519. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  520. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  521. {
  522. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  523. }
  524. end = *p + len;
  525. if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
  526. return( ret );
  527. if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
  528. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  529. if( *p + len != end )
  530. return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
  531. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  532. if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
  533. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  534. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
  535. return( ret );
  536. #if defined(MBEDTLS_RSA_C)
  537. if( pk_alg == MBEDTLS_PK_RSA )
  538. {
  539. ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
  540. } else
  541. #endif /* MBEDTLS_RSA_C */
  542. #if defined(MBEDTLS_ECP_C)
  543. if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
  544. {
  545. ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
  546. if( ret == 0 )
  547. ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
  548. } else
  549. #endif /* MBEDTLS_ECP_C */
  550. ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
  551. if( ret == 0 && *p != end )
  552. ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
  553. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  554. if( ret != 0 )
  555. mbedtls_pk_free( pk );
  556. return( ret );
  557. }
  558. #if defined(MBEDTLS_RSA_C)
  559. /*
  560. * Parse a PKCS#1 encoded private RSA key
  561. */
  562. static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
  563. const unsigned char *key,
  564. size_t keylen )
  565. {
  566. int ret, version;
  567. size_t len;
  568. unsigned char *p, *end;
  569. mbedtls_mpi T;
  570. mbedtls_mpi_init( &T );
  571. p = (unsigned char *) key;
  572. end = p + keylen;
  573. /*
  574. * This function parses the RSAPrivateKey (PKCS#1)
  575. *
  576. * RSAPrivateKey ::= SEQUENCE {
  577. * version Version,
  578. * modulus INTEGER, -- n
  579. * publicExponent INTEGER, -- e
  580. * privateExponent INTEGER, -- d
  581. * prime1 INTEGER, -- p
  582. * prime2 INTEGER, -- q
  583. * exponent1 INTEGER, -- d mod (p-1)
  584. * exponent2 INTEGER, -- d mod (q-1)
  585. * coefficient INTEGER, -- (inverse of q) mod p
  586. * otherPrimeInfos OtherPrimeInfos OPTIONAL
  587. * }
  588. */
  589. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  590. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  591. {
  592. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  593. }
  594. end = p + len;
  595. if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
  596. {
  597. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  598. }
  599. if( version != 0 )
  600. {
  601. return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
  602. }
  603. /* Import N */
  604. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  605. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  606. ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
  607. NULL, 0, NULL, 0 ) ) != 0 )
  608. goto cleanup;
  609. p += len;
  610. /* Import E */
  611. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  612. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  613. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
  614. NULL, 0, p, len ) ) != 0 )
  615. goto cleanup;
  616. p += len;
  617. /* Import D */
  618. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  619. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  620. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
  621. p, len, NULL, 0 ) ) != 0 )
  622. goto cleanup;
  623. p += len;
  624. /* Import P */
  625. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  626. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  627. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
  628. NULL, 0, NULL, 0 ) ) != 0 )
  629. goto cleanup;
  630. p += len;
  631. /* Import Q */
  632. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  633. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  634. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
  635. NULL, 0, NULL, 0 ) ) != 0 )
  636. goto cleanup;
  637. p += len;
  638. /* Complete the RSA private key */
  639. if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
  640. goto cleanup;
  641. /* Check optional parameters */
  642. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
  643. ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
  644. ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
  645. goto cleanup;
  646. if( p != end )
  647. {
  648. ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  649. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
  650. }
  651. cleanup:
  652. mbedtls_mpi_free( &T );
  653. if( ret != 0 )
  654. {
  655. /* Wrap error code if it's coming from a lower level */
  656. if( ( ret & 0xff80 ) == 0 )
  657. ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
  658. else
  659. ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  660. mbedtls_rsa_free( rsa );
  661. }
  662. return( ret );
  663. }
  664. #endif /* MBEDTLS_RSA_C */
  665. #if defined(MBEDTLS_ECP_C)
  666. /*
  667. * Parse a SEC1 encoded private EC key
  668. */
  669. static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
  670. const unsigned char *key,
  671. size_t keylen )
  672. {
  673. int ret;
  674. int version, pubkey_done;
  675. size_t len;
  676. mbedtls_asn1_buf params;
  677. unsigned char *p = (unsigned char *) key;
  678. unsigned char *end = p + keylen;
  679. unsigned char *end2;
  680. /*
  681. * RFC 5915, or SEC1 Appendix C.4
  682. *
  683. * ECPrivateKey ::= SEQUENCE {
  684. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  685. * privateKey OCTET STRING,
  686. * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  687. * publicKey [1] BIT STRING OPTIONAL
  688. * }
  689. */
  690. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  691. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  692. {
  693. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  694. }
  695. end = p + len;
  696. if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
  697. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  698. if( version != 1 )
  699. return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
  700. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  701. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  702. if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
  703. {
  704. mbedtls_ecp_keypair_free( eck );
  705. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  706. }
  707. p += len;
  708. pubkey_done = 0;
  709. if( p != end )
  710. {
  711. /*
  712. * Is 'parameters' present?
  713. */
  714. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  715. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
  716. {
  717. if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
  718. ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
  719. {
  720. mbedtls_ecp_keypair_free( eck );
  721. return( ret );
  722. }
  723. }
  724. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  725. {
  726. mbedtls_ecp_keypair_free( eck );
  727. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  728. }
  729. }
  730. if( p != end )
  731. {
  732. /*
  733. * Is 'publickey' present? If not, or if we can't read it (eg because it
  734. * is compressed), create it from the private key.
  735. */
  736. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  737. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
  738. {
  739. end2 = p + len;
  740. if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
  741. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  742. if( p + len != end2 )
  743. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  744. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  745. if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
  746. pubkey_done = 1;
  747. else
  748. {
  749. /*
  750. * The only acceptable failure mode of pk_get_ecpubkey() above
  751. * is if the point format is not recognized.
  752. */
  753. if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
  754. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  755. }
  756. }
  757. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  758. {
  759. mbedtls_ecp_keypair_free( eck );
  760. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  761. }
  762. }
  763. if( ! pubkey_done &&
  764. ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
  765. NULL, NULL ) ) != 0 )
  766. {
  767. mbedtls_ecp_keypair_free( eck );
  768. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  769. }
  770. if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
  771. {
  772. mbedtls_ecp_keypair_free( eck );
  773. return( ret );
  774. }
  775. return( 0 );
  776. }
  777. #endif /* MBEDTLS_ECP_C */
  778. /*
  779. * Parse an unencrypted PKCS#8 encoded private key
  780. *
  781. * Notes:
  782. *
  783. * - This function does not own the key buffer. It is the
  784. * responsibility of the caller to take care of zeroizing
  785. * and freeing it after use.
  786. *
  787. * - The function is responsible for freeing the provided
  788. * PK context on failure.
  789. *
  790. */
  791. static int pk_parse_key_pkcs8_unencrypted_der(
  792. mbedtls_pk_context *pk,
  793. const unsigned char* key,
  794. size_t keylen )
  795. {
  796. int ret, version;
  797. size_t len;
  798. mbedtls_asn1_buf params;
  799. unsigned char *p = (unsigned char *) key;
  800. unsigned char *end = p + keylen;
  801. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  802. const mbedtls_pk_info_t *pk_info;
  803. /*
  804. * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
  805. *
  806. * PrivateKeyInfo ::= SEQUENCE {
  807. * version Version,
  808. * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
  809. * privateKey PrivateKey,
  810. * attributes [0] IMPLICIT Attributes OPTIONAL }
  811. *
  812. * Version ::= INTEGER
  813. * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
  814. * PrivateKey ::= OCTET STRING
  815. *
  816. * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
  817. */
  818. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  819. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  820. {
  821. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  822. }
  823. end = p + len;
  824. if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
  825. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  826. if( version != 0 )
  827. return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
  828. if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
  829. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  830. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  831. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  832. if( len < 1 )
  833. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  834. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  835. if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
  836. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  837. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
  838. return( ret );
  839. #if defined(MBEDTLS_RSA_C)
  840. if( pk_alg == MBEDTLS_PK_RSA )
  841. {
  842. if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
  843. {
  844. mbedtls_pk_free( pk );
  845. return( ret );
  846. }
  847. } else
  848. #endif /* MBEDTLS_RSA_C */
  849. #if defined(MBEDTLS_ECP_C)
  850. if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
  851. {
  852. if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
  853. ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
  854. {
  855. mbedtls_pk_free( pk );
  856. return( ret );
  857. }
  858. } else
  859. #endif /* MBEDTLS_ECP_C */
  860. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  861. return( 0 );
  862. }
  863. /*
  864. * Parse an encrypted PKCS#8 encoded private key
  865. *
  866. * To save space, the decryption happens in-place on the given key buffer.
  867. * Also, while this function may modify the keybuffer, it doesn't own it,
  868. * and instead it is the responsibility of the caller to zeroize and properly
  869. * free it after use.
  870. *
  871. */
  872. #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  873. static int pk_parse_key_pkcs8_encrypted_der(
  874. mbedtls_pk_context *pk,
  875. unsigned char *key, size_t keylen,
  876. const unsigned char *pwd, size_t pwdlen )
  877. {
  878. int ret, decrypted = 0;
  879. size_t len;
  880. unsigned char *buf;
  881. unsigned char *p, *end;
  882. mbedtls_asn1_buf pbe_alg_oid, pbe_params;
  883. #if defined(MBEDTLS_PKCS12_C)
  884. mbedtls_cipher_type_t cipher_alg;
  885. mbedtls_md_type_t md_alg;
  886. #endif
  887. p = key;
  888. end = p + keylen;
  889. if( pwdlen == 0 )
  890. return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
  891. /*
  892. * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
  893. *
  894. * EncryptedPrivateKeyInfo ::= SEQUENCE {
  895. * encryptionAlgorithm EncryptionAlgorithmIdentifier,
  896. * encryptedData EncryptedData
  897. * }
  898. *
  899. * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
  900. *
  901. * EncryptedData ::= OCTET STRING
  902. *
  903. * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
  904. *
  905. */
  906. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  907. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  908. {
  909. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  910. }
  911. end = p + len;
  912. if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
  913. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  914. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  915. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  916. buf = p;
  917. /*
  918. * Decrypt EncryptedData with appropriate PBE
  919. */
  920. #if defined(MBEDTLS_PKCS12_C)
  921. if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
  922. {
  923. if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
  924. cipher_alg, md_alg,
  925. pwd, pwdlen, p, len, buf ) ) != 0 )
  926. {
  927. if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
  928. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  929. return( ret );
  930. }
  931. decrypted = 1;
  932. }
  933. else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
  934. {
  935. if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
  936. MBEDTLS_PKCS12_PBE_DECRYPT,
  937. pwd, pwdlen,
  938. p, len, buf ) ) != 0 )
  939. {
  940. return( ret );
  941. }
  942. // Best guess for password mismatch when using RC4. If first tag is
  943. // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
  944. //
  945. if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  946. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  947. decrypted = 1;
  948. }
  949. else
  950. #endif /* MBEDTLS_PKCS12_C */
  951. #if defined(MBEDTLS_PKCS5_C)
  952. if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
  953. {
  954. if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
  955. p, len, buf ) ) != 0 )
  956. {
  957. if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
  958. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  959. return( ret );
  960. }
  961. decrypted = 1;
  962. }
  963. else
  964. #endif /* MBEDTLS_PKCS5_C */
  965. {
  966. ((void) pwd);
  967. }
  968. if( decrypted == 0 )
  969. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  970. return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
  971. }
  972. #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
  973. /*
  974. * Parse a private key
  975. */
  976. int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
  977. const unsigned char *key, size_t keylen,
  978. const unsigned char *pwd, size_t pwdlen )
  979. {
  980. int ret;
  981. const mbedtls_pk_info_t *pk_info;
  982. #if defined(MBEDTLS_PEM_PARSE_C)
  983. size_t len;
  984. mbedtls_pem_context pem;
  985. #endif
  986. PK_VALIDATE_RET( pk != NULL );
  987. if( keylen == 0 )
  988. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  989. PK_VALIDATE_RET( key != NULL );
  990. #if defined(MBEDTLS_PEM_PARSE_C)
  991. mbedtls_pem_init( &pem );
  992. #if defined(MBEDTLS_RSA_C)
  993. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  994. if( key[keylen - 1] != '\0' )
  995. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  996. else
  997. ret = mbedtls_pem_read_buffer( &pem,
  998. "-----BEGIN RSA PRIVATE KEY-----",
  999. "-----END RSA PRIVATE KEY-----",
  1000. key, pwd, pwdlen, &len );
  1001. if( ret == 0 )
  1002. {
  1003. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
  1004. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
  1005. ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
  1006. pem.buf, pem.buflen ) ) != 0 )
  1007. {
  1008. mbedtls_pk_free( pk );
  1009. }
  1010. mbedtls_pem_free( &pem );
  1011. return( ret );
  1012. }
  1013. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
  1014. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  1015. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
  1016. return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
  1017. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  1018. return( ret );
  1019. #endif /* MBEDTLS_RSA_C */
  1020. #if defined(MBEDTLS_ECP_C)
  1021. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  1022. if( key[keylen - 1] != '\0' )
  1023. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  1024. else
  1025. ret = mbedtls_pem_read_buffer( &pem,
  1026. "-----BEGIN EC PRIVATE KEY-----",
  1027. "-----END EC PRIVATE KEY-----",
  1028. key, pwd, pwdlen, &len );
  1029. if( ret == 0 )
  1030. {
  1031. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
  1032. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
  1033. ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
  1034. pem.buf, pem.buflen ) ) != 0 )
  1035. {
  1036. mbedtls_pk_free( pk );
  1037. }
  1038. mbedtls_pem_free( &pem );
  1039. return( ret );
  1040. }
  1041. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
  1042. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  1043. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
  1044. return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
  1045. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  1046. return( ret );
  1047. #endif /* MBEDTLS_ECP_C */
  1048. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  1049. if( key[keylen - 1] != '\0' )
  1050. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  1051. else
  1052. ret = mbedtls_pem_read_buffer( &pem,
  1053. "-----BEGIN PRIVATE KEY-----",
  1054. "-----END PRIVATE KEY-----",
  1055. key, NULL, 0, &len );
  1056. if( ret == 0 )
  1057. {
  1058. if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
  1059. pem.buf, pem.buflen ) ) != 0 )
  1060. {
  1061. mbedtls_pk_free( pk );
  1062. }
  1063. mbedtls_pem_free( &pem );
  1064. return( ret );
  1065. }
  1066. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  1067. return( ret );
  1068. #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  1069. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  1070. if( key[keylen - 1] != '\0' )
  1071. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  1072. else
  1073. ret = mbedtls_pem_read_buffer( &pem,
  1074. "-----BEGIN ENCRYPTED PRIVATE KEY-----",
  1075. "-----END ENCRYPTED PRIVATE KEY-----",
  1076. key, NULL, 0, &len );
  1077. if( ret == 0 )
  1078. {
  1079. if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
  1080. pem.buf, pem.buflen,
  1081. pwd, pwdlen ) ) != 0 )
  1082. {
  1083. mbedtls_pk_free( pk );
  1084. }
  1085. mbedtls_pem_free( &pem );
  1086. return( ret );
  1087. }
  1088. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  1089. return( ret );
  1090. #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
  1091. #else
  1092. ((void) pwd);
  1093. ((void) pwdlen);
  1094. #endif /* MBEDTLS_PEM_PARSE_C */
  1095. /*
  1096. * At this point we only know it's not a PEM formatted key. Could be any
  1097. * of the known DER encoded private key formats
  1098. *
  1099. * We try the different DER format parsers to see if one passes without
  1100. * error
  1101. */
  1102. #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  1103. {
  1104. unsigned char *key_copy;
  1105. if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
  1106. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  1107. memcpy( key_copy, key, keylen );
  1108. ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
  1109. pwd, pwdlen );
  1110. mbedtls_platform_zeroize( key_copy, keylen );
  1111. mbedtls_free( key_copy );
  1112. }
  1113. if( ret == 0 )
  1114. return( 0 );
  1115. mbedtls_pk_free( pk );
  1116. mbedtls_pk_init( pk );
  1117. if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
  1118. {
  1119. return( ret );
  1120. }
  1121. #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
  1122. if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
  1123. return( 0 );
  1124. mbedtls_pk_free( pk );
  1125. mbedtls_pk_init( pk );
  1126. #if defined(MBEDTLS_RSA_C)
  1127. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
  1128. if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
  1129. pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
  1130. {
  1131. return( 0 );
  1132. }
  1133. mbedtls_pk_free( pk );
  1134. mbedtls_pk_init( pk );
  1135. #endif /* MBEDTLS_RSA_C */
  1136. #if defined(MBEDTLS_ECP_C)
  1137. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
  1138. if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
  1139. pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
  1140. key, keylen ) == 0 )
  1141. {
  1142. return( 0 );
  1143. }
  1144. mbedtls_pk_free( pk );
  1145. #endif /* MBEDTLS_ECP_C */
  1146. /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
  1147. * it is ok to leave the PK context initialized but not
  1148. * freed: It is the caller's responsibility to call pk_init()
  1149. * before calling this function, and to call pk_free()
  1150. * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
  1151. * isn't, this leads to mbedtls_pk_free() being called
  1152. * twice, once here and once by the caller, but this is
  1153. * also ok and in line with the mbedtls_pk_free() calls
  1154. * on failed PEM parsing attempts. */
  1155. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  1156. }
  1157. /*
  1158. * Parse a public key
  1159. */
  1160. int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
  1161. const unsigned char *key, size_t keylen )
  1162. {
  1163. int ret;
  1164. unsigned char *p;
  1165. #if defined(MBEDTLS_RSA_C)
  1166. const mbedtls_pk_info_t *pk_info;
  1167. #endif
  1168. #if defined(MBEDTLS_PEM_PARSE_C)
  1169. size_t len;
  1170. mbedtls_pem_context pem;
  1171. #endif
  1172. PK_VALIDATE_RET( ctx != NULL );
  1173. if( keylen == 0 )
  1174. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  1175. PK_VALIDATE_RET( key != NULL || keylen == 0 );
  1176. #if defined(MBEDTLS_PEM_PARSE_C)
  1177. mbedtls_pem_init( &pem );
  1178. #if defined(MBEDTLS_RSA_C)
  1179. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  1180. if( key[keylen - 1] != '\0' )
  1181. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  1182. else
  1183. ret = mbedtls_pem_read_buffer( &pem,
  1184. "-----BEGIN RSA PUBLIC KEY-----",
  1185. "-----END RSA PUBLIC KEY-----",
  1186. key, NULL, 0, &len );
  1187. if( ret == 0 )
  1188. {
  1189. p = pem.buf;
  1190. if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
  1191. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  1192. if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
  1193. return( ret );
  1194. if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
  1195. mbedtls_pk_free( ctx );
  1196. mbedtls_pem_free( &pem );
  1197. return( ret );
  1198. }
  1199. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  1200. {
  1201. mbedtls_pem_free( &pem );
  1202. return( ret );
  1203. }
  1204. #endif /* MBEDTLS_RSA_C */
  1205. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  1206. if( key[keylen - 1] != '\0' )
  1207. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  1208. else
  1209. ret = mbedtls_pem_read_buffer( &pem,
  1210. "-----BEGIN PUBLIC KEY-----",
  1211. "-----END PUBLIC KEY-----",
  1212. key, NULL, 0, &len );
  1213. if( ret == 0 )
  1214. {
  1215. /*
  1216. * Was PEM encoded
  1217. */
  1218. p = pem.buf;
  1219. ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
  1220. mbedtls_pem_free( &pem );
  1221. return( ret );
  1222. }
  1223. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  1224. {
  1225. mbedtls_pem_free( &pem );
  1226. return( ret );
  1227. }
  1228. mbedtls_pem_free( &pem );
  1229. #endif /* MBEDTLS_PEM_PARSE_C */
  1230. #if defined(MBEDTLS_RSA_C)
  1231. if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
  1232. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  1233. if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
  1234. return( ret );
  1235. p = (unsigned char *)key;
  1236. ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
  1237. if( ret == 0 )
  1238. {
  1239. return( ret );
  1240. }
  1241. mbedtls_pk_free( ctx );
  1242. if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
  1243. {
  1244. return( ret );
  1245. }
  1246. #endif /* MBEDTLS_RSA_C */
  1247. p = (unsigned char *) key;
  1248. ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
  1249. return( ret );
  1250. }
  1251. #endif /* MBEDTLS_PK_PARSE_C */