Increase the size of the stack buffer to prevent an overflow.
[openssl.git] / crypto / ec / eck_prn.c
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* ====================================================================
11  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12  * Portions originally developed by SUN MICROSYSTEMS, INC., and
13  * contributed to the OpenSSL project.
14  */
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/evp.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
21
22 #ifndef OPENSSL_NO_STDIO
23 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
24 {
25     BIO *b;
26     int ret;
27
28     if ((b = BIO_new(BIO_s_file())) == NULL) {
29         ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
30         return (0);
31     }
32     BIO_set_fp(b, fp, BIO_NOCLOSE);
33     ret = ECPKParameters_print(b, x, off);
34     BIO_free(b);
35     return (ret);
36 }
37
38 int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
39 {
40     BIO *b;
41     int ret;
42
43     if ((b = BIO_new(BIO_s_file())) == NULL) {
44         ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
45         return (0);
46     }
47     BIO_set_fp(b, fp, BIO_NOCLOSE);
48     ret = EC_KEY_print(b, x, off);
49     BIO_free(b);
50     return (ret);
51 }
52
53 int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
54 {
55     BIO *b;
56     int ret;
57
58     if ((b = BIO_new(BIO_s_file())) == NULL) {
59         ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
60         return (0);
61     }
62     BIO_set_fp(b, fp, BIO_NOCLOSE);
63     ret = ECParameters_print(b, x);
64     BIO_free(b);
65     return (ret);
66 }
67 #endif
68
69 static int print_bin(BIO *fp, const char *str, const unsigned char *num,
70                      size_t len, int off);
71
72 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
73 {
74     int ret = 0, reason = ERR_R_BIO_LIB;
75     BN_CTX *ctx = NULL;
76     const EC_POINT *point = NULL;
77     BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL;
78     const BIGNUM *order = NULL, *cofactor = NULL;
79     const unsigned char *seed;
80     size_t seed_len = 0;
81
82     static const char *gen_compressed = "Generator (compressed):";
83     static const char *gen_uncompressed = "Generator (uncompressed):";
84     static const char *gen_hybrid = "Generator (hybrid):";
85
86     if (!x) {
87         reason = ERR_R_PASSED_NULL_PARAMETER;
88         goto err;
89     }
90
91     ctx = BN_CTX_new();
92     if (ctx == NULL) {
93         reason = ERR_R_MALLOC_FAILURE;
94         goto err;
95     }
96
97     if (EC_GROUP_get_asn1_flag(x)) {
98         /* the curve parameter are given by an asn1 OID */
99         int nid;
100         const char *nname;
101
102         if (!BIO_indent(bp, off, 128))
103             goto err;
104
105         nid = EC_GROUP_get_curve_name(x);
106         if (nid == 0)
107             goto err;
108         if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
109             goto err;
110         if (BIO_printf(bp, "\n") <= 0)
111             goto err;
112         nname = EC_curve_nid2nist(nid);
113         if (nname) {
114             if (!BIO_indent(bp, off, 128))
115                 goto err;
116             if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
117                 goto err;
118         }
119     } else {
120         /* explicit parameters */
121         int is_char_two = 0;
122         point_conversion_form_t form;
123         int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
124
125         if (tmp_nid == NID_X9_62_characteristic_two_field)
126             is_char_two = 1;
127
128         if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
129             (b = BN_new()) == NULL) {
130             reason = ERR_R_MALLOC_FAILURE;
131             goto err;
132         }
133 #ifndef OPENSSL_NO_EC2M
134         if (is_char_two) {
135             if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
136                 reason = ERR_R_EC_LIB;
137                 goto err;
138             }
139         } else                  /* prime field */
140 #endif
141         {
142             if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
143                 reason = ERR_R_EC_LIB;
144                 goto err;
145             }
146         }
147
148         if ((point = EC_GROUP_get0_generator(x)) == NULL) {
149             reason = ERR_R_EC_LIB;
150             goto err;
151         }
152         order = EC_GROUP_get0_order(x);
153         cofactor = EC_GROUP_get0_cofactor(x);
154         if (order == NULL) {
155             reason = ERR_R_EC_LIB;
156             goto err;
157         }
158
159         form = EC_GROUP_get_point_conversion_form(x);
160
161         if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
162             reason = ERR_R_EC_LIB;
163             goto err;
164         }
165
166         if ((seed = EC_GROUP_get0_seed(x)) != NULL)
167             seed_len = EC_GROUP_get_seed_len(x);
168
169         if (!BIO_indent(bp, off, 128))
170             goto err;
171
172         /* print the 'short name' of the field type */
173         if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
174             <= 0)
175             goto err;
176
177         if (is_char_two) {
178             /* print the 'short name' of the base type OID */
179             int basis_type = EC_GROUP_get_basis_type(x);
180             if (basis_type == 0)
181                 goto err;
182
183             if (!BIO_indent(bp, off, 128))
184                 goto err;
185
186             if (BIO_printf(bp, "Basis Type: %s\n",
187                            OBJ_nid2sn(basis_type)) <= 0)
188                 goto err;
189
190             /* print the polynomial */
191             if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
192                                               off))
193                 goto err;
194         } else {
195             if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
196                 goto err;
197         }
198         if ((a != NULL) && !ASN1_bn_print(bp, "A:   ", a, NULL, off))
199             goto err;
200         if ((b != NULL) && !ASN1_bn_print(bp, "B:   ", b, NULL, off))
201             goto err;
202         if (form == POINT_CONVERSION_COMPRESSED) {
203             if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
204                                                 NULL, off))
205                 goto err;
206         } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
207             if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
208                                                 NULL, off))
209                 goto err;
210         } else {                /* form == POINT_CONVERSION_HYBRID */
211
212             if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
213                                                 NULL, off))
214                 goto err;
215         }
216         if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
217                                               NULL, off))
218             goto err;
219         if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
220                                                  NULL, off))
221             goto err;
222         if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
223             goto err;
224     }
225     ret = 1;
226  err:
227     if (!ret)
228         ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
229     BN_free(p);
230     BN_free(a);
231     BN_free(b);
232     BN_free(gen);
233     BN_CTX_free(ctx);
234     return (ret);
235 }
236
237 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
238                      size_t len, int off)
239 {
240     size_t i;
241     char str[128 + 1 + 4];
242
243     if (buf == NULL)
244         return 1;
245     if (off > 0) {
246         if (off > 128)
247             off = 128;
248         memset(str, ' ', off);
249         if (BIO_write(fp, str, off) <= 0)
250             return 0;
251     } else {
252         off = 0;
253     }
254
255     if (BIO_printf(fp, "%s", name) <= 0)
256         return 0;
257
258     for (i = 0; i < len; i++) {
259         if ((i % 15) == 0) {
260             str[0] = '\n';
261             memset(&(str[1]), ' ', off + 4);
262             if (BIO_write(fp, str, off + 1 + 4) <= 0)
263                 return 0;
264         }
265         if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
266             0)
267             return 0;
268     }
269     if (BIO_write(fp, "\n", 1) <= 0)
270         return 0;
271
272     return 1;
273 }