1 /* ====================================================================
2 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 void do_print_errors(void)
52 const char *file, *data;
55 while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)))
57 fprintf(stderr, "ERROR:%lx:lib=%d,func=%d,reason=%d"
58 ":file=%s:line=%d:%s\n",
59 l, ERR_GET_LIB(l), ERR_GET_FUNC(l), ERR_GET_REASON(l),
60 file, line, flags & ERR_TXT_STRING ? data : "");
64 int hex2bin(const char *in, unsigned char *out)
69 for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
71 if ((in[n1] >= '0') && (in[n1] <= '9'))
73 else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
74 ch = in[n1++] - 'A' + 10;
75 else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
76 ch = in[n1++] - 'a' + 10;
86 if ((in[n1] >= '0') && (in[n1] <= '9'))
88 else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
89 ch = in[n1++] - 'A' + 10;
90 else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
91 ch = in[n1++] - 'a' + 10;
99 unsigned char *hex2bin_m(const char *in, long *plen)
102 p = OPENSSL_malloc((strlen(in) + 1)/2);
103 *plen = hex2bin(in, p);
107 int do_hex2bn(BIGNUM **pr, const char *in)
112 p = hex2bin_m(in, &plen);
119 if (BN_bin2bn(p, plen, *pr))
125 int do_bn_print(FILE *out, BIGNUM *bn)
129 len = BN_num_bytes(bn);
136 tmp = OPENSSL_malloc(len);
139 fprintf(stderr, "Memory allocation error\n");
143 for (i = 0; i < len; i++)
144 fprintf(out, "%02x", tmp[i]);
149 int do_bn_print_name(FILE *out, const char *name, BIGNUM *bn)
152 fprintf(out, "%s = ", name);
153 r = do_bn_print(out, bn);
160 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
162 char *keyword, *value, *p, *q;
163 strcpy(linebuf, olinebuf);
165 /* Skip leading space */
166 while (isspace((unsigned char)*keyword))
169 /* Look for = sign */
170 p = strchr(linebuf, '=');
178 /* Remove trailing space */
179 while (isspace((unsigned char)*q))
185 /* Remove leading space from value */
186 while (isspace((unsigned char)*value))
189 /* Remove trailing space from value */
190 p = value + strlen(value) - 1;
192 while (*p == '\n' || isspace((unsigned char)*p))
200 BIGNUM *hex2bn(const char *in)
204 if (!do_hex2bn(&p, in))
210 int bin2hex(const unsigned char *in,int len,char *out)
215 for (n1=0,n2=0 ; n1 < len ; ++n1)
232 void pv(const char *tag,const unsigned char *val,int len)
236 bin2hex(val,len,obuf);
237 printf("%s = %s\n",tag,obuf);
240 /* To avoid extensive changes to test program at this stage just convert
241 * the input line into an acceptable form. Keyword lines converted to form
242 * "keyword = value\n" no matter what white space present, all other lines
243 * just have leading and trailing space removed.
246 int tidy_line(char *linebuf, char *olinebuf)
248 char *keyword, *value, *p, *q;
249 strcpy(linebuf, olinebuf);
251 /* Skip leading space */
252 while (isspace((unsigned char)*keyword))
254 /* Look for = sign */
255 p = strchr(linebuf, '=');
257 /* If no '=' just chop leading, trailing ws */
260 p = keyword + strlen(keyword) - 1;
261 while (*p == '\n' || isspace((unsigned char)*p))
263 strcpy(olinebuf, keyword);
264 strcat(olinebuf, "\n");
270 /* Remove trailing space */
271 while (isspace((unsigned char)*q))
277 /* Remove leading space from value */
278 while (isspace((unsigned char)*value))
281 /* Remove trailing space from value */
282 p = value + strlen(value) - 1;
284 while (*p == '\n' || isspace((unsigned char)*p))
287 strcpy(olinebuf, keyword);
288 strcat(olinebuf, " = ");
289 strcat(olinebuf, value);
290 strcat(olinebuf, "\n");
295 /* NB: this return the number of _bits_ read */
296 int bint2bin(const char *in, int len, unsigned char *out)
301 for(n=0 ; n < len ; ++n)
303 out[n/8]|=(0x80 >> (n%8));
307 int bin2bint(const unsigned char *in,int len,char *out)
311 for(n=0 ; n < len ; ++n)
312 out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
316 /*-----------------------------------------------*/
318 void PrintValue(char *tag, unsigned char *val, int len)
323 olen = bin2hex(val, len, obuf);
324 printf("%s = %.*s\n", tag, olen, obuf);
328 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
334 olen=bin2bint(val,len,obuf);
336 olen=bin2hex(val,len,obuf);
338 fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
340 printf("%s = %.*s\n", tag, olen, obuf);