pk.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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. #include "common.h"
  8. #if defined(MBEDTLS_PK_C)
  9. #include "mbedtls/pk.h"
  10. #include "pk_wrap.h"
  11. #include "pkwrite.h"
  12. #include "pk_internal.h"
  13. #include "mbedtls/platform_util.h"
  14. #include "mbedtls/error.h"
  15. #if defined(MBEDTLS_RSA_C)
  16. #include "mbedtls/rsa.h"
  17. #include "rsa_internal.h"
  18. #endif
  19. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  20. #include "mbedtls/ecp.h"
  21. #endif
  22. #if defined(MBEDTLS_ECDSA_C)
  23. #include "mbedtls/ecdsa.h"
  24. #endif
  25. #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
  26. #include "psa_util_internal.h"
  27. #include "mbedtls/psa_util.h"
  28. #endif
  29. #include <limits.h>
  30. #include <stdint.h>
  31. #define PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE \
  32. (PSA_EXPORT_KEY_PAIR_MAX_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) ? \
  33. PSA_EXPORT_KEY_PAIR_MAX_SIZE : PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
  34. /*
  35. * Initialise a mbedtls_pk_context
  36. */
  37. void mbedtls_pk_init(mbedtls_pk_context *ctx)
  38. {
  39. ctx->pk_info = NULL;
  40. ctx->pk_ctx = NULL;
  41. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  42. ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
  43. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  44. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  45. memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
  46. ctx->pub_raw_len = 0;
  47. ctx->ec_family = 0;
  48. ctx->ec_bits = 0;
  49. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  50. }
  51. /*
  52. * Free (the components of) a mbedtls_pk_context
  53. */
  54. void mbedtls_pk_free(mbedtls_pk_context *ctx)
  55. {
  56. if (ctx == NULL) {
  57. return;
  58. }
  59. if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
  60. ctx->pk_info->ctx_free_func(ctx->pk_ctx);
  61. }
  62. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  63. /* The ownership of the priv_id key for opaque keys is external of the PK
  64. * module. It's the user responsibility to clear it after use. */
  65. if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
  66. psa_destroy_key(ctx->priv_id);
  67. }
  68. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  69. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
  70. }
  71. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  72. /*
  73. * Initialize a restart context
  74. */
  75. void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
  76. {
  77. ctx->pk_info = NULL;
  78. ctx->rs_ctx = NULL;
  79. }
  80. /*
  81. * Free the components of a restart context
  82. */
  83. void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
  84. {
  85. if (ctx == NULL || ctx->pk_info == NULL ||
  86. ctx->pk_info->rs_free_func == NULL) {
  87. return;
  88. }
  89. ctx->pk_info->rs_free_func(ctx->rs_ctx);
  90. ctx->pk_info = NULL;
  91. ctx->rs_ctx = NULL;
  92. }
  93. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  94. /*
  95. * Get pk_info structure from type
  96. */
  97. const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
  98. {
  99. switch (pk_type) {
  100. #if defined(MBEDTLS_RSA_C)
  101. case MBEDTLS_PK_RSA:
  102. return &mbedtls_rsa_info;
  103. #endif /* MBEDTLS_RSA_C */
  104. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  105. case MBEDTLS_PK_ECKEY:
  106. return &mbedtls_eckey_info;
  107. case MBEDTLS_PK_ECKEY_DH:
  108. return &mbedtls_eckeydh_info;
  109. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  110. #if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
  111. case MBEDTLS_PK_ECDSA:
  112. return &mbedtls_ecdsa_info;
  113. #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
  114. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  115. default:
  116. return NULL;
  117. }
  118. }
  119. /*
  120. * Initialise context
  121. */
  122. int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
  123. {
  124. if (info == NULL || ctx->pk_info != NULL) {
  125. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  126. }
  127. if ((info->ctx_alloc_func != NULL) &&
  128. ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
  129. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  130. }
  131. ctx->pk_info = info;
  132. return 0;
  133. }
  134. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  135. /*
  136. * Initialise a PSA-wrapping context
  137. */
  138. int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
  139. const mbedtls_svc_key_id_t key)
  140. {
  141. const mbedtls_pk_info_t *info = NULL;
  142. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  143. psa_key_type_t type;
  144. if (ctx == NULL || ctx->pk_info != NULL) {
  145. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  146. }
  147. if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
  148. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  149. }
  150. type = psa_get_key_type(&attributes);
  151. psa_reset_key_attributes(&attributes);
  152. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  153. if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
  154. info = &mbedtls_ecdsa_opaque_info;
  155. } else
  156. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  157. if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
  158. info = &mbedtls_rsa_opaque_info;
  159. } else {
  160. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  161. }
  162. ctx->pk_info = info;
  163. ctx->priv_id = key;
  164. return 0;
  165. }
  166. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  167. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  168. /*
  169. * Initialize an RSA-alt context
  170. */
  171. int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
  172. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  173. mbedtls_pk_rsa_alt_sign_func sign_func,
  174. mbedtls_pk_rsa_alt_key_len_func key_len_func)
  175. {
  176. mbedtls_rsa_alt_context *rsa_alt;
  177. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  178. if (ctx->pk_info != NULL) {
  179. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  180. }
  181. if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
  182. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  183. }
  184. ctx->pk_info = info;
  185. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  186. rsa_alt->key = key;
  187. rsa_alt->decrypt_func = decrypt_func;
  188. rsa_alt->sign_func = sign_func;
  189. rsa_alt->key_len_func = key_len_func;
  190. return 0;
  191. }
  192. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  193. /*
  194. * Tell if a PK can do the operations of the given type
  195. */
  196. int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
  197. {
  198. /* A context with null pk_info is not set up yet and can't do anything.
  199. * For backward compatibility, also accept NULL instead of a context
  200. * pointer. */
  201. if (ctx == NULL || ctx->pk_info == NULL) {
  202. return 0;
  203. }
  204. return ctx->pk_info->can_do(type);
  205. }
  206. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  207. /*
  208. * Tell if a PK can do the operations of the given PSA algorithm
  209. */
  210. int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
  211. psa_key_usage_t usage)
  212. {
  213. psa_key_usage_t key_usage;
  214. /* A context with null pk_info is not set up yet and can't do anything.
  215. * For backward compatibility, also accept NULL instead of a context
  216. * pointer. */
  217. if (ctx == NULL || ctx->pk_info == NULL) {
  218. return 0;
  219. }
  220. /* Filter out non allowed algorithms */
  221. if (PSA_ALG_IS_ECDSA(alg) == 0 &&
  222. PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
  223. PSA_ALG_IS_RSA_PSS(alg) == 0 &&
  224. alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
  225. PSA_ALG_IS_ECDH(alg) == 0) {
  226. return 0;
  227. }
  228. /* Filter out non allowed usage flags */
  229. if (usage == 0 ||
  230. (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
  231. PSA_KEY_USAGE_DECRYPT |
  232. PSA_KEY_USAGE_DERIVE)) != 0) {
  233. return 0;
  234. }
  235. /* Wildcard hash is not allowed */
  236. if (PSA_ALG_IS_SIGN_HASH(alg) &&
  237. PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
  238. return 0;
  239. }
  240. if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
  241. mbedtls_pk_type_t type;
  242. if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
  243. type = MBEDTLS_PK_ECKEY;
  244. } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
  245. alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
  246. type = MBEDTLS_PK_RSA;
  247. } else if (PSA_ALG_IS_RSA_PSS(alg)) {
  248. type = MBEDTLS_PK_RSASSA_PSS;
  249. } else {
  250. return 0;
  251. }
  252. if (ctx->pk_info->can_do(type) == 0) {
  253. return 0;
  254. }
  255. switch (type) {
  256. case MBEDTLS_PK_ECKEY:
  257. key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
  258. break;
  259. case MBEDTLS_PK_RSA:
  260. case MBEDTLS_PK_RSASSA_PSS:
  261. key_usage = PSA_KEY_USAGE_SIGN_HASH |
  262. PSA_KEY_USAGE_SIGN_MESSAGE |
  263. PSA_KEY_USAGE_DECRYPT;
  264. break;
  265. default:
  266. /* Should never happen */
  267. return 0;
  268. }
  269. return (key_usage & usage) == usage;
  270. }
  271. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  272. psa_status_t status;
  273. status = psa_get_key_attributes(ctx->priv_id, &attributes);
  274. if (status != PSA_SUCCESS) {
  275. return 0;
  276. }
  277. psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
  278. /* Key's enrollment is available only when an Mbed TLS implementation of PSA
  279. * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
  280. * Even though we don't officially support using other implementations of PSA
  281. * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
  282. * separated. */
  283. #if defined(MBEDTLS_PSA_CRYPTO_C)
  284. psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
  285. #endif /* MBEDTLS_PSA_CRYPTO_C */
  286. key_usage = psa_get_key_usage_flags(&attributes);
  287. psa_reset_key_attributes(&attributes);
  288. if ((key_usage & usage) != usage) {
  289. return 0;
  290. }
  291. /*
  292. * Common case: the key alg [or alg2] only allows alg.
  293. * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
  294. * directly.
  295. * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
  296. * a fixed hash on key_alg [or key_alg2].
  297. */
  298. if (alg == key_alg) {
  299. return 1;
  300. }
  301. #if defined(MBEDTLS_PSA_CRYPTO_C)
  302. if (alg == key_alg2) {
  303. return 1;
  304. }
  305. #endif /* MBEDTLS_PSA_CRYPTO_C */
  306. /*
  307. * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
  308. * and alg is the same hash-and-sign family with any hash,
  309. * then alg is compliant with this key alg
  310. */
  311. if (PSA_ALG_IS_SIGN_HASH(alg)) {
  312. if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
  313. PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
  314. (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
  315. return 1;
  316. }
  317. #if defined(MBEDTLS_PSA_CRYPTO_C)
  318. if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
  319. PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
  320. (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
  321. return 1;
  322. }
  323. #endif /* MBEDTLS_PSA_CRYPTO_C */
  324. }
  325. return 0;
  326. }
  327. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  328. #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
  329. #if defined(MBEDTLS_RSA_C)
  330. static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
  331. int want_crypt)
  332. {
  333. if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
  334. if (want_crypt) {
  335. mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa);
  336. return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
  337. } else {
  338. return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
  339. }
  340. } else {
  341. if (want_crypt) {
  342. return PSA_ALG_RSA_PKCS1V15_CRYPT;
  343. } else {
  344. return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
  345. }
  346. }
  347. }
  348. #endif /* MBEDTLS_RSA_C */
  349. int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
  350. psa_key_usage_t usage,
  351. psa_key_attributes_t *attributes)
  352. {
  353. mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
  354. psa_key_usage_t more_usage = usage;
  355. if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
  356. more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
  357. } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
  358. more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
  359. } else if (usage == PSA_KEY_USAGE_DECRYPT) {
  360. more_usage |= PSA_KEY_USAGE_ENCRYPT;
  361. }
  362. more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
  363. int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
  364. usage == PSA_KEY_USAGE_VERIFY_HASH ||
  365. usage == PSA_KEY_USAGE_ENCRYPT);
  366. switch (pk_type) {
  367. #if defined(MBEDTLS_RSA_C)
  368. case MBEDTLS_PK_RSA:
  369. {
  370. int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
  371. switch (usage) {
  372. case PSA_KEY_USAGE_SIGN_MESSAGE:
  373. case PSA_KEY_USAGE_SIGN_HASH:
  374. case PSA_KEY_USAGE_VERIFY_MESSAGE:
  375. case PSA_KEY_USAGE_VERIFY_HASH:
  376. /* Nothing to do. */
  377. break;
  378. case PSA_KEY_USAGE_DECRYPT:
  379. case PSA_KEY_USAGE_ENCRYPT:
  380. want_crypt = 1;
  381. break;
  382. default:
  383. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  384. }
  385. /* Detect the presence of a private key in a way that works both
  386. * in CRT and non-CRT configurations. */
  387. mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
  388. int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
  389. if (want_private && !has_private) {
  390. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  391. }
  392. psa_set_key_type(attributes, (want_private ?
  393. PSA_KEY_TYPE_RSA_KEY_PAIR :
  394. PSA_KEY_TYPE_RSA_PUBLIC_KEY));
  395. psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
  396. psa_set_key_algorithm(attributes,
  397. psa_algorithm_for_rsa(rsa, want_crypt));
  398. break;
  399. }
  400. #endif /* MBEDTLS_RSA_C */
  401. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  402. case MBEDTLS_PK_ECKEY:
  403. case MBEDTLS_PK_ECKEY_DH:
  404. case MBEDTLS_PK_ECDSA:
  405. {
  406. int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
  407. int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
  408. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  409. psa_ecc_family_t family = pk->ec_family;
  410. size_t bits = pk->ec_bits;
  411. int has_private = 0;
  412. if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
  413. has_private = 1;
  414. }
  415. #else
  416. const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
  417. int has_private = (ec->d.n != 0);
  418. size_t bits = 0;
  419. psa_ecc_family_t family =
  420. mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
  421. #endif
  422. psa_algorithm_t alg = 0;
  423. switch (usage) {
  424. case PSA_KEY_USAGE_SIGN_MESSAGE:
  425. case PSA_KEY_USAGE_SIGN_HASH:
  426. case PSA_KEY_USAGE_VERIFY_MESSAGE:
  427. case PSA_KEY_USAGE_VERIFY_HASH:
  428. if (!sign_ok) {
  429. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  430. }
  431. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  432. alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
  433. #else
  434. alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
  435. #endif
  436. break;
  437. case PSA_KEY_USAGE_DERIVE:
  438. alg = PSA_ALG_ECDH;
  439. if (!derive_ok) {
  440. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  441. }
  442. break;
  443. default:
  444. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  445. }
  446. if (want_private && !has_private) {
  447. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  448. }
  449. psa_set_key_type(attributes, (want_private ?
  450. PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
  451. PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
  452. psa_set_key_bits(attributes, bits);
  453. psa_set_key_algorithm(attributes, alg);
  454. break;
  455. }
  456. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  457. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  458. case MBEDTLS_PK_RSA_ALT:
  459. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  460. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  461. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  462. case MBEDTLS_PK_OPAQUE:
  463. {
  464. psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
  465. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  466. status = psa_get_key_attributes(pk->priv_id, &old_attributes);
  467. if (status != PSA_SUCCESS) {
  468. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  469. }
  470. psa_key_type_t old_type = psa_get_key_type(&old_attributes);
  471. switch (usage) {
  472. case PSA_KEY_USAGE_SIGN_MESSAGE:
  473. case PSA_KEY_USAGE_SIGN_HASH:
  474. case PSA_KEY_USAGE_VERIFY_MESSAGE:
  475. case PSA_KEY_USAGE_VERIFY_HASH:
  476. if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
  477. old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
  478. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  479. }
  480. break;
  481. case PSA_KEY_USAGE_DECRYPT:
  482. case PSA_KEY_USAGE_ENCRYPT:
  483. if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
  484. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  485. }
  486. break;
  487. case PSA_KEY_USAGE_DERIVE:
  488. if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
  489. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  490. }
  491. break;
  492. default:
  493. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  494. }
  495. psa_key_type_t new_type = old_type;
  496. /* Opaque keys are always key pairs, so we don't need a check
  497. * on the input if the required usage is private. We just need
  498. * to adjust the type correctly if the required usage is public. */
  499. if (!want_private) {
  500. new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
  501. }
  502. more_usage = psa_get_key_usage_flags(&old_attributes);
  503. if ((usage & more_usage) == 0) {
  504. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  505. }
  506. psa_set_key_type(attributes, new_type);
  507. psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
  508. psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
  509. break;
  510. }
  511. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  512. default:
  513. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  514. }
  515. psa_set_key_usage_flags(attributes, more_usage);
  516. /* Key's enrollment is available only when an Mbed TLS implementation of PSA
  517. * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
  518. * Even though we don't officially support using other implementations of PSA
  519. * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
  520. * separated. */
  521. #if defined(MBEDTLS_PSA_CRYPTO_C)
  522. psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
  523. #endif
  524. return 0;
  525. }
  526. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO)
  527. static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
  528. const psa_key_attributes_t *attributes,
  529. mbedtls_svc_key_id_t *new_key_id)
  530. {
  531. unsigned char key_buffer[PSA_EXPORT_KEY_PAIR_MAX_SIZE];
  532. size_t key_length = 0;
  533. psa_status_t status = psa_export_key(old_key_id,
  534. key_buffer, sizeof(key_buffer),
  535. &key_length);
  536. if (status != PSA_SUCCESS) {
  537. return status;
  538. }
  539. status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
  540. mbedtls_platform_zeroize(key_buffer, key_length);
  541. return status;
  542. }
  543. static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
  544. const psa_key_attributes_t *attributes,
  545. mbedtls_svc_key_id_t *new_key_id)
  546. {
  547. /* Normally, we prefer copying: it's more efficient and works even
  548. * for non-exportable keys. */
  549. psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
  550. if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
  551. status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
  552. /* There are edge cases where copying won't work, but export+import
  553. * might:
  554. * - If the old key does not allow PSA_KEY_USAGE_COPY.
  555. * - If the old key's usage does not allow what attributes wants.
  556. * Because the key was intended for use in the pk module, and may
  557. * have had a policy chosen solely for what pk needs rather than
  558. * based on a detailed understanding of PSA policies, we are a bit
  559. * more liberal than psa_copy_key() here.
  560. */
  561. /* Here we need to check that the types match, otherwise we risk
  562. * importing nonsensical data. */
  563. psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
  564. status = psa_get_key_attributes(old_key_id, &old_attributes);
  565. if (status != PSA_SUCCESS) {
  566. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  567. }
  568. psa_key_type_t old_type = psa_get_key_type(&old_attributes);
  569. psa_reset_key_attributes(&old_attributes);
  570. if (old_type != psa_get_key_type(attributes)) {
  571. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  572. }
  573. status = export_import_into_psa(old_key_id, attributes, new_key_id);
  574. }
  575. return PSA_PK_TO_MBEDTLS_ERR(status);
  576. }
  577. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */
  578. static int import_pair_into_psa(const mbedtls_pk_context *pk,
  579. const psa_key_attributes_t *attributes,
  580. mbedtls_svc_key_id_t *key_id)
  581. {
  582. switch (mbedtls_pk_get_type(pk)) {
  583. #if defined(MBEDTLS_RSA_C)
  584. case MBEDTLS_PK_RSA:
  585. {
  586. if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
  587. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  588. }
  589. unsigned char key_buffer[
  590. PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)];
  591. unsigned char *const key_end = key_buffer + sizeof(key_buffer);
  592. unsigned char *key_data = key_end;
  593. int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk),
  594. key_buffer, &key_data);
  595. if (ret < 0) {
  596. return ret;
  597. }
  598. size_t key_length = key_end - key_data;
  599. ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
  600. key_data, key_length,
  601. key_id));
  602. mbedtls_platform_zeroize(key_data, key_length);
  603. return ret;
  604. }
  605. #endif /* MBEDTLS_RSA_C */
  606. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  607. case MBEDTLS_PK_ECKEY:
  608. case MBEDTLS_PK_ECKEY_DH:
  609. case MBEDTLS_PK_ECDSA:
  610. {
  611. /* We need to check the curve family, otherwise the import could
  612. * succeed with nonsensical data.
  613. * We don't check the bit-size: it's optional in attributes,
  614. * and if it's specified, psa_import_key() will know from the key
  615. * data length and will check that the bit-size matches. */
  616. psa_key_type_t to_type = psa_get_key_type(attributes);
  617. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  618. psa_ecc_family_t from_family = pk->ec_family;
  619. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  620. const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
  621. size_t from_bits = 0;
  622. psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
  623. &from_bits);
  624. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  625. if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
  626. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  627. }
  628. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  629. if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
  630. /* We have a public key and want a key pair. */
  631. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  632. }
  633. return copy_into_psa(pk->priv_id, attributes, key_id);
  634. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  635. if (ec->d.n == 0) {
  636. /* Private key not set. Assume the input is a public key only.
  637. * (The other possibility is that it's an incomplete object
  638. * where the group is set but neither the public key nor
  639. * the private key. This is not possible through ecp.h
  640. * functions, so we don't bother reporting a more suitable
  641. * error in that case.) */
  642. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  643. }
  644. unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
  645. size_t key_length = 0;
  646. int ret = mbedtls_ecp_write_key_ext(ec, &key_length,
  647. key_buffer, sizeof(key_buffer));
  648. if (ret < 0) {
  649. return ret;
  650. }
  651. ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
  652. key_buffer, key_length,
  653. key_id));
  654. mbedtls_platform_zeroize(key_buffer, key_length);
  655. return ret;
  656. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  657. }
  658. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  659. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  660. case MBEDTLS_PK_OPAQUE:
  661. return copy_into_psa(pk->priv_id, attributes, key_id);
  662. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  663. default:
  664. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  665. }
  666. }
  667. static int import_public_into_psa(const mbedtls_pk_context *pk,
  668. const psa_key_attributes_t *attributes,
  669. mbedtls_svc_key_id_t *key_id)
  670. {
  671. psa_key_type_t psa_type = psa_get_key_type(attributes);
  672. #if defined(MBEDTLS_RSA_C) || \
  673. (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \
  674. defined(MBEDTLS_USE_PSA_CRYPTO)
  675. unsigned char key_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
  676. #endif
  677. unsigned char *key_data = NULL;
  678. size_t key_length = 0;
  679. switch (mbedtls_pk_get_type(pk)) {
  680. #if defined(MBEDTLS_RSA_C)
  681. case MBEDTLS_PK_RSA:
  682. {
  683. if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
  684. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  685. }
  686. unsigned char *const key_end = key_buffer + sizeof(key_buffer);
  687. key_data = key_end;
  688. int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk),
  689. key_buffer, &key_data);
  690. if (ret < 0) {
  691. return ret;
  692. }
  693. key_length = (size_t) ret;
  694. break;
  695. }
  696. #endif /*MBEDTLS_RSA_C */
  697. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  698. case MBEDTLS_PK_ECKEY:
  699. case MBEDTLS_PK_ECKEY_DH:
  700. case MBEDTLS_PK_ECDSA:
  701. {
  702. /* We need to check the curve family, otherwise the import could
  703. * succeed with nonsensical data.
  704. * We don't check the bit-size: it's optional in attributes,
  705. * and if it's specified, psa_import_key() will know from the key
  706. * data length and will check that the bit-size matches. */
  707. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  708. if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
  709. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  710. }
  711. key_data = (unsigned char *) pk->pub_raw;
  712. key_length = pk->pub_raw_len;
  713. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  714. const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
  715. size_t from_bits = 0;
  716. psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
  717. &from_bits);
  718. if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) {
  719. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  720. }
  721. int ret = mbedtls_ecp_write_public_key(
  722. ec, MBEDTLS_ECP_PF_UNCOMPRESSED,
  723. &key_length, key_buffer, sizeof(key_buffer));
  724. if (ret < 0) {
  725. return ret;
  726. }
  727. key_data = key_buffer;
  728. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  729. break;
  730. }
  731. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  732. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  733. case MBEDTLS_PK_OPAQUE:
  734. {
  735. psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
  736. psa_status_t status =
  737. psa_get_key_attributes(pk->priv_id, &old_attributes);
  738. if (status != PSA_SUCCESS) {
  739. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  740. }
  741. psa_key_type_t old_type = psa_get_key_type(&old_attributes);
  742. psa_reset_key_attributes(&old_attributes);
  743. if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
  744. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  745. }
  746. status = psa_export_public_key(pk->priv_id,
  747. key_buffer, sizeof(key_buffer),
  748. &key_length);
  749. if (status != PSA_SUCCESS) {
  750. return PSA_PK_TO_MBEDTLS_ERR(status);
  751. }
  752. key_data = key_buffer;
  753. break;
  754. }
  755. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  756. default:
  757. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  758. }
  759. return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
  760. key_data, key_length,
  761. key_id));
  762. }
  763. int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
  764. const psa_key_attributes_t *attributes,
  765. mbedtls_svc_key_id_t *key_id)
  766. {
  767. /* Set the output immediately so that it won't contain garbage even
  768. * if we error out before calling psa_import_key(). */
  769. *key_id = MBEDTLS_SVC_KEY_ID_INIT;
  770. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  771. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) {
  772. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  773. }
  774. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  775. int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
  776. if (want_public) {
  777. return import_public_into_psa(pk, attributes, key_id);
  778. } else {
  779. return import_pair_into_psa(pk, attributes, key_id);
  780. }
  781. }
  782. static int copy_from_psa(mbedtls_svc_key_id_t key_id,
  783. mbedtls_pk_context *pk,
  784. int public_only)
  785. {
  786. psa_status_t status;
  787. psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
  788. psa_key_type_t key_type;
  789. size_t key_bits;
  790. /* Use a buffer size large enough to contain either a key pair or public key. */
  791. unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE];
  792. size_t exp_key_len;
  793. int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  794. if (pk == NULL) {
  795. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  796. }
  797. status = psa_get_key_attributes(key_id, &key_attr);
  798. if (status != PSA_SUCCESS) {
  799. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  800. }
  801. if (public_only) {
  802. status = psa_export_public_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
  803. } else {
  804. status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
  805. }
  806. if (status != PSA_SUCCESS) {
  807. ret = PSA_PK_TO_MBEDTLS_ERR(status);
  808. goto exit;
  809. }
  810. key_type = psa_get_key_type(&key_attr);
  811. if (public_only) {
  812. key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
  813. }
  814. key_bits = psa_get_key_bits(&key_attr);
  815. #if defined(MBEDTLS_RSA_C)
  816. if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
  817. (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
  818. ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
  819. if (ret != 0) {
  820. goto exit;
  821. }
  822. if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
  823. ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
  824. } else {
  825. ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
  826. }
  827. if (ret != 0) {
  828. goto exit;
  829. }
  830. psa_algorithm_t alg_type = psa_get_key_algorithm(&key_attr);
  831. mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
  832. if (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH) {
  833. md_type = mbedtls_md_type_from_psa_alg(alg_type);
  834. }
  835. if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
  836. ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
  837. } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
  838. alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
  839. ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
  840. }
  841. if (ret != 0) {
  842. goto exit;
  843. }
  844. } else
  845. #endif /* MBEDTLS_RSA_C */
  846. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  847. if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
  848. PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
  849. mbedtls_ecp_group_id grp_id;
  850. ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
  851. if (ret != 0) {
  852. goto exit;
  853. }
  854. grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
  855. ret = mbedtls_pk_ecc_set_group(pk, grp_id);
  856. if (ret != 0) {
  857. goto exit;
  858. }
  859. if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
  860. ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
  861. if (ret != 0) {
  862. goto exit;
  863. }
  864. ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
  865. mbedtls_psa_get_random,
  866. MBEDTLS_PSA_RANDOM_STATE);
  867. } else {
  868. ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
  869. }
  870. if (ret != 0) {
  871. goto exit;
  872. }
  873. } else
  874. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  875. {
  876. (void) key_bits;
  877. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  878. }
  879. exit:
  880. psa_reset_key_attributes(&key_attr);
  881. mbedtls_platform_zeroize(exp_key, sizeof(exp_key));
  882. return ret;
  883. }
  884. int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
  885. mbedtls_pk_context *pk)
  886. {
  887. return copy_from_psa(key_id, pk, 0);
  888. }
  889. int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
  890. mbedtls_pk_context *pk)
  891. {
  892. return copy_from_psa(key_id, pk, 1);
  893. }
  894. #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
  895. /*
  896. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  897. */
  898. static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
  899. {
  900. if (*hash_len != 0) {
  901. return 0;
  902. }
  903. *hash_len = mbedtls_md_get_size_from_type(md_alg);
  904. if (*hash_len == 0) {
  905. return -1;
  906. }
  907. return 0;
  908. }
  909. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  910. /*
  911. * Helper to set up a restart context if needed
  912. */
  913. static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
  914. const mbedtls_pk_info_t *info)
  915. {
  916. /* Don't do anything if already set up or invalid */
  917. if (ctx == NULL || ctx->pk_info != NULL) {
  918. return 0;
  919. }
  920. /* Should never happen when we're called */
  921. if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
  922. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  923. }
  924. if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
  925. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  926. }
  927. ctx->pk_info = info;
  928. return 0;
  929. }
  930. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  931. /*
  932. * Verify a signature (restartable)
  933. */
  934. int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
  935. mbedtls_md_type_t md_alg,
  936. const unsigned char *hash, size_t hash_len,
  937. const unsigned char *sig, size_t sig_len,
  938. mbedtls_pk_restart_ctx *rs_ctx)
  939. {
  940. if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
  941. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  942. }
  943. if (ctx->pk_info == NULL ||
  944. pk_hashlen_helper(md_alg, &hash_len) != 0) {
  945. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  946. }
  947. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  948. /* optimization: use non-restartable version if restart disabled */
  949. if (rs_ctx != NULL &&
  950. mbedtls_ecp_restart_is_enabled() &&
  951. ctx->pk_info->verify_rs_func != NULL) {
  952. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  953. if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
  954. return ret;
  955. }
  956. ret = ctx->pk_info->verify_rs_func(ctx,
  957. md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
  958. if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
  959. mbedtls_pk_restart_free(rs_ctx);
  960. }
  961. return ret;
  962. }
  963. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  964. (void) rs_ctx;
  965. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  966. if (ctx->pk_info->verify_func == NULL) {
  967. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  968. }
  969. return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
  970. sig, sig_len);
  971. }
  972. /*
  973. * Verify a signature
  974. */
  975. int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  976. const unsigned char *hash, size_t hash_len,
  977. const unsigned char *sig, size_t sig_len)
  978. {
  979. return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
  980. sig, sig_len, NULL);
  981. }
  982. /*
  983. * Verify a signature with options
  984. */
  985. int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
  986. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  987. const unsigned char *hash, size_t hash_len,
  988. const unsigned char *sig, size_t sig_len)
  989. {
  990. if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
  991. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  992. }
  993. if (ctx->pk_info == NULL) {
  994. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  995. }
  996. if (!mbedtls_pk_can_do(ctx, type)) {
  997. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  998. }
  999. if (type != MBEDTLS_PK_RSASSA_PSS) {
  1000. /* General case: no options */
  1001. if (options != NULL) {
  1002. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1003. }
  1004. return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
  1005. }
  1006. /* Ensure the PK context is of the right type otherwise mbedtls_pk_rsa()
  1007. * below would return a NULL pointer. */
  1008. if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_RSA) {
  1009. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  1010. }
  1011. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  1012. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1013. const mbedtls_pk_rsassa_pss_options *pss_opts;
  1014. #if SIZE_MAX > UINT_MAX
  1015. if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
  1016. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1017. }
  1018. #endif
  1019. if (options == NULL) {
  1020. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1021. }
  1022. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  1023. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1024. if (pss_opts->mgf1_hash_id == md_alg) {
  1025. unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
  1026. unsigned char *p;
  1027. int key_len;
  1028. size_t signature_length;
  1029. psa_status_t status = PSA_ERROR_DATA_CORRUPT;
  1030. psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
  1031. psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
  1032. mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
  1033. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1034. psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
  1035. p = buf + sizeof(buf);
  1036. key_len = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*ctx), buf, &p);
  1037. if (key_len < 0) {
  1038. return key_len;
  1039. }
  1040. psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
  1041. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  1042. psa_set_key_algorithm(&attributes, psa_sig_alg);
  1043. status = psa_import_key(&attributes,
  1044. buf + sizeof(buf) - key_len, key_len,
  1045. &key_id);
  1046. if (status != PSA_SUCCESS) {
  1047. psa_destroy_key(key_id);
  1048. return PSA_PK_TO_MBEDTLS_ERR(status);
  1049. }
  1050. /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
  1051. * on a valid signature with trailing data in a buffer, but
  1052. * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
  1053. * so for this reason the passed sig_len is overwritten. Smaller
  1054. * signature lengths should not be accepted for verification. */
  1055. signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
  1056. mbedtls_pk_get_len(ctx) : sig_len;
  1057. status = psa_verify_hash(key_id, psa_sig_alg, hash,
  1058. hash_len, sig, signature_length);
  1059. destruction_status = psa_destroy_key(key_id);
  1060. if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
  1061. return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
  1062. }
  1063. if (status == PSA_SUCCESS) {
  1064. status = destruction_status;
  1065. }
  1066. return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
  1067. } else
  1068. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1069. {
  1070. if (sig_len < mbedtls_pk_get_len(ctx)) {
  1071. return MBEDTLS_ERR_RSA_VERIFY_FAILED;
  1072. }
  1073. ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
  1074. md_alg, (unsigned int) hash_len, hash,
  1075. pss_opts->mgf1_hash_id,
  1076. pss_opts->expected_salt_len,
  1077. sig);
  1078. if (ret != 0) {
  1079. return ret;
  1080. }
  1081. if (sig_len > mbedtls_pk_get_len(ctx)) {
  1082. return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
  1083. }
  1084. return 0;
  1085. }
  1086. #else
  1087. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  1088. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  1089. }
  1090. /*
  1091. * Make a signature (restartable)
  1092. */
  1093. int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
  1094. mbedtls_md_type_t md_alg,
  1095. const unsigned char *hash, size_t hash_len,
  1096. unsigned char *sig, size_t sig_size, size_t *sig_len,
  1097. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  1098. mbedtls_pk_restart_ctx *rs_ctx)
  1099. {
  1100. if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
  1101. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1102. }
  1103. if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
  1104. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1105. }
  1106. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  1107. /* optimization: use non-restartable version if restart disabled */
  1108. if (rs_ctx != NULL &&
  1109. mbedtls_ecp_restart_is_enabled() &&
  1110. ctx->pk_info->sign_rs_func != NULL) {
  1111. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1112. if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
  1113. return ret;
  1114. }
  1115. ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
  1116. hash, hash_len,
  1117. sig, sig_size, sig_len,
  1118. f_rng, p_rng, rs_ctx->rs_ctx);
  1119. if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
  1120. mbedtls_pk_restart_free(rs_ctx);
  1121. }
  1122. return ret;
  1123. }
  1124. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  1125. (void) rs_ctx;
  1126. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  1127. if (ctx->pk_info->sign_func == NULL) {
  1128. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  1129. }
  1130. return ctx->pk_info->sign_func(ctx, md_alg,
  1131. hash, hash_len,
  1132. sig, sig_size, sig_len,
  1133. f_rng, p_rng);
  1134. }
  1135. /*
  1136. * Make a signature
  1137. */
  1138. int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  1139. const unsigned char *hash, size_t hash_len,
  1140. unsigned char *sig, size_t sig_size, size_t *sig_len,
  1141. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  1142. {
  1143. return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
  1144. sig, sig_size, sig_len,
  1145. f_rng, p_rng, NULL);
  1146. }
  1147. /*
  1148. * Make a signature given a signature type.
  1149. */
  1150. int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
  1151. mbedtls_pk_context *ctx,
  1152. mbedtls_md_type_t md_alg,
  1153. const unsigned char *hash, size_t hash_len,
  1154. unsigned char *sig, size_t sig_size, size_t *sig_len,
  1155. int (*f_rng)(void *, unsigned char *, size_t),
  1156. void *p_rng)
  1157. {
  1158. if (ctx->pk_info == NULL) {
  1159. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1160. }
  1161. if (!mbedtls_pk_can_do(ctx, pk_type)) {
  1162. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  1163. }
  1164. if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
  1165. return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
  1166. sig, sig_size, sig_len, f_rng, p_rng);
  1167. }
  1168. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  1169. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1170. const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
  1171. if (psa_md_alg == 0) {
  1172. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1173. }
  1174. if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
  1175. psa_status_t status;
  1176. /* PSA_ALG_RSA_PSS() behaves the same as PSA_ALG_RSA_PSS_ANY_SALT() when
  1177. * performing a signature, but they are encoded differently. Instead of
  1178. * extracting the proper one from the wrapped key policy, just try both. */
  1179. status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
  1180. hash, hash_len,
  1181. sig, sig_size, sig_len);
  1182. if (status == PSA_ERROR_NOT_PERMITTED) {
  1183. status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg),
  1184. hash, hash_len,
  1185. sig, sig_size, sig_len);
  1186. }
  1187. return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
  1188. }
  1189. return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
  1190. ctx->pk_ctx, hash, hash_len,
  1191. sig, sig_size, sig_len);
  1192. #else /* MBEDTLS_USE_PSA_CRYPTO */
  1193. if (sig_size < mbedtls_pk_get_len(ctx)) {
  1194. return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
  1195. }
  1196. if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
  1197. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1198. }
  1199. mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
  1200. const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
  1201. (unsigned int) hash_len, hash, sig);
  1202. if (ret == 0) {
  1203. *sig_len = rsa_ctx->len;
  1204. }
  1205. return ret;
  1206. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1207. #else
  1208. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  1209. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  1210. }
  1211. /*
  1212. * Decrypt message
  1213. */
  1214. int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
  1215. const unsigned char *input, size_t ilen,
  1216. unsigned char *output, size_t *olen, size_t osize,
  1217. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  1218. {
  1219. if (ctx->pk_info == NULL) {
  1220. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1221. }
  1222. if (ctx->pk_info->decrypt_func == NULL) {
  1223. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  1224. }
  1225. return ctx->pk_info->decrypt_func(ctx, input, ilen,
  1226. output, olen, osize, f_rng, p_rng);
  1227. }
  1228. /*
  1229. * Encrypt message
  1230. */
  1231. int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
  1232. const unsigned char *input, size_t ilen,
  1233. unsigned char *output, size_t *olen, size_t osize,
  1234. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  1235. {
  1236. if (ctx->pk_info == NULL) {
  1237. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1238. }
  1239. if (ctx->pk_info->encrypt_func == NULL) {
  1240. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  1241. }
  1242. return ctx->pk_info->encrypt_func(ctx, input, ilen,
  1243. output, olen, osize, f_rng, p_rng);
  1244. }
  1245. /*
  1246. * Check public-private key pair
  1247. */
  1248. int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
  1249. const mbedtls_pk_context *prv,
  1250. int (*f_rng)(void *, unsigned char *, size_t),
  1251. void *p_rng)
  1252. {
  1253. if (pub->pk_info == NULL ||
  1254. prv->pk_info == NULL) {
  1255. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1256. }
  1257. if (f_rng == NULL) {
  1258. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1259. }
  1260. if (prv->pk_info->check_pair_func == NULL) {
  1261. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  1262. }
  1263. if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
  1264. if (pub->pk_info->type != MBEDTLS_PK_RSA) {
  1265. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  1266. }
  1267. } else {
  1268. if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
  1269. (pub->pk_info != prv->pk_info)) {
  1270. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  1271. }
  1272. }
  1273. return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
  1274. (mbedtls_pk_context *) prv,
  1275. f_rng, p_rng);
  1276. }
  1277. /*
  1278. * Get key size in bits
  1279. */
  1280. size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
  1281. {
  1282. /* For backward compatibility, accept NULL or a context that
  1283. * isn't set up yet, and return a fake value that should be safe. */
  1284. if (ctx == NULL || ctx->pk_info == NULL) {
  1285. return 0;
  1286. }
  1287. return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
  1288. }
  1289. /*
  1290. * Export debug information
  1291. */
  1292. int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
  1293. {
  1294. if (ctx->pk_info == NULL) {
  1295. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  1296. }
  1297. if (ctx->pk_info->debug_func == NULL) {
  1298. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  1299. }
  1300. ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
  1301. return 0;
  1302. }
  1303. /*
  1304. * Access the PK type name
  1305. */
  1306. const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
  1307. {
  1308. if (ctx == NULL || ctx->pk_info == NULL) {
  1309. return "invalid PK";
  1310. }
  1311. return ctx->pk_info->name;
  1312. }
  1313. /*
  1314. * Access the PK type
  1315. */
  1316. mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
  1317. {
  1318. if (ctx == NULL || ctx->pk_info == NULL) {
  1319. return MBEDTLS_PK_NONE;
  1320. }
  1321. return ctx->pk_info->type;
  1322. }
  1323. #endif /* MBEDTLS_PK_C */