1ebfedea0SLionel Sambuc /* crypto/ec/eck_prn.c */
2ebfedea0SLionel Sambuc /*
3ebfedea0SLionel Sambuc * Written by Nils Larsch for the OpenSSL project.
4ebfedea0SLionel Sambuc */
5ebfedea0SLionel Sambuc /* ====================================================================
6ebfedea0SLionel Sambuc * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
17ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
18ebfedea0SLionel Sambuc * distribution.
19ebfedea0SLionel Sambuc *
20ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
21ebfedea0SLionel Sambuc * software must display the following acknowledgment:
22ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
23ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24ebfedea0SLionel Sambuc *
25ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
27ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
28ebfedea0SLionel Sambuc * openssl-core@openssl.org.
29ebfedea0SLionel Sambuc *
30ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
31ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
32ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
33ebfedea0SLionel Sambuc *
34ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
35ebfedea0SLionel Sambuc * acknowledgment:
36ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
37ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38ebfedea0SLionel Sambuc *
39ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
51ebfedea0SLionel Sambuc * ====================================================================
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
54ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
55ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
56ebfedea0SLionel Sambuc *
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60ebfedea0SLionel Sambuc * Portions originally developed by SUN MICROSYSTEMS, INC., and
61ebfedea0SLionel Sambuc * contributed to the OpenSSL project.
62ebfedea0SLionel Sambuc */
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc #include <stdio.h>
65ebfedea0SLionel Sambuc #include "cryptlib.h"
66ebfedea0SLionel Sambuc #include <openssl/evp.h>
67ebfedea0SLionel Sambuc #include <openssl/ec.h>
68ebfedea0SLionel Sambuc #include <openssl/bn.h>
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_FP_API
ECPKParameters_print_fp(FILE * fp,const EC_GROUP * x,int off)71ebfedea0SLionel Sambuc int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
72ebfedea0SLionel Sambuc {
73ebfedea0SLionel Sambuc BIO *b;
74ebfedea0SLionel Sambuc int ret;
75ebfedea0SLionel Sambuc
76*0a6a1f1dSLionel Sambuc if ((b = BIO_new(BIO_s_file())) == NULL) {
77ebfedea0SLionel Sambuc ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
78ebfedea0SLionel Sambuc return (0);
79ebfedea0SLionel Sambuc }
80ebfedea0SLionel Sambuc BIO_set_fp(b, fp, BIO_NOCLOSE);
81ebfedea0SLionel Sambuc ret = ECPKParameters_print(b, x, off);
82ebfedea0SLionel Sambuc BIO_free(b);
83ebfedea0SLionel Sambuc return (ret);
84ebfedea0SLionel Sambuc }
85ebfedea0SLionel Sambuc
EC_KEY_print_fp(FILE * fp,const EC_KEY * x,int off)86ebfedea0SLionel Sambuc int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
87ebfedea0SLionel Sambuc {
88ebfedea0SLionel Sambuc BIO *b;
89ebfedea0SLionel Sambuc int ret;
90ebfedea0SLionel Sambuc
91*0a6a1f1dSLionel Sambuc if ((b = BIO_new(BIO_s_file())) == NULL) {
92ebfedea0SLionel Sambuc ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
93ebfedea0SLionel Sambuc return (0);
94ebfedea0SLionel Sambuc }
95ebfedea0SLionel Sambuc BIO_set_fp(b, fp, BIO_NOCLOSE);
96ebfedea0SLionel Sambuc ret = EC_KEY_print(b, x, off);
97ebfedea0SLionel Sambuc BIO_free(b);
98ebfedea0SLionel Sambuc return (ret);
99ebfedea0SLionel Sambuc }
100ebfedea0SLionel Sambuc
ECParameters_print_fp(FILE * fp,const EC_KEY * x)101ebfedea0SLionel Sambuc int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
102ebfedea0SLionel Sambuc {
103ebfedea0SLionel Sambuc BIO *b;
104ebfedea0SLionel Sambuc int ret;
105ebfedea0SLionel Sambuc
106*0a6a1f1dSLionel Sambuc if ((b = BIO_new(BIO_s_file())) == NULL) {
107ebfedea0SLionel Sambuc ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
108ebfedea0SLionel Sambuc return (0);
109ebfedea0SLionel Sambuc }
110ebfedea0SLionel Sambuc BIO_set_fp(b, fp, BIO_NOCLOSE);
111ebfedea0SLionel Sambuc ret = ECParameters_print(b, x);
112ebfedea0SLionel Sambuc BIO_free(b);
113ebfedea0SLionel Sambuc return (ret);
114ebfedea0SLionel Sambuc }
115ebfedea0SLionel Sambuc #endif
116ebfedea0SLionel Sambuc
EC_KEY_print(BIO * bp,const EC_KEY * x,int off)117ebfedea0SLionel Sambuc int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
118ebfedea0SLionel Sambuc {
119ebfedea0SLionel Sambuc EVP_PKEY *pk;
120ebfedea0SLionel Sambuc int ret;
121ebfedea0SLionel Sambuc pk = EVP_PKEY_new();
122ebfedea0SLionel Sambuc if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
123ebfedea0SLionel Sambuc return 0;
124ebfedea0SLionel Sambuc ret = EVP_PKEY_print_private(bp, pk, off, NULL);
125ebfedea0SLionel Sambuc EVP_PKEY_free(pk);
126ebfedea0SLionel Sambuc return ret;
127ebfedea0SLionel Sambuc }
128ebfedea0SLionel Sambuc
ECParameters_print(BIO * bp,const EC_KEY * x)129ebfedea0SLionel Sambuc int ECParameters_print(BIO *bp, const EC_KEY *x)
130ebfedea0SLionel Sambuc {
131ebfedea0SLionel Sambuc EVP_PKEY *pk;
132ebfedea0SLionel Sambuc int ret;
133ebfedea0SLionel Sambuc pk = EVP_PKEY_new();
134ebfedea0SLionel Sambuc if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
135ebfedea0SLionel Sambuc return 0;
136ebfedea0SLionel Sambuc ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
137ebfedea0SLionel Sambuc EVP_PKEY_free(pk);
138ebfedea0SLionel Sambuc return ret;
139ebfedea0SLionel Sambuc }
140ebfedea0SLionel Sambuc
141ebfedea0SLionel Sambuc static int print_bin(BIO *fp, const char *str, const unsigned char *num,
142ebfedea0SLionel Sambuc size_t len, int off);
143ebfedea0SLionel Sambuc
ECPKParameters_print(BIO * bp,const EC_GROUP * x,int off)144ebfedea0SLionel Sambuc int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
145ebfedea0SLionel Sambuc {
146ebfedea0SLionel Sambuc unsigned char *buffer = NULL;
147ebfedea0SLionel Sambuc size_t buf_len = 0, i;
148ebfedea0SLionel Sambuc int ret = 0, reason = ERR_R_BIO_LIB;
149ebfedea0SLionel Sambuc BN_CTX *ctx = NULL;
150ebfedea0SLionel Sambuc const EC_POINT *point = NULL;
151ebfedea0SLionel Sambuc BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL,
152ebfedea0SLionel Sambuc *order = NULL, *cofactor = NULL;
153ebfedea0SLionel Sambuc const unsigned char *seed;
154ebfedea0SLionel Sambuc size_t seed_len = 0;
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc static const char *gen_compressed = "Generator (compressed):";
157ebfedea0SLionel Sambuc static const char *gen_uncompressed = "Generator (uncompressed):";
158ebfedea0SLionel Sambuc static const char *gen_hybrid = "Generator (hybrid):";
159ebfedea0SLionel Sambuc
160*0a6a1f1dSLionel Sambuc if (!x) {
161ebfedea0SLionel Sambuc reason = ERR_R_PASSED_NULL_PARAMETER;
162ebfedea0SLionel Sambuc goto err;
163ebfedea0SLionel Sambuc }
164ebfedea0SLionel Sambuc
165ebfedea0SLionel Sambuc ctx = BN_CTX_new();
166*0a6a1f1dSLionel Sambuc if (ctx == NULL) {
167ebfedea0SLionel Sambuc reason = ERR_R_MALLOC_FAILURE;
168ebfedea0SLionel Sambuc goto err;
169ebfedea0SLionel Sambuc }
170ebfedea0SLionel Sambuc
171*0a6a1f1dSLionel Sambuc if (EC_GROUP_get_asn1_flag(x)) {
172ebfedea0SLionel Sambuc /* the curve parameter are given by an asn1 OID */
173ebfedea0SLionel Sambuc int nid;
174ebfedea0SLionel Sambuc
175ebfedea0SLionel Sambuc if (!BIO_indent(bp, off, 128))
176ebfedea0SLionel Sambuc goto err;
177ebfedea0SLionel Sambuc
178ebfedea0SLionel Sambuc nid = EC_GROUP_get_curve_name(x);
179ebfedea0SLionel Sambuc if (nid == 0)
180ebfedea0SLionel Sambuc goto err;
181ebfedea0SLionel Sambuc
182ebfedea0SLionel Sambuc if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
183ebfedea0SLionel Sambuc goto err;
184ebfedea0SLionel Sambuc if (BIO_printf(bp, "\n") <= 0)
185ebfedea0SLionel Sambuc goto err;
186*0a6a1f1dSLionel Sambuc } else {
187ebfedea0SLionel Sambuc /* explicit parameters */
188ebfedea0SLionel Sambuc int is_char_two = 0;
189ebfedea0SLionel Sambuc point_conversion_form_t form;
190ebfedea0SLionel Sambuc int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc if (tmp_nid == NID_X9_62_characteristic_two_field)
193ebfedea0SLionel Sambuc is_char_two = 1;
194ebfedea0SLionel Sambuc
195ebfedea0SLionel Sambuc if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
196ebfedea0SLionel Sambuc (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
197*0a6a1f1dSLionel Sambuc (cofactor = BN_new()) == NULL) {
198ebfedea0SLionel Sambuc reason = ERR_R_MALLOC_FAILURE;
199ebfedea0SLionel Sambuc goto err;
200ebfedea0SLionel Sambuc }
201ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_EC2M
202*0a6a1f1dSLionel Sambuc if (is_char_two) {
203*0a6a1f1dSLionel Sambuc if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
204ebfedea0SLionel Sambuc reason = ERR_R_EC_LIB;
205ebfedea0SLionel Sambuc goto err;
206ebfedea0SLionel Sambuc }
207*0a6a1f1dSLionel Sambuc } else /* prime field */
208ebfedea0SLionel Sambuc #endif
209ebfedea0SLionel Sambuc {
210*0a6a1f1dSLionel Sambuc if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
211ebfedea0SLionel Sambuc reason = ERR_R_EC_LIB;
212ebfedea0SLionel Sambuc goto err;
213ebfedea0SLionel Sambuc }
214ebfedea0SLionel Sambuc }
215ebfedea0SLionel Sambuc
216*0a6a1f1dSLionel Sambuc if ((point = EC_GROUP_get0_generator(x)) == NULL) {
217ebfedea0SLionel Sambuc reason = ERR_R_EC_LIB;
218ebfedea0SLionel Sambuc goto err;
219ebfedea0SLionel Sambuc }
220ebfedea0SLionel Sambuc if (!EC_GROUP_get_order(x, order, NULL) ||
221*0a6a1f1dSLionel Sambuc !EC_GROUP_get_cofactor(x, cofactor, NULL)) {
222ebfedea0SLionel Sambuc reason = ERR_R_EC_LIB;
223ebfedea0SLionel Sambuc goto err;
224ebfedea0SLionel Sambuc }
225ebfedea0SLionel Sambuc
226ebfedea0SLionel Sambuc form = EC_GROUP_get_point_conversion_form(x);
227ebfedea0SLionel Sambuc
228*0a6a1f1dSLionel Sambuc if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
229ebfedea0SLionel Sambuc reason = ERR_R_EC_LIB;
230ebfedea0SLionel Sambuc goto err;
231ebfedea0SLionel Sambuc }
232ebfedea0SLionel Sambuc
233ebfedea0SLionel Sambuc buf_len = (size_t)BN_num_bytes(p);
234ebfedea0SLionel Sambuc if (buf_len < (i = (size_t)BN_num_bytes(a)))
235ebfedea0SLionel Sambuc buf_len = i;
236ebfedea0SLionel Sambuc if (buf_len < (i = (size_t)BN_num_bytes(b)))
237ebfedea0SLionel Sambuc buf_len = i;
238ebfedea0SLionel Sambuc if (buf_len < (i = (size_t)BN_num_bytes(gen)))
239ebfedea0SLionel Sambuc buf_len = i;
240ebfedea0SLionel Sambuc if (buf_len < (i = (size_t)BN_num_bytes(order)))
241ebfedea0SLionel Sambuc buf_len = i;
242ebfedea0SLionel Sambuc if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
243ebfedea0SLionel Sambuc buf_len = i;
244ebfedea0SLionel Sambuc
245ebfedea0SLionel Sambuc if ((seed = EC_GROUP_get0_seed(x)) != NULL)
246ebfedea0SLionel Sambuc seed_len = EC_GROUP_get_seed_len(x);
247ebfedea0SLionel Sambuc
248ebfedea0SLionel Sambuc buf_len += 10;
249*0a6a1f1dSLionel Sambuc if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
250ebfedea0SLionel Sambuc reason = ERR_R_MALLOC_FAILURE;
251ebfedea0SLionel Sambuc goto err;
252ebfedea0SLionel Sambuc }
253ebfedea0SLionel Sambuc
254ebfedea0SLionel Sambuc if (!BIO_indent(bp, off, 128))
255ebfedea0SLionel Sambuc goto err;
256ebfedea0SLionel Sambuc
257ebfedea0SLionel Sambuc /* print the 'short name' of the field type */
258ebfedea0SLionel Sambuc if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
259ebfedea0SLionel Sambuc <= 0)
260ebfedea0SLionel Sambuc goto err;
261ebfedea0SLionel Sambuc
262*0a6a1f1dSLionel Sambuc if (is_char_two) {
263ebfedea0SLionel Sambuc /* print the 'short name' of the base type OID */
264ebfedea0SLionel Sambuc int basis_type = EC_GROUP_get_basis_type(x);
265ebfedea0SLionel Sambuc if (basis_type == 0)
266ebfedea0SLionel Sambuc goto err;
267ebfedea0SLionel Sambuc
268ebfedea0SLionel Sambuc if (!BIO_indent(bp, off, 128))
269ebfedea0SLionel Sambuc goto err;
270ebfedea0SLionel Sambuc
271ebfedea0SLionel Sambuc if (BIO_printf(bp, "Basis Type: %s\n",
272ebfedea0SLionel Sambuc OBJ_nid2sn(basis_type)) <= 0)
273ebfedea0SLionel Sambuc goto err;
274ebfedea0SLionel Sambuc
275ebfedea0SLionel Sambuc /* print the polynomial */
276ebfedea0SLionel Sambuc if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
277ebfedea0SLionel Sambuc off))
278ebfedea0SLionel Sambuc goto err;
279*0a6a1f1dSLionel Sambuc } else {
280ebfedea0SLionel Sambuc if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer, off))
281ebfedea0SLionel Sambuc goto err;
282ebfedea0SLionel Sambuc }
283ebfedea0SLionel Sambuc if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off))
284ebfedea0SLionel Sambuc goto err;
285ebfedea0SLionel Sambuc if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off))
286ebfedea0SLionel Sambuc goto err;
287*0a6a1f1dSLionel Sambuc if (form == POINT_CONVERSION_COMPRESSED) {
288ebfedea0SLionel Sambuc if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
289ebfedea0SLionel Sambuc buffer, off))
290ebfedea0SLionel Sambuc goto err;
291*0a6a1f1dSLionel Sambuc } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
292ebfedea0SLionel Sambuc if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
293ebfedea0SLionel Sambuc buffer, off))
294ebfedea0SLionel Sambuc goto err;
295*0a6a1f1dSLionel Sambuc } else { /* form == POINT_CONVERSION_HYBRID */
296*0a6a1f1dSLionel Sambuc
297ebfedea0SLionel Sambuc if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
298ebfedea0SLionel Sambuc buffer, off))
299ebfedea0SLionel Sambuc goto err;
300ebfedea0SLionel Sambuc }
301ebfedea0SLionel Sambuc if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
302*0a6a1f1dSLionel Sambuc buffer, off))
303*0a6a1f1dSLionel Sambuc goto err;
304ebfedea0SLionel Sambuc if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
305*0a6a1f1dSLionel Sambuc buffer, off))
306*0a6a1f1dSLionel Sambuc goto err;
307ebfedea0SLionel Sambuc if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
308ebfedea0SLionel Sambuc goto err;
309ebfedea0SLionel Sambuc }
310ebfedea0SLionel Sambuc ret = 1;
311ebfedea0SLionel Sambuc err:
312ebfedea0SLionel Sambuc if (!ret)
313ebfedea0SLionel Sambuc ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
314ebfedea0SLionel Sambuc if (p)
315ebfedea0SLionel Sambuc BN_free(p);
316ebfedea0SLionel Sambuc if (a)
317ebfedea0SLionel Sambuc BN_free(a);
318ebfedea0SLionel Sambuc if (b)
319ebfedea0SLionel Sambuc BN_free(b);
320ebfedea0SLionel Sambuc if (gen)
321ebfedea0SLionel Sambuc BN_free(gen);
322ebfedea0SLionel Sambuc if (order)
323ebfedea0SLionel Sambuc BN_free(order);
324ebfedea0SLionel Sambuc if (cofactor)
325ebfedea0SLionel Sambuc BN_free(cofactor);
326ebfedea0SLionel Sambuc if (ctx)
327ebfedea0SLionel Sambuc BN_CTX_free(ctx);
328ebfedea0SLionel Sambuc if (buffer != NULL)
329ebfedea0SLionel Sambuc OPENSSL_free(buffer);
330ebfedea0SLionel Sambuc return (ret);
331ebfedea0SLionel Sambuc }
332ebfedea0SLionel Sambuc
print_bin(BIO * fp,const char * name,const unsigned char * buf,size_t len,int off)333ebfedea0SLionel Sambuc static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
334ebfedea0SLionel Sambuc size_t len, int off)
335ebfedea0SLionel Sambuc {
336ebfedea0SLionel Sambuc size_t i;
337ebfedea0SLionel Sambuc char str[128];
338ebfedea0SLionel Sambuc
339ebfedea0SLionel Sambuc if (buf == NULL)
340ebfedea0SLionel Sambuc return 1;
341*0a6a1f1dSLionel Sambuc if (off > 0) {
342ebfedea0SLionel Sambuc if (off > 128)
343ebfedea0SLionel Sambuc off = 128;
344ebfedea0SLionel Sambuc memset(str, ' ', off);
345ebfedea0SLionel Sambuc if (BIO_write(fp, str, off) <= 0)
346ebfedea0SLionel Sambuc return 0;
347*0a6a1f1dSLionel Sambuc } else {
348*0a6a1f1dSLionel Sambuc off = 0;
349ebfedea0SLionel Sambuc }
350ebfedea0SLionel Sambuc
351ebfedea0SLionel Sambuc if (BIO_printf(fp, "%s", name) <= 0)
352ebfedea0SLionel Sambuc return 0;
353ebfedea0SLionel Sambuc
354*0a6a1f1dSLionel Sambuc for (i = 0; i < len; i++) {
355*0a6a1f1dSLionel Sambuc if ((i % 15) == 0) {
356ebfedea0SLionel Sambuc str[0] = '\n';
357ebfedea0SLionel Sambuc memset(&(str[1]), ' ', off + 4);
358ebfedea0SLionel Sambuc if (BIO_write(fp, str, off + 1 + 4) <= 0)
359ebfedea0SLionel Sambuc return 0;
360ebfedea0SLionel Sambuc }
361*0a6a1f1dSLionel Sambuc if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
362*0a6a1f1dSLionel Sambuc 0)
363ebfedea0SLionel Sambuc return 0;
364ebfedea0SLionel Sambuc }
365ebfedea0SLionel Sambuc if (BIO_write(fp, "\n", 1) <= 0)
366ebfedea0SLionel Sambuc return 0;
367ebfedea0SLionel Sambuc
368ebfedea0SLionel Sambuc return 1;
369ebfedea0SLionel Sambuc }
370