pkparse.c 42 KB

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