p_ec_asn1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/evp.h>
  56. #include <openssl/asn1t.h>
  57. #include <openssl/bn.h>
  58. #include <openssl/bytestring.h>
  59. #include <openssl/ec.h>
  60. #include <openssl/ec_key.h>
  61. #include <openssl/ecdsa.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/x509.h>
  66. #include "internal.h"
  67. static int eckey_pub_encode(CBB *out, const EVP_PKEY *key) {
  68. const EC_KEY *ec_key = key->pkey.ec;
  69. const EC_GROUP *group = EC_KEY_get0_group(ec_key);
  70. int curve_nid = EC_GROUP_get_curve_name(group);
  71. if (curve_nid == NID_undef) {
  72. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_NID_FOR_CURVE);
  73. return 0;
  74. }
  75. const EC_POINT *public_key = EC_KEY_get0_public_key(ec_key);
  76. /* See RFC 5480, section 2. */
  77. CBB spki, algorithm, key_bitstring;
  78. if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
  79. !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
  80. !OBJ_nid2cbb(&algorithm, NID_X9_62_id_ecPublicKey) ||
  81. !OBJ_nid2cbb(&algorithm, curve_nid) ||
  82. !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
  83. !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
  84. !EC_POINT_point2cbb(&key_bitstring, group, public_key,
  85. POINT_CONVERSION_UNCOMPRESSED, NULL) ||
  86. !CBB_flush(out)) {
  87. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. static int eckey_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  93. /* See RFC 5480, section 2. */
  94. /* The parameters are a named curve. */
  95. CBS named_curve;
  96. if (!CBS_get_asn1(params, &named_curve, CBS_ASN1_OBJECT) ||
  97. CBS_len(params) != 0) {
  98. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  99. return 0;
  100. }
  101. EC_KEY *eckey = EC_KEY_new_by_curve_name(OBJ_cbs2nid(&named_curve));
  102. if (eckey == NULL) {
  103. return 0;
  104. }
  105. EC_POINT *point = EC_POINT_new(EC_KEY_get0_group(eckey));
  106. if (point == NULL ||
  107. !EC_POINT_oct2point(EC_KEY_get0_group(eckey), point, CBS_data(key),
  108. CBS_len(key), NULL) ||
  109. !EC_KEY_set_public_key(eckey, point)) {
  110. goto err;
  111. }
  112. EC_POINT_free(point);
  113. EVP_PKEY_assign_EC_KEY(out, eckey);
  114. return 1;
  115. err:
  116. EC_POINT_free(point);
  117. EC_KEY_free(eckey);
  118. return 0;
  119. }
  120. static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  121. int r;
  122. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  123. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  124. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  125. r = EC_POINT_cmp(group, pa, pb, NULL);
  126. if (r == 0) {
  127. return 1;
  128. } else if (r == 1) {
  129. return 0;
  130. } else {
  131. return -2;
  132. }
  133. }
  134. static int eckey_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  135. /* See RFC 5915. */
  136. EC_GROUP *group = EC_KEY_parse_parameters(params);
  137. if (group == NULL || CBS_len(params) != 0) {
  138. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  139. EC_GROUP_free(group);
  140. return 0;
  141. }
  142. EC_KEY *ec_key = EC_KEY_parse_private_key(key, group);
  143. EC_GROUP_free(group);
  144. if (ec_key == NULL || CBS_len(key) != 0) {
  145. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  146. EC_KEY_free(ec_key);
  147. return 0;
  148. }
  149. EVP_PKEY_assign_EC_KEY(out, ec_key);
  150. return 1;
  151. }
  152. static int eckey_priv_encode(CBB *out, const EVP_PKEY *key) {
  153. const EC_KEY *ec_key = key->pkey.ec;
  154. int curve_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  155. if (curve_nid == NID_undef) {
  156. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_NID_FOR_CURVE);
  157. return 0;
  158. }
  159. /* Omit the redundant copy of the curve name. This contradicts RFC 5915 but
  160. * aligns with PKCS #11. SEC 1 only says they may be omitted if known by other
  161. * means. Both OpenSSL and NSS omit the redundant parameters, so we omit them
  162. * as well. */
  163. unsigned enc_flags = EC_KEY_get_enc_flags(ec_key) | EC_PKEY_NO_PARAMETERS;
  164. /* See RFC 5915. */
  165. CBB pkcs8, algorithm, private_key;
  166. if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
  167. !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
  168. !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
  169. !OBJ_nid2cbb(&algorithm, NID_X9_62_id_ecPublicKey) ||
  170. !OBJ_nid2cbb(&algorithm, curve_nid) ||
  171. !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
  172. !EC_KEY_marshal_private_key(&private_key, ec_key, enc_flags) ||
  173. !CBB_flush(out)) {
  174. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  175. return 0;
  176. }
  177. return 1;
  178. }
  179. static int int_ec_size(const EVP_PKEY *pkey) {
  180. return ECDSA_size(pkey->pkey.ec);
  181. }
  182. static int ec_bits(const EVP_PKEY *pkey) {
  183. const EC_GROUP *group = EC_KEY_get0_group(pkey->pkey.ec);
  184. if (group == NULL) {
  185. ERR_clear_error();
  186. return 0;
  187. }
  188. return BN_num_bits(EC_GROUP_get0_order(group));
  189. }
  190. static int ec_missing_parameters(const EVP_PKEY *pkey) {
  191. return EC_KEY_get0_group(pkey->pkey.ec) == NULL;
  192. }
  193. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  194. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  195. if (group == NULL ||
  196. EC_KEY_set_group(to->pkey.ec, group) == 0) {
  197. return 0;
  198. }
  199. EC_GROUP_free(group);
  200. return 1;
  201. }
  202. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  203. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  204. *group_b = EC_KEY_get0_group(b->pkey.ec);
  205. if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) {
  206. /* mismatch */
  207. return 0;
  208. }
  209. return 1;
  210. }
  211. static void int_ec_free(EVP_PKEY *pkey) { EC_KEY_free(pkey->pkey.ec); }
  212. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
  213. uint8_t *buffer = NULL;
  214. const char *ecstr;
  215. size_t buf_len = 0, i;
  216. int ret = 0, reason = ERR_R_BIO_LIB;
  217. BN_CTX *ctx = NULL;
  218. const EC_GROUP *group;
  219. const EC_POINT *public_key;
  220. const BIGNUM *priv_key;
  221. uint8_t *pub_key_bytes = NULL;
  222. size_t pub_key_bytes_len = 0;
  223. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  224. reason = ERR_R_PASSED_NULL_PARAMETER;
  225. goto err;
  226. }
  227. ctx = BN_CTX_new();
  228. if (ctx == NULL) {
  229. reason = ERR_R_MALLOC_FAILURE;
  230. goto err;
  231. }
  232. if (ktype > 0) {
  233. public_key = EC_KEY_get0_public_key(x);
  234. if (public_key != NULL) {
  235. pub_key_bytes_len = EC_POINT_point2oct(
  236. group, public_key, EC_KEY_get_conv_form(x), NULL, 0, ctx);
  237. if (pub_key_bytes_len == 0) {
  238. reason = ERR_R_MALLOC_FAILURE;
  239. goto err;
  240. }
  241. pub_key_bytes = OPENSSL_malloc(pub_key_bytes_len);
  242. if (pub_key_bytes == NULL) {
  243. reason = ERR_R_MALLOC_FAILURE;
  244. goto err;
  245. }
  246. pub_key_bytes_len =
  247. EC_POINT_point2oct(group, public_key, EC_KEY_get_conv_form(x),
  248. pub_key_bytes, pub_key_bytes_len, ctx);
  249. if (pub_key_bytes_len == 0) {
  250. reason = ERR_R_MALLOC_FAILURE;
  251. goto err;
  252. }
  253. buf_len = pub_key_bytes_len;
  254. }
  255. }
  256. if (ktype == 2) {
  257. priv_key = EC_KEY_get0_private_key(x);
  258. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) {
  259. buf_len = i;
  260. }
  261. } else {
  262. priv_key = NULL;
  263. }
  264. if (ktype > 0) {
  265. buf_len += 10;
  266. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  267. reason = ERR_R_MALLOC_FAILURE;
  268. goto err;
  269. }
  270. }
  271. if (ktype == 2) {
  272. ecstr = "Private-Key";
  273. } else if (ktype == 1) {
  274. ecstr = "Public-Key";
  275. } else {
  276. ecstr = "ECDSA-Parameters";
  277. }
  278. if (!BIO_indent(bp, off, 128)) {
  279. goto err;
  280. }
  281. const BIGNUM *order = EC_GROUP_get0_order(group);
  282. if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0) {
  283. goto err;
  284. }
  285. if ((priv_key != NULL) &&
  286. !ASN1_bn_print(bp, "priv:", priv_key, buffer, off)) {
  287. goto err;
  288. }
  289. if (pub_key_bytes != NULL) {
  290. BIO_hexdump(bp, pub_key_bytes, pub_key_bytes_len, off);
  291. }
  292. /* TODO(fork): implement */
  293. /*
  294. if (!ECPKParameters_print(bp, group, off))
  295. goto err; */
  296. ret = 1;
  297. err:
  298. if (!ret) {
  299. OPENSSL_PUT_ERROR(EVP, reason);
  300. }
  301. OPENSSL_free(pub_key_bytes);
  302. BN_CTX_free(ctx);
  303. OPENSSL_free(buffer);
  304. return ret;
  305. }
  306. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  307. ASN1_PCTX *ctx) {
  308. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  309. }
  310. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  311. ASN1_PCTX *ctx) {
  312. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
  313. }
  314. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  315. ASN1_PCTX *ctx) {
  316. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
  317. }
  318. static int eckey_opaque(const EVP_PKEY *pkey) {
  319. return EC_KEY_is_opaque(pkey->pkey.ec);
  320. }
  321. static int old_ec_priv_decode(EVP_PKEY *pkey, const uint8_t **pder,
  322. int derlen) {
  323. EC_KEY *ec;
  324. if (!(ec = d2i_ECPrivateKey(NULL, pder, derlen))) {
  325. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  326. return 0;
  327. }
  328. EVP_PKEY_assign_EC_KEY(pkey, ec);
  329. return 1;
  330. }
  331. const EVP_PKEY_ASN1_METHOD ec_asn1_meth = {
  332. EVP_PKEY_EC,
  333. 0,
  334. "EC",
  335. eckey_pub_decode,
  336. eckey_pub_encode,
  337. eckey_pub_cmp,
  338. eckey_pub_print,
  339. eckey_priv_decode,
  340. eckey_priv_encode,
  341. eckey_priv_print,
  342. eckey_opaque,
  343. 0 /* pkey_supports_digest */,
  344. int_ec_size,
  345. ec_bits,
  346. ec_missing_parameters,
  347. ec_copy_parameters,
  348. ec_cmp_parameters,
  349. eckey_param_print,
  350. 0,
  351. int_ec_free,
  352. old_ec_priv_decode,
  353. NULL /* digest_verify_init_from_algorithm */,
  354. NULL /* digest_sign_algorithm */,
  355. };