pk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Public Key abstraction layer
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PK_C)
  52. #include "mbedtls/pk.h"
  53. #include "mbedtls/pk_internal.h"
  54. #include "mbedtls/platform_util.h"
  55. #if defined(MBEDTLS_RSA_C)
  56. #include "mbedtls/rsa.h"
  57. #endif
  58. #if defined(MBEDTLS_ECP_C)
  59. #include "mbedtls/ecp.h"
  60. #endif
  61. #if defined(MBEDTLS_ECDSA_C)
  62. #include "mbedtls/ecdsa.h"
  63. #endif
  64. #include <limits.h>
  65. #include <stdint.h>
  66. /* Parameter validation macros based on platform_util.h */
  67. #define PK_VALIDATE_RET( cond ) \
  68. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
  69. #define PK_VALIDATE( cond ) \
  70. MBEDTLS_INTERNAL_VALIDATE( cond )
  71. /*
  72. * Initialise a mbedtls_pk_context
  73. */
  74. void mbedtls_pk_init( mbedtls_pk_context *ctx )
  75. {
  76. PK_VALIDATE( ctx != NULL );
  77. ctx->pk_info = NULL;
  78. ctx->pk_ctx = NULL;
  79. }
  80. /*
  81. * Free (the components of) a mbedtls_pk_context
  82. */
  83. void mbedtls_pk_free( mbedtls_pk_context *ctx )
  84. {
  85. if( ctx == NULL )
  86. return;
  87. if ( ctx->pk_info != NULL )
  88. ctx->pk_info->ctx_free_func( ctx->pk_ctx );
  89. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
  90. }
  91. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  92. /*
  93. * Initialize a restart context
  94. */
  95. void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
  96. {
  97. PK_VALIDATE( ctx != NULL );
  98. ctx->pk_info = NULL;
  99. ctx->rs_ctx = NULL;
  100. }
  101. /*
  102. * Free the components of a restart context
  103. */
  104. void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
  105. {
  106. if( ctx == NULL || ctx->pk_info == NULL ||
  107. ctx->pk_info->rs_free_func == NULL )
  108. {
  109. return;
  110. }
  111. ctx->pk_info->rs_free_func( ctx->rs_ctx );
  112. ctx->pk_info = NULL;
  113. ctx->rs_ctx = NULL;
  114. }
  115. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  116. /*
  117. * Get pk_info structure from type
  118. */
  119. const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
  120. {
  121. switch( pk_type ) {
  122. #if defined(MBEDTLS_RSA_C)
  123. case MBEDTLS_PK_RSA:
  124. return( &mbedtls_rsa_info );
  125. #endif
  126. #if defined(MBEDTLS_ECP_C)
  127. case MBEDTLS_PK_ECKEY:
  128. return( &mbedtls_eckey_info );
  129. case MBEDTLS_PK_ECKEY_DH:
  130. return( &mbedtls_eckeydh_info );
  131. #endif
  132. #if defined(MBEDTLS_ECDSA_C)
  133. case MBEDTLS_PK_ECDSA:
  134. return( &mbedtls_ecdsa_info );
  135. #endif
  136. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  137. default:
  138. return( NULL );
  139. }
  140. }
  141. /*
  142. * Initialise context
  143. */
  144. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
  145. {
  146. PK_VALIDATE_RET( ctx != NULL );
  147. if( info == NULL || ctx->pk_info != NULL )
  148. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  149. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  150. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  151. ctx->pk_info = info;
  152. return( 0 );
  153. }
  154. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  155. /*
  156. * Initialize an RSA-alt context
  157. */
  158. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  159. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  160. mbedtls_pk_rsa_alt_sign_func sign_func,
  161. mbedtls_pk_rsa_alt_key_len_func key_len_func )
  162. {
  163. mbedtls_rsa_alt_context *rsa_alt;
  164. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  165. PK_VALIDATE_RET( ctx != NULL );
  166. if( ctx->pk_info != NULL )
  167. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  168. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  169. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  170. ctx->pk_info = info;
  171. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  172. rsa_alt->key = key;
  173. rsa_alt->decrypt_func = decrypt_func;
  174. rsa_alt->sign_func = sign_func;
  175. rsa_alt->key_len_func = key_len_func;
  176. return( 0 );
  177. }
  178. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  179. /*
  180. * Tell if a PK can do the operations of the given type
  181. */
  182. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
  183. {
  184. /* A context with null pk_info is not set up yet and can't do anything.
  185. * For backward compatibility, also accept NULL instead of a context
  186. * pointer. */
  187. if( ctx == NULL || ctx->pk_info == NULL )
  188. return( 0 );
  189. return( ctx->pk_info->can_do( type ) );
  190. }
  191. /*
  192. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  193. */
  194. static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
  195. {
  196. const mbedtls_md_info_t *md_info;
  197. if( *hash_len != 0 && md_alg == MBEDTLS_MD_NONE )
  198. return( 0 );
  199. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  200. return( -1 );
  201. if ( *hash_len != 0 && *hash_len < mbedtls_md_get_size( md_info ) )
  202. return ( -1 );
  203. *hash_len = mbedtls_md_get_size( md_info );
  204. return( 0 );
  205. }
  206. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  207. /*
  208. * Helper to set up a restart context if needed
  209. */
  210. static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
  211. const mbedtls_pk_info_t *info )
  212. {
  213. /* Don't do anything if already set up or invalid */
  214. if( ctx == NULL || ctx->pk_info != NULL )
  215. return( 0 );
  216. /* Should never happen when we're called */
  217. if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
  218. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  219. if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
  220. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  221. ctx->pk_info = info;
  222. return( 0 );
  223. }
  224. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  225. /*
  226. * Verify a signature (restartable)
  227. */
  228. int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
  229. mbedtls_md_type_t md_alg,
  230. const unsigned char *hash, size_t hash_len,
  231. const unsigned char *sig, size_t sig_len,
  232. mbedtls_pk_restart_ctx *rs_ctx )
  233. {
  234. PK_VALIDATE_RET( ctx != NULL );
  235. PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
  236. hash != NULL );
  237. PK_VALIDATE_RET( sig != NULL );
  238. if( ctx->pk_info == NULL ||
  239. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  240. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  241. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  242. /* optimization: use non-restartable version if restart disabled */
  243. if( rs_ctx != NULL &&
  244. mbedtls_ecp_restart_is_enabled() &&
  245. ctx->pk_info->verify_rs_func != NULL )
  246. {
  247. int ret;
  248. if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
  249. return( ret );
  250. ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
  251. md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
  252. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  253. mbedtls_pk_restart_free( rs_ctx );
  254. return( ret );
  255. }
  256. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  257. (void) rs_ctx;
  258. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  259. if( ctx->pk_info->verify_func == NULL )
  260. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  261. return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
  262. sig, sig_len ) );
  263. }
  264. /*
  265. * Verify a signature
  266. */
  267. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  268. const unsigned char *hash, size_t hash_len,
  269. const unsigned char *sig, size_t sig_len )
  270. {
  271. return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
  272. sig, sig_len, NULL ) );
  273. }
  274. /*
  275. * Verify a signature with options
  276. */
  277. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  278. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  279. const unsigned char *hash, size_t hash_len,
  280. const unsigned char *sig, size_t sig_len )
  281. {
  282. PK_VALIDATE_RET( ctx != NULL );
  283. PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
  284. hash != NULL );
  285. PK_VALIDATE_RET( sig != NULL );
  286. if( ctx->pk_info == NULL )
  287. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  288. if( ! mbedtls_pk_can_do( ctx, type ) )
  289. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  290. if( type == MBEDTLS_PK_RSASSA_PSS )
  291. {
  292. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  293. int ret;
  294. const mbedtls_pk_rsassa_pss_options *pss_opts;
  295. #if SIZE_MAX > UINT_MAX
  296. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  297. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  298. #endif /* SIZE_MAX > UINT_MAX */
  299. if( options == NULL )
  300. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  301. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  302. if( sig_len < mbedtls_pk_get_len( ctx ) )
  303. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  304. ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
  305. NULL, NULL, MBEDTLS_RSA_PUBLIC,
  306. md_alg, (unsigned int) hash_len, hash,
  307. pss_opts->mgf1_hash_id,
  308. pss_opts->expected_salt_len,
  309. sig );
  310. if( ret != 0 )
  311. return( ret );
  312. if( sig_len > mbedtls_pk_get_len( ctx ) )
  313. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  314. return( 0 );
  315. #else
  316. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  317. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  318. }
  319. /* General case: no options */
  320. if( options != NULL )
  321. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  322. return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
  323. }
  324. /*
  325. * Make a signature (restartable)
  326. */
  327. int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
  328. mbedtls_md_type_t md_alg,
  329. const unsigned char *hash, size_t hash_len,
  330. unsigned char *sig, size_t *sig_len,
  331. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  332. mbedtls_pk_restart_ctx *rs_ctx )
  333. {
  334. PK_VALIDATE_RET( ctx != NULL );
  335. PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
  336. hash != NULL );
  337. PK_VALIDATE_RET( sig != NULL );
  338. if( ctx->pk_info == NULL ||
  339. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  340. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  341. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  342. /* optimization: use non-restartable version if restart disabled */
  343. if( rs_ctx != NULL &&
  344. mbedtls_ecp_restart_is_enabled() &&
  345. ctx->pk_info->sign_rs_func != NULL )
  346. {
  347. int ret;
  348. if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
  349. return( ret );
  350. ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
  351. hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
  352. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  353. mbedtls_pk_restart_free( rs_ctx );
  354. return( ret );
  355. }
  356. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  357. (void) rs_ctx;
  358. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  359. if( ctx->pk_info->sign_func == NULL )
  360. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  361. return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
  362. sig, sig_len, f_rng, p_rng ) );
  363. }
  364. /*
  365. * Make a signature
  366. */
  367. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  368. const unsigned char *hash, size_t hash_len,
  369. unsigned char *sig, size_t *sig_len,
  370. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  371. {
  372. return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
  373. sig, sig_len, f_rng, p_rng, NULL ) );
  374. }
  375. /*
  376. * Decrypt message
  377. */
  378. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  379. const unsigned char *input, size_t ilen,
  380. unsigned char *output, size_t *olen, size_t osize,
  381. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  382. {
  383. PK_VALIDATE_RET( ctx != NULL );
  384. PK_VALIDATE_RET( input != NULL || ilen == 0 );
  385. PK_VALIDATE_RET( output != NULL || osize == 0 );
  386. PK_VALIDATE_RET( olen != NULL );
  387. if( ctx->pk_info == NULL )
  388. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  389. if( ctx->pk_info->decrypt_func == NULL )
  390. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  391. return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
  392. output, olen, osize, f_rng, p_rng ) );
  393. }
  394. /*
  395. * Encrypt message
  396. */
  397. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  398. const unsigned char *input, size_t ilen,
  399. unsigned char *output, size_t *olen, size_t osize,
  400. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  401. {
  402. PK_VALIDATE_RET( ctx != NULL );
  403. PK_VALIDATE_RET( input != NULL || ilen == 0 );
  404. PK_VALIDATE_RET( output != NULL || osize == 0 );
  405. PK_VALIDATE_RET( olen != NULL );
  406. if( ctx->pk_info == NULL )
  407. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  408. if( ctx->pk_info->encrypt_func == NULL )
  409. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  410. return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
  411. output, olen, osize, f_rng, p_rng ) );
  412. }
  413. /*
  414. * Check public-private key pair
  415. */
  416. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
  417. {
  418. PK_VALIDATE_RET( pub != NULL );
  419. PK_VALIDATE_RET( prv != NULL );
  420. if( pub->pk_info == NULL ||
  421. prv->pk_info == NULL ||
  422. prv->pk_info->check_pair_func == NULL )
  423. {
  424. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  425. }
  426. if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
  427. {
  428. if( pub->pk_info->type != MBEDTLS_PK_RSA )
  429. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  430. }
  431. else
  432. {
  433. if( pub->pk_info != prv->pk_info )
  434. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  435. }
  436. return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
  437. }
  438. /*
  439. * Get key size in bits
  440. */
  441. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
  442. {
  443. /* For backward compatibility, accept NULL or a context that
  444. * isn't set up yet, and return a fake value that should be safe. */
  445. if( ctx == NULL || ctx->pk_info == NULL )
  446. return( 0 );
  447. return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
  448. }
  449. /*
  450. * Export debug information
  451. */
  452. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
  453. {
  454. PK_VALIDATE_RET( ctx != NULL );
  455. if( ctx->pk_info == NULL )
  456. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  457. if( ctx->pk_info->debug_func == NULL )
  458. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  459. ctx->pk_info->debug_func( ctx->pk_ctx, items );
  460. return( 0 );
  461. }
  462. /*
  463. * Access the PK type name
  464. */
  465. const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
  466. {
  467. if( ctx == NULL || ctx->pk_info == NULL )
  468. return( "invalid PK" );
  469. return( ctx->pk_info->name );
  470. }
  471. /*
  472. * Access the PK type
  473. */
  474. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
  475. {
  476. if( ctx == NULL || ctx->pk_info == NULL )
  477. return( MBEDTLS_PK_NONE );
  478. return( ctx->pk_info->type );
  479. }
  480. #endif /* MBEDTLS_PK_C */