1 /* ====================================================================
2  * Copyright (c) 2007 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
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
14  *    distribution.
15  *
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/)"
20  *
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.
25  *
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.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
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.
47  *
48  */
49 
do_print_errors(void)50 void do_print_errors(void)
51 	{
52 	const char *file, *data;
53 	int line, flags;
54 	unsigned long l;
55 	while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)))
56 		{
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 : "");
61 		}
62 	}
63 
hex2bin(const char * in,unsigned char * out)64 int hex2bin(const char *in, unsigned char *out)
65     {
66     int n1, n2;
67     unsigned char ch;
68 
69     for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
70 	{ /* first byte */
71 	if ((in[n1] >= '0') && (in[n1] <= '9'))
72 	    ch = in[n1++] - '0';
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;
77 	else
78 	    return -1;
79 	if(!in[n1])
80 	    {
81 	    out[n2++]=ch;
82 	    break;
83 	    }
84 	out[n2] = ch << 4;
85 	/* second byte */
86 	if ((in[n1] >= '0') && (in[n1] <= '9'))
87 	    ch = in[n1++] - '0';
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;
92 	else
93 	    return -1;
94 	out[n2++] |= ch;
95 	}
96     return n2;
97     }
98 
hex2bin_m(const char * in,long * plen)99 unsigned char *hex2bin_m(const char *in, long *plen)
100 	{
101 	unsigned char *p;
102 	p = OPENSSL_malloc((strlen(in) + 1)/2);
103 	*plen = hex2bin(in, p);
104 	return p;
105 	}
106 
do_hex2bn(BIGNUM ** pr,const char * in)107 int do_hex2bn(BIGNUM **pr, const char *in)
108 	{
109 	unsigned char *p;
110 	long plen;
111 	int r = 0;
112 	p = hex2bin_m(in, &plen);
113 	if (!p)
114 		return 0;
115 	if (!*pr)
116 		*pr = BN_new();
117 	if (!*pr)
118 		return 0;
119 	if (BN_bin2bn(p, plen, *pr))
120 		r = 1;
121 	OPENSSL_free(p);
122 	return r;
123 	}
124 
do_bn_print(FILE * out,BIGNUM * bn)125 int do_bn_print(FILE *out, BIGNUM *bn)
126 	{
127 	int len, i;
128 	unsigned char *tmp;
129 	len = BN_num_bytes(bn);
130 	if (len == 0)
131 		{
132 		fputs("00", out);
133 		return 1;
134 		}
135 
136 	tmp = OPENSSL_malloc(len);
137 	if (!tmp)
138 		{
139 		fprintf(stderr, "Memory allocation error\n");
140 		return 0;
141 		}
142 	BN_bn2bin(bn, tmp);
143 	for (i = 0; i < len; i++)
144 		fprintf(out, "%02x", tmp[i]);
145 	OPENSSL_free(tmp);
146 	return 1;
147 	}
148 
do_bn_print_name(FILE * out,const char * name,BIGNUM * bn)149 int do_bn_print_name(FILE *out, const char *name, BIGNUM *bn)
150 	{
151 	int r;
152 	fprintf(out, "%s = ", name);
153 	r = do_bn_print(out, bn);
154 	if (!r)
155 		return 0;
156 	fputs("\n", out);
157 	return 1;
158 	}
159 
parse_line(char ** pkw,char ** pval,char * linebuf,char * olinebuf)160 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
161 	{
162 	char *keyword, *value, *p, *q;
163 	strcpy(linebuf, olinebuf);
164 	keyword = linebuf;
165 	/* Skip leading space */
166 	while (isspace((unsigned char)*keyword))
167 		keyword++;
168 
169 	/* Look for = sign */
170 	p = strchr(linebuf, '=');
171 
172 	/* If no '=' exit */
173 	if (!p)
174 		return 0;
175 
176 	q = p - 1;
177 
178 	/* Remove trailing space */
179 	while (isspace((unsigned char)*q))
180 		*q-- = 0;
181 
182 	*p = 0;
183 	value = p + 1;
184 
185 	/* Remove leading space from value */
186 	while (isspace((unsigned char)*value))
187 		value++;
188 
189 	/* Remove trailing space from value */
190 	p = value + strlen(value) - 1;
191 
192 	while (*p == '\n' || isspace((unsigned char)*p))
193 		*p-- = 0;
194 
195 	*pkw = keyword;
196 	*pval = value;
197 	return 1;
198 	}
199 
hex2bn(const char * in)200 BIGNUM *hex2bn(const char *in)
201     {
202     BIGNUM *p=NULL;
203 
204     if (!do_hex2bn(&p, in))
205 	return NULL;
206 
207     return p;
208     }
209 
bin2hex(const unsigned char * in,int len,char * out)210 int bin2hex(const unsigned char *in,int len,char *out)
211     {
212     int n1, n2;
213     unsigned char ch;
214 
215     for (n1=0,n2=0 ; n1 < len ; ++n1)
216 	{
217 	ch=in[n1] >> 4;
218 	if (ch <= 0x09)
219 	    out[n2++]=ch+'0';
220 	else
221 	    out[n2++]=ch-10+'a';
222 	ch=in[n1] & 0x0f;
223 	if(ch <= 0x09)
224 	    out[n2++]=ch+'0';
225 	else
226 	    out[n2++]=ch-10+'a';
227 	}
228     out[n2]='\0';
229     return n2;
230     }
231 
pv(const char * tag,const unsigned char * val,int len)232 void pv(const char *tag,const unsigned char *val,int len)
233     {
234     char obuf[2048];
235 
236     bin2hex(val,len,obuf);
237     printf("%s = %s\n",tag,obuf);
238     }
239 
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.
244  */
245 
tidy_line(char * linebuf,char * olinebuf)246 int tidy_line(char *linebuf, char *olinebuf)
247 	{
248 	char *keyword, *value, *p, *q;
249 	strcpy(linebuf, olinebuf);
250 	keyword = linebuf;
251 	/* Skip leading space */
252 	while (isspace((unsigned char)*keyword))
253 		keyword++;
254 	/* Look for = sign */
255 	p = strchr(linebuf, '=');
256 
257 	/* If no '=' just chop leading, trailing ws */
258 	if (!p)
259 		{
260 		p = keyword + strlen(keyword) - 1;
261 		while (*p == '\n' || isspace((unsigned char)*p))
262 			*p-- = 0;
263 		strcpy(olinebuf, keyword);
264 		strcat(olinebuf, "\n");
265 		return 1;
266 		}
267 
268 	q = p - 1;
269 
270 	/* Remove trailing space */
271 	while (isspace((unsigned char)*q))
272 		*q-- = 0;
273 
274 	*p = 0;
275 	value = p + 1;
276 
277 	/* Remove leading space from value */
278 	while (isspace((unsigned char)*value))
279 		value++;
280 
281 	/* Remove trailing space from value */
282 	p = value + strlen(value) - 1;
283 
284 	while (*p == '\n' || isspace((unsigned char)*p))
285 		*p-- = 0;
286 
287 	strcpy(olinebuf, keyword);
288 	strcat(olinebuf, " = ");
289 	strcat(olinebuf, value);
290 	strcat(olinebuf, "\n");
291 
292 	return 1;
293 	}
294 
295 /* NB: this return the number of _bits_ read */
bint2bin(const char * in,int len,unsigned char * out)296 int bint2bin(const char *in, int len, unsigned char *out)
297     {
298     int n;
299 
300     memset(out,0,len);
301     for(n=0 ; n < len ; ++n)
302 	if(in[n] == '1')
303 	    out[n/8]|=(0x80 >> (n%8));
304     return len;
305     }
306 
bin2bint(const unsigned char * in,int len,char * out)307 int bin2bint(const unsigned char *in,int len,char *out)
308     {
309     int n;
310 
311     for(n=0 ; n < len ; ++n)
312 	out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
313     return n;
314     }
315 
316 /*-----------------------------------------------*/
317 
PrintValue(char * tag,unsigned char * val,int len)318 void PrintValue(char *tag, unsigned char *val, int len)
319 {
320 #if VERBOSE
321   char obuf[2048];
322   int olen;
323   olen = bin2hex(val, len, obuf);
324   printf("%s = %.*s\n", tag, olen, obuf);
325 #endif
326 }
327 
OutputValue(char * tag,unsigned char * val,int len,FILE * rfp,int bitmode)328 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
329     {
330     char obuf[2048];
331     int olen;
332 
333     if(bitmode)
334 	olen=bin2bint(val,len,obuf);
335     else
336 	olen=bin2hex(val,len,obuf);
337 
338     fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
339 #if VERBOSE
340     printf("%s = %.*s\n", tag, olen, obuf);
341 #endif
342     }
343 
344