pkparse.c 38 KB

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