pkparse.c 38 KB

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