25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

251 satır
8.0 KiB

  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/bn.h>
  57. #include <openssl/bytestring.h>
  58. #include <openssl/ec.h>
  59. #include <openssl/ec_key.h>
  60. #include <openssl/ecdsa.h>
  61. #include <openssl/err.h>
  62. #include "internal.h"
  63. static int eckey_pub_encode(CBB *out, const EVP_PKEY *key) {
  64. const EC_KEY *ec_key = key->pkey.ec;
  65. const EC_GROUP *group = EC_KEY_get0_group(ec_key);
  66. const EC_POINT *public_key = EC_KEY_get0_public_key(ec_key);
  67. // See RFC 5480, section 2.
  68. CBB spki, algorithm, oid, key_bitstring;
  69. if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
  70. !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
  71. !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  72. !CBB_add_bytes(&oid, ec_asn1_meth.oid, ec_asn1_meth.oid_len) ||
  73. !EC_KEY_marshal_curve_name(&algorithm, group) ||
  74. !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
  75. !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
  76. !EC_POINT_point2cbb(&key_bitstring, group, public_key,
  77. POINT_CONVERSION_UNCOMPRESSED, NULL) ||
  78. !CBB_flush(out)) {
  79. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. static int eckey_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  85. // See RFC 5480, section 2.
  86. // The parameters are a named curve.
  87. EC_POINT *point = NULL;
  88. EC_KEY *eckey = NULL;
  89. EC_GROUP *group = EC_KEY_parse_curve_name(params);
  90. if (group == NULL || CBS_len(params) != 0) {
  91. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  92. goto err;
  93. }
  94. eckey = EC_KEY_new();
  95. if (eckey == NULL || !EC_KEY_set_group(eckey, group)) {
  96. goto err;
  97. }
  98. point = EC_POINT_new(group);
  99. if (point == NULL ||
  100. !EC_POINT_oct2point(group, point, CBS_data(key), CBS_len(key), NULL) ||
  101. !EC_KEY_set_public_key(eckey, point)) {
  102. goto err;
  103. }
  104. EC_GROUP_free(group);
  105. EC_POINT_free(point);
  106. EVP_PKEY_assign_EC_KEY(out, eckey);
  107. return 1;
  108. err:
  109. EC_GROUP_free(group);
  110. EC_POINT_free(point);
  111. EC_KEY_free(eckey);
  112. return 0;
  113. }
  114. static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  115. int r;
  116. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  117. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  118. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  119. r = EC_POINT_cmp(group, pa, pb, NULL);
  120. if (r == 0) {
  121. return 1;
  122. } else if (r == 1) {
  123. return 0;
  124. } else {
  125. return -2;
  126. }
  127. }
  128. static int eckey_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  129. // See RFC 5915.
  130. EC_GROUP *group = EC_KEY_parse_parameters(params);
  131. if (group == NULL || CBS_len(params) != 0) {
  132. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  133. EC_GROUP_free(group);
  134. return 0;
  135. }
  136. EC_KEY *ec_key = EC_KEY_parse_private_key(key, group);
  137. EC_GROUP_free(group);
  138. if (ec_key == NULL || CBS_len(key) != 0) {
  139. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  140. EC_KEY_free(ec_key);
  141. return 0;
  142. }
  143. EVP_PKEY_assign_EC_KEY(out, ec_key);
  144. return 1;
  145. }
  146. static int eckey_priv_encode(CBB *out, const EVP_PKEY *key) {
  147. const EC_KEY *ec_key = key->pkey.ec;
  148. // Omit the redundant copy of the curve name. This contradicts RFC 5915 but
  149. // aligns with PKCS #11. SEC 1 only says they may be omitted if known by other
  150. // means. Both OpenSSL and NSS omit the redundant parameters, so we omit them
  151. // as well.
  152. unsigned enc_flags = EC_KEY_get_enc_flags(ec_key) | EC_PKEY_NO_PARAMETERS;
  153. // See RFC 5915.
  154. CBB pkcs8, algorithm, oid, private_key;
  155. if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
  156. !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
  157. !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
  158. !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  159. !CBB_add_bytes(&oid, ec_asn1_meth.oid, ec_asn1_meth.oid_len) ||
  160. !EC_KEY_marshal_curve_name(&algorithm, EC_KEY_get0_group(ec_key)) ||
  161. !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
  162. !EC_KEY_marshal_private_key(&private_key, ec_key, enc_flags) ||
  163. !CBB_flush(out)) {
  164. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  165. return 0;
  166. }
  167. return 1;
  168. }
  169. static int int_ec_size(const EVP_PKEY *pkey) {
  170. return ECDSA_size(pkey->pkey.ec);
  171. }
  172. static int ec_bits(const EVP_PKEY *pkey) {
  173. const EC_GROUP *group = EC_KEY_get0_group(pkey->pkey.ec);
  174. if (group == NULL) {
  175. ERR_clear_error();
  176. return 0;
  177. }
  178. return BN_num_bits(EC_GROUP_get0_order(group));
  179. }
  180. static int ec_missing_parameters(const EVP_PKEY *pkey) {
  181. return EC_KEY_get0_group(pkey->pkey.ec) == NULL;
  182. }
  183. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  184. return EC_KEY_set_group(to->pkey.ec, EC_KEY_get0_group(from->pkey.ec));
  185. }
  186. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  187. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  188. *group_b = EC_KEY_get0_group(b->pkey.ec);
  189. if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) {
  190. // mismatch
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. static void int_ec_free(EVP_PKEY *pkey) { EC_KEY_free(pkey->pkey.ec); }
  196. static int eckey_opaque(const EVP_PKEY *pkey) {
  197. return EC_KEY_is_opaque(pkey->pkey.ec);
  198. }
  199. const EVP_PKEY_ASN1_METHOD ec_asn1_meth = {
  200. EVP_PKEY_EC,
  201. // 1.2.840.10045.2.1
  202. {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01}, 7,
  203. eckey_pub_decode,
  204. eckey_pub_encode,
  205. eckey_pub_cmp,
  206. eckey_priv_decode,
  207. eckey_priv_encode,
  208. eckey_opaque,
  209. int_ec_size,
  210. ec_bits,
  211. ec_missing_parameters,
  212. ec_copy_parameters,
  213. ec_cmp_parameters,
  214. int_ec_free,
  215. };