minicrypto-pem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2017,2018 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. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #ifdef _WINDOWS
  27. #include "wincompat.h"
  28. #else
  29. #include <unistd.h>
  30. #endif
  31. #include "picotls.h"
  32. #include "picotls/minicrypto.h"
  33. #include "picotls/asn1.h"
  34. #include "picotls/pembase64.h"
  35. /*
  36. * This function could be declared as static, but we want to access it
  37. * in the unit tests.
  38. */
  39. size_t ptls_minicrypto_asn1_decode_private_key(ptls_asn1_pkcs8_private_key_t *pkey, int *decode_error,
  40. ptls_minicrypto_log_ctx_t *log_ctx)
  41. {
  42. uint8_t *bytes = pkey->vec.base;
  43. size_t bytes_max = pkey->vec.len;
  44. /* read the ASN1 messages */
  45. size_t byte_index = 0;
  46. uint32_t seq0_length = 0;
  47. size_t last_byte0;
  48. uint32_t seq1_length = 0;
  49. size_t last_byte1 = 0;
  50. uint32_t oid_length;
  51. size_t last_oid_byte;
  52. uint32_t key_data_length;
  53. size_t key_data_last;
  54. /* start with sequence */
  55. byte_index = ptls_asn1_get_expected_type_and_length(bytes, bytes_max, byte_index, 0x30, &seq0_length, NULL, &last_byte0,
  56. decode_error, log_ctx);
  57. if (*decode_error == 0 && bytes_max != last_byte0) {
  58. byte_index = ptls_asn1_error_message("Length larger than message", bytes_max, byte_index, 0, log_ctx);
  59. *decode_error = PTLS_ERROR_BER_EXCESSIVE_LENGTH;
  60. }
  61. if (*decode_error == 0) {
  62. /* get first component: version, INTEGER, expect value 0 */
  63. if (byte_index + 3 > bytes_max) {
  64. byte_index = ptls_asn1_error_message("Cannot find key version", bytes_max, byte_index, 0, log_ctx);
  65. *decode_error = PTLS_ERROR_INCORRECT_PEM_KEY_VERSION;
  66. } else if (bytes[byte_index] != 0x02 || bytes[byte_index + 1] != 0x01 || bytes[byte_index + 2] != 0x00) {
  67. *decode_error = PTLS_ERROR_INCORRECT_PEM_KEY_VERSION;
  68. byte_index = ptls_asn1_error_message("Incorrect PEM Version", bytes_max, byte_index, 0, log_ctx);
  69. } else {
  70. byte_index += 3;
  71. if (log_ctx != NULL) {
  72. log_ctx->fn(log_ctx->ctx, " Version = 1,\n");
  73. }
  74. }
  75. }
  76. if (*decode_error == 0) {
  77. /* open embedded sequence */
  78. byte_index = ptls_asn1_get_expected_type_and_length(bytes, bytes_max, byte_index, 0x30, &seq1_length, NULL, &last_byte1,
  79. decode_error, log_ctx);
  80. }
  81. if (*decode_error == 0) {
  82. if (log_ctx != NULL) {
  83. log_ctx->fn(log_ctx->ctx, " Algorithm Identifier:\n");
  84. }
  85. /* get length of OID */
  86. byte_index = ptls_asn1_get_expected_type_and_length(bytes, last_byte1, byte_index, 0x06, &oid_length, NULL, &last_oid_byte,
  87. decode_error, log_ctx);
  88. if (*decode_error == 0) {
  89. if (log_ctx != NULL) {
  90. /* print the OID value */
  91. log_ctx->fn(log_ctx->ctx, " Algorithm:");
  92. ptls_asn1_dump_content(bytes + byte_index, oid_length, 0, log_ctx);
  93. log_ctx->fn(log_ctx->ctx, ",\n");
  94. }
  95. pkey->algorithm_index = byte_index;
  96. pkey->algorithm_length = oid_length;
  97. byte_index += oid_length;
  98. }
  99. }
  100. if (*decode_error == 0) {
  101. /* get parameters, ANY */
  102. if (log_ctx != NULL) {
  103. log_ctx->fn(log_ctx->ctx, " Parameters:\n");
  104. }
  105. if (last_byte1 <= byte_index) {
  106. pkey->parameters_index = 0;
  107. pkey->parameters_length = 0;
  108. } else {
  109. pkey->parameters_index = byte_index;
  110. pkey->parameters_length =
  111. (uint32_t)ptls_asn1_validation_recursive(bytes + byte_index, last_byte1 - byte_index, decode_error, 2, log_ctx);
  112. if (*decode_error == 0) {
  113. byte_index += pkey->parameters_length;
  114. }
  115. }
  116. if (log_ctx != NULL) {
  117. log_ctx->fn(log_ctx->ctx, "\n");
  118. }
  119. /* close sequence */
  120. if (*decode_error == 0 && byte_index != last_byte1) {
  121. byte_index = ptls_asn1_error_message("Length larger than element", bytes_max, byte_index, 2, log_ctx);
  122. *decode_error = PTLS_ERROR_BER_ELEMENT_TOO_SHORT;
  123. }
  124. }
  125. /* get octet string, key */
  126. if (*decode_error == 0) {
  127. byte_index = ptls_asn1_get_expected_type_and_length(bytes, last_byte0, byte_index, 0x04, &key_data_length, NULL,
  128. &key_data_last, decode_error, log_ctx);
  129. if (*decode_error == 0) {
  130. pkey->key_data_index = byte_index;
  131. pkey->key_data_length = key_data_length;
  132. byte_index += key_data_length;
  133. if (log_ctx != NULL) {
  134. log_ctx->fn(log_ctx->ctx, " Key data (%d bytes):\n", key_data_length);
  135. (void)ptls_asn1_validation_recursive(bytes + pkey->key_data_index, key_data_length, decode_error, 1, log_ctx);
  136. log_ctx->fn(log_ctx->ctx, "\n");
  137. }
  138. }
  139. }
  140. if (*decode_error == 0 && byte_index != last_byte0) {
  141. byte_index = ptls_asn1_error_message("Length larger than element", bytes_max, byte_index, 0, log_ctx);
  142. *decode_error = PTLS_ERROR_BER_ELEMENT_TOO_SHORT;
  143. }
  144. if (log_ctx != NULL) {
  145. log_ctx->fn(log_ctx->ctx, "\n");
  146. }
  147. return byte_index;
  148. }
  149. static int ptls_pem_parse_private_key(char const *pem_fname, ptls_asn1_pkcs8_private_key_t *pkey,
  150. ptls_minicrypto_log_ctx_t *log_ctx)
  151. {
  152. size_t nb_keys = 0;
  153. int ret = ptls_load_pem_objects(pem_fname, "PRIVATE KEY", &pkey->vec, 1, &nb_keys);
  154. if (ret == 0) {
  155. if (nb_keys != 1) {
  156. ret = PTLS_ERROR_PEM_LABEL_NOT_FOUND;
  157. }
  158. }
  159. if (ret == 0 && nb_keys == 1) {
  160. int decode_error = 0;
  161. if (log_ctx != NULL) {
  162. log_ctx->fn(log_ctx->ctx, "\nFound PRIVATE KEY, length = %d bytes\n", (int)pkey->vec.len);
  163. }
  164. (void)ptls_minicrypto_asn1_decode_private_key(pkey, &decode_error, log_ctx);
  165. if (decode_error != 0) {
  166. ret = decode_error;
  167. }
  168. }
  169. return ret;
  170. }
  171. static const uint8_t ptls_asn1_algorithm_ecdsa[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01};
  172. static const uint8_t ptls_asn1_curve_secp256r1[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07};
  173. static int ptls_set_ecdsa_private_key(ptls_context_t *ctx, ptls_asn1_pkcs8_private_key_t *pkey, ptls_minicrypto_log_ctx_t *log_ctx)
  174. {
  175. uint8_t *bytes = pkey->vec.base + pkey->parameters_index;
  176. size_t bytes_max = pkey->parameters_length;
  177. size_t byte_index = 0;
  178. uint8_t *curve_id = NULL;
  179. uint32_t curve_id_length = 0;
  180. int decode_error = 0;
  181. uint32_t seq_length;
  182. size_t last_byte = 0;
  183. uint8_t *ecdsa_key_data = NULL;
  184. uint32_t ecdsa_key_data_length = 0;
  185. size_t ecdsa_key_data_last = 0;
  186. /* We expect the parameters to include just the curve ID */
  187. byte_index = ptls_asn1_get_expected_type_and_length(bytes, bytes_max, byte_index, 0x06, &curve_id_length, NULL, &last_byte,
  188. &decode_error, log_ctx);
  189. if (decode_error == 0 && bytes_max != last_byte) {
  190. byte_index = ptls_asn1_error_message("Length larger than parameters", bytes_max, byte_index, 0, log_ctx);
  191. decode_error = PTLS_ERROR_BER_EXCESSIVE_LENGTH;
  192. }
  193. if (decode_error == 0) {
  194. curve_id = bytes + byte_index;
  195. if (log_ctx != NULL) {
  196. /* print the OID value */
  197. log_ctx->fn(log_ctx->ctx, "Curve: ");
  198. ptls_asn1_dump_content(curve_id, curve_id_length, 0, log_ctx);
  199. log_ctx->fn(log_ctx->ctx, "\n");
  200. }
  201. }
  202. /* We expect the key data to follow the ECDSA structure per RFC 5915 */
  203. bytes = pkey->vec.base + pkey->key_data_index;
  204. bytes_max = pkey->key_data_length;
  205. byte_index = 0;
  206. /* decode the wrapping sequence */
  207. if (decode_error == 0) {
  208. byte_index = ptls_asn1_get_expected_type_and_length(bytes, bytes_max, byte_index, 0x30, &seq_length, NULL, &last_byte,
  209. &decode_error, log_ctx);
  210. }
  211. if (decode_error == 0 && bytes_max != last_byte) {
  212. byte_index = ptls_asn1_error_message("Length larger than key data", bytes_max, byte_index, 0, log_ctx);
  213. decode_error = PTLS_ERROR_BER_ELEMENT_TOO_SHORT;
  214. }
  215. /* verify and skip the version number 1 */
  216. if (decode_error == 0) {
  217. /* get first component: version, INTEGER, expect value 0 */
  218. if (byte_index + 3 > bytes_max) {
  219. byte_index = ptls_asn1_error_message("Cannot find ECDSA Key Data Version", bytes_max, byte_index, 0, log_ctx);
  220. decode_error = PTLS_ERROR_INCORRECT_ASN1_ECDSA_KEY_SYNTAX;
  221. } else if (bytes[byte_index] != 0x02 || bytes[byte_index + 1] != 0x01 || bytes[byte_index + 2] != 0x01) {
  222. decode_error = PTLS_ERROR_INCORRECT_PEM_ECDSA_KEY_VERSION;
  223. byte_index = ptls_asn1_error_message("Incorrect ECDSA Key Data Version", bytes_max, byte_index, 0, log_ctx);
  224. } else {
  225. byte_index += 3;
  226. if (log_ctx != NULL) {
  227. log_ctx->fn(log_ctx->ctx, "ECDSA Version = 1,\n");
  228. }
  229. }
  230. }
  231. /* obtain the octet string that contains the ECDSA private key */
  232. if (decode_error == 0) {
  233. byte_index = ptls_asn1_get_expected_type_and_length(bytes, last_byte, byte_index, 0x04, &ecdsa_key_data_length, NULL,
  234. &ecdsa_key_data_last, &decode_error, log_ctx);
  235. if (decode_error == 0) {
  236. ecdsa_key_data = bytes + byte_index;
  237. }
  238. }
  239. /* If everything is fine, associate the ECDSA key with the context */
  240. if (curve_id_length == sizeof(ptls_asn1_curve_secp256r1) && curve_id != NULL &&
  241. memcmp(curve_id, ptls_asn1_curve_secp256r1, sizeof(ptls_asn1_curve_secp256r1)) == 0) {
  242. if (SECP256R1_PRIVATE_KEY_SIZE != ecdsa_key_data_length) {
  243. decode_error = PTLS_ERROR_INCORRECT_PEM_ECDSA_KEYSIZE;
  244. if (log_ctx != NULL) {
  245. /* print the OID value */
  246. log_ctx->fn(log_ctx->ctx, "Wrong SECP256R1 key length, %d instead of %d.\n", ecdsa_key_data_length,
  247. SECP256R1_PRIVATE_KEY_SIZE);
  248. }
  249. } else {
  250. ptls_minicrypto_secp256r1sha256_sign_certificate_t *minicrypto_sign_certificate;
  251. minicrypto_sign_certificate = (ptls_minicrypto_secp256r1sha256_sign_certificate_t *)malloc(
  252. sizeof(ptls_minicrypto_secp256r1sha256_sign_certificate_t));
  253. if (minicrypto_sign_certificate == NULL) {
  254. decode_error = PTLS_ERROR_NO_MEMORY;
  255. } else {
  256. memset(minicrypto_sign_certificate, 0, sizeof(ptls_minicrypto_secp256r1sha256_sign_certificate_t));
  257. decode_error = ptls_minicrypto_init_secp256r1sha256_sign_certificate(
  258. minicrypto_sign_certificate, ptls_iovec_init(ecdsa_key_data, ecdsa_key_data_length));
  259. }
  260. if (decode_error == 0) {
  261. ctx->sign_certificate = &minicrypto_sign_certificate->super;
  262. if (log_ctx != NULL) {
  263. /* print the OID value */
  264. log_ctx->fn(log_ctx->ctx, "Initialized SECP512R1 signing key with %d bytes.\n", ecdsa_key_data_length);
  265. }
  266. } else if (log_ctx != NULL) {
  267. log_ctx->fn(log_ctx->ctx, "SECP512R1 init with %d bytes returns %d.\n", ecdsa_key_data_length, decode_error);
  268. }
  269. }
  270. } else {
  271. decode_error = PTLS_ERROR_INCORRECT_PEM_ECDSA_CURVE;
  272. if (log_ctx != NULL) {
  273. /* print the OID value */
  274. log_ctx->fn(log_ctx->ctx, "Curve is not supported for signatures.\n");
  275. }
  276. }
  277. return decode_error;
  278. }
  279. int ptls_minicrypto_load_private_key(ptls_context_t *ctx, char const *pem_fname)
  280. {
  281. ptls_asn1_pkcs8_private_key_t pkey = {{0}};
  282. int ret = ptls_pem_parse_private_key(pem_fname, &pkey, NULL);
  283. if (ret != 0)
  284. goto err;
  285. /* Check that this is the expected key type.
  286. * At this point, the minicrypto library only supports ECDSA keys.
  287. * In theory, we could add support for RSA keys at some point.
  288. */
  289. if (pkey.algorithm_length != sizeof(ptls_asn1_algorithm_ecdsa) ||
  290. memcmp(pkey.vec.base + pkey.algorithm_index, ptls_asn1_algorithm_ecdsa, sizeof(ptls_asn1_algorithm_ecdsa)) != 0) {
  291. ret = -1;
  292. goto err;
  293. }
  294. ret = ptls_set_ecdsa_private_key(ctx, &pkey, NULL);
  295. err:
  296. if (pkey.vec.base) {
  297. ptls_clear_memory(pkey.vec.base, pkey.vec.len);
  298. free(pkey.vec.base);
  299. }
  300. return ret;
  301. }