2
0

eck_prn.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "internal/deprecated.h"
  11. #include <stdio.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/evp.h>
  14. #include <openssl/ec.h>
  15. #include <openssl/bn.h>
  16. #ifndef OPENSSL_NO_DEPRECATED_3_0
  17. # ifndef OPENSSL_NO_STDIO
  18. int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
  19. {
  20. BIO *b;
  21. int ret;
  22. if ((b = BIO_new(BIO_s_file())) == NULL) {
  23. ERR_raise(ERR_LIB_EC, ERR_R_BUF_LIB);
  24. return 0;
  25. }
  26. BIO_set_fp(b, fp, BIO_NOCLOSE);
  27. ret = ECPKParameters_print(b, x, off);
  28. BIO_free(b);
  29. return ret;
  30. }
  31. int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
  32. {
  33. BIO *b;
  34. int ret;
  35. if ((b = BIO_new(BIO_s_file())) == NULL) {
  36. ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
  37. return 0;
  38. }
  39. BIO_set_fp(b, fp, BIO_NOCLOSE);
  40. ret = EC_KEY_print(b, x, off);
  41. BIO_free(b);
  42. return ret;
  43. }
  44. int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
  45. {
  46. BIO *b;
  47. int ret;
  48. if ((b = BIO_new(BIO_s_file())) == NULL) {
  49. ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
  50. return 0;
  51. }
  52. BIO_set_fp(b, fp, BIO_NOCLOSE);
  53. ret = ECParameters_print(b, x);
  54. BIO_free(b);
  55. return ret;
  56. }
  57. #endif /* OPENSSL_NO_STDIO */
  58. static int print_bin(BIO *fp, const char *str, const unsigned char *num,
  59. size_t len, int off);
  60. int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
  61. {
  62. int ret = 0, reason = ERR_R_BIO_LIB;
  63. BN_CTX *ctx = NULL;
  64. const EC_POINT *point = NULL;
  65. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  66. unsigned char *gen_buf = NULL;
  67. const BIGNUM *order = NULL, *cofactor = NULL;
  68. const unsigned char *seed;
  69. size_t seed_len = 0, gen_buf_len = 0;
  70. static const char *gen_compressed = "Generator (compressed):";
  71. static const char *gen_uncompressed = "Generator (uncompressed):";
  72. static const char *gen_hybrid = "Generator (hybrid):";
  73. if (!x) {
  74. reason = ERR_R_PASSED_NULL_PARAMETER;
  75. goto err;
  76. }
  77. ctx = BN_CTX_new();
  78. if (ctx == NULL) {
  79. reason = ERR_R_BN_LIB;
  80. goto err;
  81. }
  82. if (EC_GROUP_get_asn1_flag(x)) {
  83. /* the curve parameter are given by an asn1 OID */
  84. int nid;
  85. const char *nname;
  86. if (!BIO_indent(bp, off, 128))
  87. goto err;
  88. nid = EC_GROUP_get_curve_name(x);
  89. if (nid == 0)
  90. goto err;
  91. if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
  92. goto err;
  93. if (BIO_printf(bp, "\n") <= 0)
  94. goto err;
  95. nname = EC_curve_nid2nist(nid);
  96. if (nname) {
  97. if (!BIO_indent(bp, off, 128))
  98. goto err;
  99. if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
  100. goto err;
  101. }
  102. } else {
  103. const char *form_str;
  104. /* explicit parameters */
  105. int is_char_two = 0;
  106. point_conversion_form_t form;
  107. int tmp_nid = EC_GROUP_get_field_type(x);
  108. if (tmp_nid == NID_X9_62_characteristic_two_field)
  109. is_char_two = 1;
  110. if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
  111. (b = BN_new()) == NULL) {
  112. reason = ERR_R_BN_LIB;
  113. goto err;
  114. }
  115. if (!EC_GROUP_get_curve(x, p, a, b, ctx)) {
  116. reason = ERR_R_EC_LIB;
  117. goto err;
  118. }
  119. if ((point = EC_GROUP_get0_generator(x)) == NULL) {
  120. reason = ERR_R_EC_LIB;
  121. goto err;
  122. }
  123. order = EC_GROUP_get0_order(x);
  124. cofactor = EC_GROUP_get0_cofactor(x);
  125. if (order == NULL) {
  126. reason = ERR_R_EC_LIB;
  127. goto err;
  128. }
  129. form = EC_GROUP_get_point_conversion_form(x);
  130. gen_buf_len = EC_POINT_point2buf(x, point, form, &gen_buf, ctx);
  131. if (gen_buf_len == 0) {
  132. reason = ERR_R_EC_LIB;
  133. goto err;
  134. }
  135. if ((seed = EC_GROUP_get0_seed(x)) != NULL)
  136. seed_len = EC_GROUP_get_seed_len(x);
  137. if (!BIO_indent(bp, off, 128))
  138. goto err;
  139. /* print the 'short name' of the field type */
  140. if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
  141. <= 0)
  142. goto err;
  143. if (is_char_two) {
  144. /* print the 'short name' of the base type OID */
  145. int basis_type = EC_GROUP_get_basis_type(x);
  146. if (basis_type == 0)
  147. goto err;
  148. if (!BIO_indent(bp, off, 128))
  149. goto err;
  150. if (BIO_printf(bp, "Basis Type: %s\n",
  151. OBJ_nid2sn(basis_type)) <= 0)
  152. goto err;
  153. /* print the polynomial */
  154. if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
  155. off))
  156. goto err;
  157. } else {
  158. if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
  159. goto err;
  160. }
  161. if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
  162. goto err;
  163. if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
  164. goto err;
  165. if (form == POINT_CONVERSION_COMPRESSED)
  166. form_str = gen_compressed;
  167. else if (form == POINT_CONVERSION_UNCOMPRESSED)
  168. form_str = gen_uncompressed;
  169. else
  170. form_str = gen_hybrid;
  171. if (gen_buf != NULL
  172. && !print_bin(bp, form_str, gen_buf, gen_buf_len, off))
  173. goto err;
  174. if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, NULL, off))
  175. goto err;
  176. if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
  177. NULL, off))
  178. goto err;
  179. if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
  180. goto err;
  181. }
  182. ret = 1;
  183. err:
  184. if (!ret)
  185. ERR_raise(ERR_LIB_EC, reason);
  186. BN_free(p);
  187. BN_free(a);
  188. BN_free(b);
  189. OPENSSL_clear_free(gen_buf, gen_buf_len);
  190. BN_CTX_free(ctx);
  191. return ret;
  192. }
  193. static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
  194. size_t len, int off)
  195. {
  196. size_t i;
  197. char str[128 + 1 + 4];
  198. if (buf == NULL)
  199. return 1;
  200. if (off > 0) {
  201. if (off > 128)
  202. off = 128;
  203. memset(str, ' ', off);
  204. if (BIO_write(fp, str, off) <= 0)
  205. return 0;
  206. } else {
  207. off = 0;
  208. }
  209. if (BIO_printf(fp, "%s", name) <= 0)
  210. return 0;
  211. for (i = 0; i < len; i++) {
  212. if ((i % 15) == 0) {
  213. str[0] = '\n';
  214. memset(&(str[1]), ' ', off + 4);
  215. if (BIO_write(fp, str, off + 1 + 4) <= 0)
  216. return 0;
  217. }
  218. if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
  219. 0)
  220. return 0;
  221. }
  222. if (BIO_write(fp, "\n", 1) <= 0)
  223. return 0;
  224. return 1;
  225. }
  226. #endif /* OPENSSL_NO_DEPRECATED_3_0 */