mbedtls_sign.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * Copyright (c) 2023, Christian Huitema
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #ifdef _WINDOWS
  23. #include "wincompat.h"
  24. #endif
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <picotls.h>
  29. #include <mbedtls/mbedtls_config.h>
  30. #include <mbedtls/build_info.h>
  31. #include <mbedtls/pk.h>
  32. #include <mbedtls/pem.h>
  33. #include <mbedtls/error.h>
  34. #include <psa/crypto.h>
  35. #include <psa/crypto_struct.h>
  36. #include <psa/crypto_values.h>
  37. /* #include "ptls_mbedtls.h" */
  38. typedef struct st_ptls_mbedtls_signature_scheme_t {
  39. uint16_t scheme_id;
  40. psa_algorithm_t hash_algo;
  41. } ptls_mbedtls_signature_scheme_t;
  42. typedef struct st_ptls_mbedtls_sign_certificate_t {
  43. ptls_sign_certificate_t super;
  44. mbedtls_svc_key_id_t key_id;
  45. psa_key_attributes_t attributes;
  46. const ptls_mbedtls_signature_scheme_t *schemes;
  47. } ptls_mbedtls_sign_certificate_t;
  48. static const unsigned char ptls_mbedtls_oid_ec_key[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01};
  49. static const unsigned char ptls_mbedtls_oid_rsa_key[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01};
  50. static const unsigned char ptls_mbedtls_oid_ed25519[] = {0x2b, 0x65, 0x70};
  51. static const ptls_mbedtls_signature_scheme_t rsa_signature_schemes[] = {{PTLS_SIGNATURE_RSA_PSS_RSAE_SHA256, PSA_ALG_SHA_256},
  52. {PTLS_SIGNATURE_RSA_PSS_RSAE_SHA384, PSA_ALG_SHA_384},
  53. {PTLS_SIGNATURE_RSA_PSS_RSAE_SHA512, PSA_ALG_SHA_512},
  54. {UINT16_MAX, PSA_ALG_NONE}};
  55. static const ptls_mbedtls_signature_scheme_t secp256r1_signature_schemes[] = {
  56. {PTLS_SIGNATURE_ECDSA_SECP256R1_SHA256, PSA_ALG_SHA_256}, {UINT16_MAX, PSA_ALG_NONE}};
  57. static const ptls_mbedtls_signature_scheme_t secp384r1_signature_schemes[] = {
  58. {PTLS_SIGNATURE_ECDSA_SECP384R1_SHA384, PSA_ALG_SHA_384}, {UINT16_MAX, PSA_ALG_NONE}};
  59. static const ptls_mbedtls_signature_scheme_t secp521r1_signature_schemes[] = {
  60. {PTLS_SIGNATURE_ECDSA_SECP521R1_SHA512, PSA_ALG_SHA_512}, {UINT16_MAX, PSA_ALG_NONE}};
  61. static const ptls_mbedtls_signature_scheme_t ed25519_signature_schemes[] = {{PTLS_SIGNATURE_ED25519, PSA_ALG_NONE},
  62. {UINT16_MAX, PSA_ALG_NONE}};
  63. #if defined(MBEDTLS_PEM_PARSE_C)
  64. /* Mapping of MBEDTLS APIs to Picotls */
  65. static int ptls_mbedtls_parse_der_length(const unsigned char *pem_buf, size_t pem_len, size_t *px, size_t *pl)
  66. {
  67. int ret = 0;
  68. size_t x = *px;
  69. size_t l = pem_buf[x++];
  70. if (l > 128) {
  71. size_t ll = l & 0x7F;
  72. l = 0;
  73. while (ll > 0 && x + l < pem_len) {
  74. l *= 256;
  75. l += pem_buf[x++];
  76. ll--;
  77. }
  78. }
  79. *pl = l;
  80. *px = x;
  81. return ret;
  82. }
  83. static int ptls_mbedtls_parse_ecdsa_field(const unsigned char *pem_buf, size_t pem_len, size_t *key_index, size_t *key_length)
  84. {
  85. int ret = 0;
  86. size_t x = 0;
  87. // const unsigned char head = { 0x30, l-2, 0x02, 0x01, 0x01, 0x04 }
  88. if (pem_len < 16 || pem_buf[x++] != 0x30 /* type = sequence */) {
  89. ret = -1;
  90. } else {
  91. size_t l = 0;
  92. ret = ptls_mbedtls_parse_der_length(pem_buf, pem_len, &x, &l);
  93. if (x + l != pem_len) {
  94. ret = -1;
  95. }
  96. }
  97. if (ret == 0) {
  98. if (pem_buf[x++] != 0x02 /* type = int */ || pem_buf[x++] != 0x01 /* length of int = 1 */ ||
  99. pem_buf[x++] != 0x01 /* version = 1 */ || pem_buf[x++] != 0x04 /*octet string */ || pem_buf[x] + x >= pem_len) {
  100. ret = -1;
  101. } else {
  102. *key_index = x + 1;
  103. *key_length = pem_buf[x];
  104. x += 1 + pem_buf[x];
  105. if (x < pem_len && pem_buf[x] == 0xa0) {
  106. /* decode the EC parameters, identify the curve */
  107. x++;
  108. if (x + pem_buf[x] >= pem_len) {
  109. /* EC parameters extend beyond buffer */
  110. ret = -1;
  111. } else {
  112. x += pem_buf[x] + 1;
  113. }
  114. }
  115. if (ret == 0 && x < pem_len) {
  116. /* skip the public key parameter */
  117. if (pem_buf[x++] != 0xa1 || x >= pem_len) {
  118. ret = -1;
  119. } else {
  120. size_t l = 0;
  121. ret = ptls_mbedtls_parse_der_length(pem_buf, pem_len, &x, &l);
  122. x += l;
  123. }
  124. }
  125. if (x != pem_len) {
  126. ret = -1;
  127. }
  128. }
  129. }
  130. return ret;
  131. }
  132. /* On input, key_index points at the "key information" in a
  133. * "private key" message. For EDDSA, this contains an
  134. * octet string carrying the key itself. On return, key index
  135. * and key length are updated to point at the key field.
  136. */
  137. static int ptls_mbedtls_parse_eddsa_key(const unsigned char *pem_buf, size_t pem_len, size_t *key_index, size_t *key_length)
  138. {
  139. int ret = 0;
  140. size_t x = *key_index;
  141. size_t l_key = 0;
  142. if (*key_length < 2 || pem_buf[x++] != 0x04) {
  143. ret = -1;
  144. } else {
  145. ret = ptls_mbedtls_parse_der_length(pem_buf, pem_len, &x, &l_key);
  146. if (x + l_key != *key_index + *key_length) {
  147. ret = -1;
  148. } else {
  149. *key_index = x;
  150. *key_length = l_key;
  151. }
  152. }
  153. return ret;
  154. }
  155. /* If using PKCS8 encoding, the "private key" field contains the
  156. * same "ecdsa field" found in PEM "EC PRIVATE KEY" files. We
  157. * use the same parser, but we need to reset indices so they
  158. * reflect the unwrapped key.
  159. */
  160. int ptls_mbedtls_parse_ec_private_key(const unsigned char *pem_buf, size_t pem_len, size_t *key_index, size_t *key_length)
  161. {
  162. size_t x_offset = 0;
  163. size_t x_len = 0;
  164. int ret = ptls_mbedtls_parse_ecdsa_field(pem_buf + *key_index, *key_length, &x_offset, &x_len);
  165. if (ret == 0) {
  166. *key_index += x_offset;
  167. *key_length = x_len;
  168. }
  169. return ret;
  170. }
  171. int test_parse_private_key_field(const unsigned char *pem_buf, size_t pem_len, size_t *oid_index, size_t *oid_length,
  172. size_t *key_index, size_t *key_length)
  173. {
  174. int ret = 0;
  175. size_t l_oid = 0;
  176. size_t x_oid = 0;
  177. size_t l_key = 0;
  178. size_t x_key = 0;
  179. size_t x = 0;
  180. /* const unsigned char head = {0x30, l - 2, 0x02, 0x01, 0x00} */
  181. if (pem_len < 16 || pem_buf[x++] != 0x30 /* type = sequence */) {
  182. ret = -1;
  183. } else {
  184. size_t l = 0;
  185. ret = ptls_mbedtls_parse_der_length(pem_buf, pem_len, &x, &l);
  186. if (x + l != pem_len) {
  187. ret = -1;
  188. }
  189. }
  190. if (ret == 0) {
  191. if (pem_buf[x++] != 0x02 /* type = int */ || pem_buf[x++] != 0x01 /* length of int = 1 */ ||
  192. pem_buf[x++] != 0x00 /* version = 0 */ || pem_buf[x++] != 0x30 /* sequence */) {
  193. ret = -1;
  194. } else {
  195. /* the sequence contains the OID and optional key attributes,
  196. * which we ignore for now.
  197. */
  198. size_t l_seq = 0;
  199. size_t x_seq;
  200. ret = ptls_mbedtls_parse_der_length(pem_buf, pem_len, &x, &l_seq);
  201. x_seq = x;
  202. if (x + l_seq >= pem_len || pem_buf[x++] != 0x06) {
  203. ret = -1;
  204. } else {
  205. l_oid = pem_buf[x++];
  206. x_oid = x;
  207. if (x + l_oid > x_seq + l_seq) {
  208. ret = -1;
  209. } else {
  210. x = x_seq + l_seq;
  211. }
  212. }
  213. }
  214. }
  215. if (ret == 0) {
  216. /* At that point the oid has been identified.
  217. * The next parameter is an octet string containing the key info.
  218. */
  219. if (x + 2 > pem_len || pem_buf[x++] != 0x04) {
  220. ret = -1;
  221. } else {
  222. ret = ptls_mbedtls_parse_der_length(pem_buf, pem_len, &x, &l_key);
  223. x_key = x;
  224. x += l_key;
  225. if (x > pem_len) {
  226. ret = -1;
  227. }
  228. }
  229. }
  230. *oid_index = x_oid;
  231. *oid_length = l_oid;
  232. *key_index = x_key;
  233. *key_length = l_key;
  234. return ret;
  235. }
  236. int ptls_mbedtls_get_der_key(mbedtls_pem_context *pem, mbedtls_pk_type_t *pk_type, const unsigned char *key, size_t keylen,
  237. const unsigned char *pwd, size_t pwdlen, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  238. {
  239. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  240. #if defined(MBEDTLS_PEM_PARSE_C)
  241. size_t len;
  242. #endif
  243. if (keylen == 0) {
  244. return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  245. }
  246. mbedtls_pem_init(pem);
  247. #if defined(MBEDTLS_RSA_C)
  248. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  249. if (key[keylen - 1] != '\0') {
  250. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  251. } else {
  252. ret = mbedtls_pem_read_buffer(pem, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----", key, pwd, pwdlen,
  253. &len);
  254. }
  255. if (ret == 0) {
  256. *pk_type = MBEDTLS_PK_RSA;
  257. return ret;
  258. } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
  259. return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
  260. } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
  261. return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
  262. } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
  263. return ret;
  264. }
  265. #endif /* MBEDTLS_RSA_C */
  266. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  267. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  268. if (key[keylen - 1] != '\0') {
  269. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  270. } else {
  271. ret =
  272. mbedtls_pem_read_buffer(pem, "-----BEGIN EC PRIVATE KEY-----", "-----END EC PRIVATE KEY-----", key, pwd, pwdlen, &len);
  273. }
  274. if (ret == 0) {
  275. *pk_type = MBEDTLS_PK_ECKEY;
  276. return ret;
  277. } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
  278. return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
  279. } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
  280. return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
  281. } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
  282. return ret;
  283. }
  284. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  285. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  286. if (key[keylen - 1] != '\0') {
  287. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  288. } else {
  289. ret = mbedtls_pem_read_buffer(pem, "-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----", key, NULL, 0, &len);
  290. if (ret == 0) {
  291. /* info is unknown */
  292. return ret;
  293. } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
  294. return ret;
  295. }
  296. }
  297. #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  298. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  299. if (key[keylen - 1] != '\0') {
  300. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  301. } else {
  302. ret = mbedtls_pem_read_buffer(pem, "-----BEGIN ENCRYPTED PRIVATE KEY-----", "-----END ENCRYPTED PRIVATE KEY-----", key,
  303. NULL, 0, &len);
  304. }
  305. if (ret == 0) {
  306. /* infor is unknown */
  307. return ret;
  308. } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
  309. return ret;
  310. }
  311. #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
  312. return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  313. }
  314. #endif
  315. const ptls_mbedtls_signature_scheme_t *ptls_mbedtls_select_signature_scheme(const ptls_mbedtls_signature_scheme_t *available,
  316. const uint16_t *algorithms, size_t num_algorithms)
  317. {
  318. const ptls_mbedtls_signature_scheme_t *scheme;
  319. /* select the algorithm, driven by server-isde preference of `available` */
  320. for (scheme = available; scheme->scheme_id != UINT16_MAX; ++scheme) {
  321. for (size_t i = 0; i != num_algorithms; ++i) {
  322. if (algorithms[i] == scheme->scheme_id) {
  323. return scheme;
  324. }
  325. }
  326. }
  327. return NULL;
  328. }
  329. int ptls_mbedtls_set_available_schemes(ptls_mbedtls_sign_certificate_t *signer)
  330. {
  331. int ret = 0;
  332. psa_algorithm_t algo = psa_get_key_algorithm(&signer->attributes);
  333. size_t nb_bits = psa_get_key_bits(&signer->attributes);
  334. switch (algo) {
  335. case PSA_ALG_RSA_PKCS1V15_SIGN_RAW:
  336. signer->schemes = rsa_signature_schemes;
  337. break;
  338. case PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256):
  339. signer->schemes = secp256r1_signature_schemes;
  340. break;
  341. case PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_384):
  342. signer->schemes = secp384r1_signature_schemes;
  343. break;
  344. case PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_512):
  345. signer->schemes = secp521r1_signature_schemes;
  346. break;
  347. case PSA_ALG_ECDSA_BASE:
  348. switch (nb_bits) {
  349. case 521:
  350. signer->schemes = secp521r1_signature_schemes;
  351. break;
  352. case 384:
  353. signer->schemes = secp384r1_signature_schemes;
  354. break;
  355. case 256:
  356. signer->schemes = secp256r1_signature_schemes;
  357. break;
  358. default:
  359. signer->schemes = secp256r1_signature_schemes;
  360. ret = -1;
  361. break;
  362. }
  363. break;
  364. case PSA_ALG_ED25519PH:
  365. signer->schemes = ed25519_signature_schemes;
  366. break;
  367. default:
  368. printf("Unknown algo: %x\n", algo);
  369. ret = -1;
  370. }
  371. return ret;
  372. }
  373. /*
  374. * Sign a certificate
  375. * - step1, selected a signature algorithm compatible with the public key algorithm
  376. * and with the list specified by the application.
  377. * - step2, compute the hash with the specified algorithm.
  378. * - step3, compute the signature of the hash using psa_sign_hash.
  379. *
  380. * In the case of RSA, we use the algorithm PSA_ALG_RSA_PKCS1V15_SIGN_RAW, which
  381. * pads the hash according to PKCS1V15 before doing the private key operation.
  382. * The implementation of RSA/PKCS1V15 also includes a verification step to protect
  383. * against key attacks through partial faults.
  384. *
  385. * MBEDTLS has a "psa_sign_message" that combines step2 and step3. However, it
  386. * requires specifying an algorithm type that exactly specifies the signature
  387. * algorithm, such as "RSA with SHA384". This is not compatible with the
  388. * "RSA sign raw" algorithm. Instead, we decompose the operation in two steps.
  389. * There is no performance penalty doing so, as "psa_sign_message" is only
  390. * a convenience API.
  391. */
  392. int ptls_mbedtls_sign_certificate(ptls_sign_certificate_t *_self, ptls_t *tls, ptls_async_job_t **async,
  393. uint16_t *selected_algorithm, ptls_buffer_t *outbuf, ptls_iovec_t input,
  394. const uint16_t *algorithms, size_t num_algorithms)
  395. {
  396. int ret = 0;
  397. ptls_mbedtls_sign_certificate_t *self =
  398. (ptls_mbedtls_sign_certificate_t *)(((unsigned char *)_self) - offsetof(struct st_ptls_mbedtls_sign_certificate_t, super));
  399. /* First, find the set of compatible algorithms */
  400. const ptls_mbedtls_signature_scheme_t *scheme = ptls_mbedtls_select_signature_scheme(self->schemes, algorithms, num_algorithms);
  401. if (scheme == NULL) {
  402. ret = PTLS_ERROR_INCOMPATIBLE_KEY;
  403. } else {
  404. /* First prepare the hash */
  405. unsigned char hash_buffer[PTLS_MAX_DIGEST_SIZE];
  406. unsigned char *hash_value = NULL;
  407. size_t hash_length = 0;
  408. if (scheme->hash_algo == PSA_ALG_NONE) {
  409. hash_value = input.base;
  410. hash_length = input.len;
  411. } else {
  412. if (psa_hash_compute(scheme->hash_algo, input.base, input.len, hash_buffer, PTLS_MAX_DIGEST_SIZE, &hash_length) !=
  413. PSA_SUCCESS) {
  414. ret = PTLS_ERROR_NOT_AVAILABLE;
  415. } else {
  416. hash_value = hash_buffer;
  417. }
  418. }
  419. if (ret == 0) {
  420. psa_algorithm_t sign_algo = psa_get_key_algorithm(&self->attributes);
  421. size_t nb_bits = psa_get_key_bits(&self->attributes);
  422. size_t nb_bytes = (nb_bits + 7) / 8;
  423. if (nb_bits == 0) {
  424. if (sign_algo == PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
  425. /* assume at most 4096 bit key */
  426. nb_bytes = 512;
  427. } else {
  428. /* Max size assumed, secp521r1 */
  429. nb_bytes = 124;
  430. }
  431. } else if (sign_algo != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
  432. nb_bytes *= 2;
  433. }
  434. if ((ret = ptls_buffer_reserve(outbuf, nb_bytes)) == 0) {
  435. size_t signature_length = 0;
  436. if (psa_sign_hash(self->key_id, sign_algo, hash_value, hash_length, outbuf->base + outbuf->off, nb_bytes,
  437. &signature_length) != 0) {
  438. ret = PTLS_ERROR_INCOMPATIBLE_KEY;
  439. } else {
  440. outbuf->off += signature_length;
  441. }
  442. }
  443. }
  444. }
  445. return ret;
  446. }
  447. void ptls_mbedtls_dispose_sign_certificate(ptls_sign_certificate_t *_self)
  448. {
  449. if (_self != NULL) {
  450. ptls_mbedtls_sign_certificate_t *self =
  451. (ptls_mbedtls_sign_certificate_t *)(((unsigned char *)_self) -
  452. offsetof(struct st_ptls_mbedtls_sign_certificate_t, super));
  453. /* Destroy the key */
  454. psa_destroy_key(self->key_id);
  455. psa_reset_key_attributes(&self->attributes);
  456. memset(self, 0, sizeof(ptls_mbedtls_sign_certificate_t));
  457. free(self);
  458. }
  459. }
  460. /*
  461. * An RSa key is encoded in DER as:
  462. * RSAPrivateKey ::= SEQUENCE {
  463. * version INTEGER, -- must be 0
  464. * modulus INTEGER, -- n
  465. * publicExponent INTEGER, -- e
  466. * privateExponent INTEGER, -- d
  467. * prime1 INTEGER, -- p
  468. * prime2 INTEGER, -- q
  469. * exponent1 INTEGER, -- d mod (p-1)
  470. * exponent2 INTEGER, -- d mod (q-1)
  471. * coefficient INTEGER, -- (inverse of q) mod p
  472. * }
  473. *
  474. * The number of key bits is the size in bits of the integer N.
  475. * We must decode the length in octets of the integer representation,
  476. * then subtract the number of zeros at the beginning of the data.
  477. */
  478. int ptls_mbedtls_rsa_get_key_bits(const unsigned char *key_value, size_t key_length, size_t *p_nb_bits)
  479. {
  480. int ret = 0;
  481. size_t nb_bytes = 0;
  482. size_t nb_bits = 0;
  483. size_t x = 0;
  484. if (key_length > 16 && key_value[x++] == 0x30) {
  485. /* get the length of the sequence. */
  486. size_t l = 0;
  487. ret = ptls_mbedtls_parse_der_length(key_value, key_length, &x, &l);
  488. if (x + l != key_length) {
  489. ret = -1;
  490. }
  491. }
  492. if (ret == 0 && key_value[x] == 0x02 && key_value[x + 1] == 0x01 && key_value[x + 2] == 0x00 && key_value[x + 3] == 0x02) {
  493. x += 4;
  494. ret = ptls_mbedtls_parse_der_length(key_value, key_length, &x, &nb_bytes);
  495. } else {
  496. ret = -1;
  497. }
  498. if (ret == 0) {
  499. unsigned char v = key_value[x];
  500. nb_bits = 8 * nb_bytes;
  501. if (v == 0) {
  502. nb_bits -= 8;
  503. } else {
  504. while ((v & 0x80) == 0) {
  505. nb_bits--;
  506. v <<= 1;
  507. }
  508. }
  509. }
  510. *p_nb_bits = nb_bits;
  511. return ret;
  512. }
  513. void ptls_mbedtls_set_rsa_key_attributes(ptls_mbedtls_sign_certificate_t *signer, const unsigned char *key_value, size_t key_length)
  514. {
  515. size_t nb_bits = 0;
  516. psa_set_key_usage_flags(&signer->attributes, PSA_KEY_USAGE_SIGN_HASH);
  517. psa_set_key_algorithm(&signer->attributes, PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
  518. psa_set_key_type(&signer->attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
  519. if (ptls_mbedtls_rsa_get_key_bits(key_value, key_length, &nb_bits) == 0) {
  520. psa_set_key_bits(&signer->attributes, nb_bits);
  521. }
  522. }
  523. int ptls_mbedtls_set_ec_key_attributes(ptls_mbedtls_sign_certificate_t *signer, size_t key_length)
  524. {
  525. int ret = 0;
  526. psa_set_key_usage_flags(&signer->attributes, PSA_KEY_USAGE_SIGN_HASH);
  527. psa_set_key_algorithm(&signer->attributes, PSA_ALG_ECDSA_BASE);
  528. psa_set_key_type(&signer->attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
  529. if (key_length == 32) {
  530. psa_set_key_algorithm(&signer->attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
  531. psa_set_key_bits(&signer->attributes, 256);
  532. } else if (key_length == 48) {
  533. psa_set_key_algorithm(&signer->attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_384));
  534. psa_set_key_bits(&signer->attributes, 384);
  535. } else if (key_length == 66) {
  536. psa_set_key_algorithm(&signer->attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_512));
  537. psa_set_key_bits(&signer->attributes, 521);
  538. } else {
  539. ret = -1;
  540. }
  541. return ret;
  542. }
  543. int ptls_mbedtls_load_private_key(ptls_context_t *ctx, char const *pem_fname)
  544. {
  545. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  546. size_t n;
  547. unsigned char *buf;
  548. mbedtls_pem_context pem = {0};
  549. mbedtls_pk_type_t pk_type = 0;
  550. /* mbedtls_svc_key_id_t key_id = 0; */
  551. size_t key_length = 0;
  552. size_t key_index = 0;
  553. ptls_mbedtls_sign_certificate_t *signer = (ptls_mbedtls_sign_certificate_t *)malloc(sizeof(ptls_mbedtls_sign_certificate_t));
  554. if (signer == NULL) {
  555. return (PTLS_ERROR_NO_MEMORY);
  556. }
  557. memset(signer, 0, sizeof(ptls_mbedtls_sign_certificate_t));
  558. signer->attributes = psa_key_attributes_init();
  559. if ((ret = mbedtls_pk_load_file(pem_fname, &buf, &n)) != 0) {
  560. if (ret == MBEDTLS_ERR_PK_ALLOC_FAILED) {
  561. return (PTLS_ERROR_NO_MEMORY);
  562. } else {
  563. return (PTLS_ERROR_NOT_AVAILABLE);
  564. }
  565. }
  566. ret = ptls_mbedtls_get_der_key(&pem, &pk_type, buf, n, NULL, 0, NULL, NULL);
  567. /* We cannot use the platform API:
  568. mbedtls_zeroize_and_free(buf, n);
  569. so we do our own thing.
  570. */
  571. memset(buf, 0, n);
  572. free(buf);
  573. if (ret == 0) {
  574. if (pk_type == MBEDTLS_PK_RSA) {
  575. key_length = pem.private_buflen;
  576. ptls_mbedtls_set_rsa_key_attributes(signer, pem.private_buf, key_length);
  577. } else if (pk_type == MBEDTLS_PK_ECKEY) {
  578. ret = ptls_mbedtls_parse_ecdsa_field(pem.private_buf, pem.private_buflen, &key_index, &key_length);
  579. if (ret == 0) {
  580. ret = ptls_mbedtls_set_ec_key_attributes(signer, key_length);
  581. }
  582. } else if (pk_type == MBEDTLS_PK_NONE) {
  583. /* TODO: not clear whether MBDED TLS supports ED25519 yet. Probably not. */
  584. /* Should have option to encode RSA or ECDSA using PKCS8 */
  585. size_t oid_index = 0;
  586. size_t oid_length = 0;
  587. psa_set_key_usage_flags(&signer->attributes, PSA_KEY_USAGE_SIGN_HASH);
  588. ret =
  589. test_parse_private_key_field(pem.private_buf, pem.private_buflen, &oid_index, &oid_length, &key_index, &key_length);
  590. if (ret == 0) {
  591. /* need to parse the OID in order to set the parameters */
  592. if (oid_length == sizeof(ptls_mbedtls_oid_ec_key) &&
  593. memcmp(pem.private_buf + oid_index, ptls_mbedtls_oid_ec_key, sizeof(ptls_mbedtls_oid_ec_key)) == 0) {
  594. ret = ptls_mbedtls_parse_ec_private_key(pem.private_buf, pem.private_buflen, &key_index, &key_length);
  595. if (ret == 0) {
  596. ret = ptls_mbedtls_set_ec_key_attributes(signer, key_length);
  597. }
  598. } else if (oid_length == sizeof(ptls_mbedtls_oid_ed25519) &&
  599. memcmp(pem.private_buf + oid_index, ptls_mbedtls_oid_ed25519, sizeof(ptls_mbedtls_oid_ed25519)) == 0) {
  600. /* We recognized ED25519 -- PSA_ECC_FAMILY_TWISTED_EDWARDS -- PSA_ALG_ED25519PH */
  601. psa_set_key_algorithm(&signer->attributes, PSA_ALG_PURE_EDDSA);
  602. psa_set_key_type(&signer->attributes, PSA_ECC_FAMILY_TWISTED_EDWARDS);
  603. ret = ptls_mbedtls_parse_eddsa_key(pem.private_buf, pem.private_buflen, &key_index, &key_length);
  604. psa_set_key_bits(&signer->attributes, 256);
  605. } else if (oid_length == sizeof(ptls_mbedtls_oid_rsa_key) &&
  606. memcmp(pem.private_buf + oid_index, ptls_mbedtls_oid_rsa_key, sizeof(ptls_mbedtls_oid_rsa_key)) == 0) {
  607. /* We recognized RSA */
  608. key_length = pem.private_buflen;
  609. ptls_mbedtls_set_rsa_key_attributes(signer, pem.private_buf, key_length);
  610. } else {
  611. ret = PTLS_ERROR_NOT_AVAILABLE;
  612. }
  613. }
  614. } else {
  615. ret = -1;
  616. }
  617. if (ret == 0) {
  618. /* Now that we have the DER or bytes for the key, try import into PSA */
  619. psa_status_t status = psa_import_key(&signer->attributes, pem.private_buf + key_index, key_length, &signer->key_id);
  620. if (status != PSA_SUCCESS) {
  621. ret = -1;
  622. } else {
  623. ret = ptls_mbedtls_set_available_schemes(signer);
  624. }
  625. }
  626. /* Free the PEM buffer */
  627. mbedtls_pem_free(&pem);
  628. }
  629. if (ret == 0) {
  630. signer->super.cb = ptls_mbedtls_sign_certificate;
  631. ctx->sign_certificate = &signer->super;
  632. } else {
  633. /* Dispose of what we have allocated. */
  634. ptls_mbedtls_dispose_sign_certificate(&signer->super);
  635. }
  636. return ret;
  637. }