pkparse.c 48 KB

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