summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/t_pkey.c
diff options
context:
space:
mode:
authorryker <>1998-10-05 20:13:14 +0000
committerryker <>1998-10-05 20:13:14 +0000
commitaeeae06a79815dc190061534d47236cec09f9e32 (patch)
tree851692b9c2f9c04f077666855641900f19fdb217 /src/lib/libcrypto/asn1/t_pkey.c
parenta4f79641824cbf9f60ca9d1168d1fcc46717a82a (diff)
downloadopenbsd-aeeae06a79815dc190061534d47236cec09f9e32.tar.gz
openbsd-aeeae06a79815dc190061534d47236cec09f9e32.tar.bz2
openbsd-aeeae06a79815dc190061534d47236cec09f9e32.zip
Import of SSLeay-0.9.0b with RSA and IDEA stubbed + OpenBSD build
functionality for shared libs. Note that routines such as sslv2_init and friends that use RSA will not work due to lack of RSA in this library. Needs documentation and help from ports for easy upgrade to full functionality where legally possible.
Diffstat (limited to 'src/lib/libcrypto/asn1/t_pkey.c')
-rw-r--r--src/lib/libcrypto/asn1/t_pkey.c392
1 files changed, 392 insertions, 0 deletions
diff --git a/src/lib/libcrypto/asn1/t_pkey.c b/src/lib/libcrypto/asn1/t_pkey.c
new file mode 100644
index 0000000000..bc518d59a2
--- /dev/null
+++ b/src/lib/libcrypto/asn1/t_pkey.c
@@ -0,0 +1,392 @@
1/* crypto/asn1/t_pkey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "bn.h"
63#ifndef NO_RSA
64#include "rsa.h"
65#endif
66#ifndef NO_DH
67#include "dh.h"
68#endif
69#ifndef NO_DSA
70#include "dsa.h"
71#endif
72
73/* DHerr(DH_F_DHPARAMS_PRINT,ERR_R_MALLOC_FAILURE);
74 * DSAerr(DSA_F_DSAPARAMS_PRINT,ERR_R_MALLOC_FAILURE);
75 */
76
77#ifndef NOPROTO
78static int print(BIO *fp,char *str,BIGNUM *num,
79 unsigned char *buf,int off);
80#else
81static int print();
82#endif
83
84#ifndef NO_RSA
85#ifndef NO_FP_API
86int RSA_print_fp(fp,x,off)
87FILE *fp;
88RSA *x;
89int off;
90 {
91 BIO *b;
92 int ret;
93
94 if ((b=BIO_new(BIO_s_file())) == NULL)
95 {
96 RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
97 return(0);
98 }
99 BIO_set_fp(b,fp,BIO_NOCLOSE);
100 ret=RSA_print(b,x,off);
101 BIO_free(b);
102 return(ret);
103 }
104#endif
105
106int RSA_print(bp,x,off)
107BIO *bp;
108RSA *x;
109int off;
110 {
111 char str[128],*s;
112 unsigned char *m=NULL;
113 int i,ret=0;
114
115 i=RSA_size(x);
116 m=(unsigned char *)Malloc((unsigned int)i+10);
117 if (m == NULL)
118 {
119 RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
120 goto err;
121 }
122
123 if (off)
124 {
125 if (off > 128) off=128;
126 memset(str,' ',off);
127 }
128 if (x->d != NULL)
129 {
130 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
131 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
132 <= 0) goto err;
133 }
134
135 if (x->d == NULL)
136 sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
137 else
138 strcpy(str,"modulus:");
139 if (!print(bp,str,x->n,m,off)) goto err;
140 s=(x->d == NULL)?"Exponent:":"publicExponent:";
141 if (!print(bp,s,x->e,m,off)) goto err;
142 if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
143 if (!print(bp,"prime1:",x->p,m,off)) goto err;
144 if (!print(bp,"prime2:",x->q,m,off)) goto err;
145 if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
146 if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
147 if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
148 ret=1;
149err:
150 if (m != NULL) Free((char *)m);
151 return(ret);
152 }
153#endif /* NO_RSA */
154
155#ifndef NO_DSA
156#ifndef NO_FP_API
157int DSA_print_fp(fp,x,off)
158FILE *fp;
159DSA *x;
160int off;
161 {
162 BIO *b;
163 int ret;
164
165 if ((b=BIO_new(BIO_s_file())) == NULL)
166 {
167 DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
168 return(0);
169 }
170 BIO_set_fp(b,fp,BIO_NOCLOSE);
171 ret=DSA_print(b,x,off);
172 BIO_free(b);
173 return(ret);
174 }
175#endif
176
177int DSA_print(bp,x,off)
178BIO *bp;
179DSA *x;
180int off;
181 {
182 char str[128];
183 unsigned char *m=NULL;
184 int i,ret=0;
185 BIGNUM *bn=NULL;
186
187 if (x->p != NULL)
188 bn=x->p;
189 else if (x->priv_key != NULL)
190 bn=x->priv_key;
191 else if (x->pub_key != NULL)
192 bn=x->pub_key;
193
194 /* larger than needed but what the hell :-) */
195 if (bn != NULL)
196 i=BN_num_bytes(bn)*2;
197 else
198 i=256;
199 m=(unsigned char *)Malloc((unsigned int)i+10);
200 if (m == NULL)
201 {
202 DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
203 goto err;
204 }
205
206 if (off)
207 {
208 if (off > 128) off=128;
209 memset(str,' ',off);
210 }
211 if (x->priv_key != NULL)
212 {
213 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
214 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
215 <= 0) goto err;
216 }
217
218 if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
219 goto err;
220 if ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
221 goto err;
222 if ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err;
223 if ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err;
224 if ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err;
225 ret=1;
226err:
227 if (m != NULL) Free((char *)m);
228 return(ret);
229 }
230#endif /* !NO_DSA */
231
232static int print(bp,number,num,buf,off)
233BIO *bp;
234char *number;
235BIGNUM *num;
236unsigned char *buf;
237int off;
238 {
239 int n,i;
240 char str[128],*neg;
241
242 if (num == NULL) return(1);
243 neg=(num->neg)?"-":"";
244 if (off)
245 {
246 if (off > 128) off=128;
247 memset(str,' ',off);
248 if (BIO_write(bp,str,off) <= 0) return(0);
249 }
250
251 if (BN_num_bytes(num) <= BN_BYTES)
252 {
253 if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
254 (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
255 <= 0) return(0);
256 }
257 else
258 {
259 buf[0]=0;
260 if (BIO_printf(bp,"%s%s",number,
261 (neg[0] == '-')?" (Negative)":"") <= 0)
262 return(0);
263 n=BN_bn2bin(num,&buf[1]);
264
265 if (buf[1] & 0x80)
266 n++;
267 else buf++;
268
269 for (i=0; i<n; i++)
270 {
271 if ((i%15) == 0)
272 {
273 str[0]='\n';
274 memset(&(str[1]),' ',off+4);
275 if (BIO_write(bp,str,off+1+4) <= 0) return(0);
276 }
277 if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
278 <= 0) return(0);
279 }
280 if (BIO_write(bp,"\n",1) <= 0) return(0);
281 }
282 return(1);
283 }
284
285#ifndef NO_DH
286#ifndef NO_FP_API
287int DHparams_print_fp(fp,x)
288FILE *fp;
289DH *x;
290 {
291 BIO *b;
292 int ret;
293
294 if ((b=BIO_new(BIO_s_file())) == NULL)
295 {
296 DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
297 return(0);
298 }
299 BIO_set_fp(b,fp,BIO_NOCLOSE);
300 ret=DHparams_print(b, x);
301 BIO_free(b);
302 return(ret);
303 }
304#endif
305
306int DHparams_print(bp,x)
307BIO *bp;
308DH *x;
309 {
310 unsigned char *m=NULL;
311 int reason=ERR_R_BUF_LIB,i,ret=0;
312
313 i=BN_num_bytes(x->p);
314 m=(unsigned char *)Malloc((unsigned int)i+10);
315 if (m == NULL)
316 {
317 reason=ERR_R_MALLOC_FAILURE;
318 goto err;
319 }
320
321 if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
322 BN_num_bits(x->p)) <= 0)
323 goto err;
324 if (!print(bp,"prime:",x->p,m,4)) goto err;
325 if (!print(bp,"generator:",x->g,m,4)) goto err;
326 if (x->length != 0)
327 {
328 if (BIO_printf(bp," recomented-private-length: %d bits\n",
329 (int)x->length) <= 0) goto err;
330 }
331 ret=1;
332 if (0)
333 {
334err:
335 DHerr(DH_F_DHPARAMS_PRINT,reason);
336 }
337 if (m != NULL) Free((char *)m);
338 return(ret);
339 }
340#endif
341
342#ifndef NO_DSA
343#ifndef NO_FP_API
344int DSAparams_print_fp(fp,x)
345FILE *fp;
346DSA *x;
347 {
348 BIO *b;
349 int ret;
350
351 if ((b=BIO_new(BIO_s_file())) == NULL)
352 {
353 DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
354 return(0);
355 }
356 BIO_set_fp(b,fp,BIO_NOCLOSE);
357 ret=DSAparams_print(b, x);
358 BIO_free(b);
359 return(ret);
360 }
361#endif
362
363int DSAparams_print(bp,x)
364BIO *bp;
365DSA *x;
366 {
367 unsigned char *m=NULL;
368 int reason=ERR_R_BUF_LIB,i,ret=0;
369
370 i=BN_num_bytes(x->p);
371 m=(unsigned char *)Malloc((unsigned int)i+10);
372 if (m == NULL)
373 {
374 reason=ERR_R_MALLOC_FAILURE;
375 goto err;
376 }
377
378 if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
379 BN_num_bits(x->p)) <= 0)
380 goto err;
381 if (!print(bp,"p:",x->p,m,4)) goto err;
382 if (!print(bp,"q:",x->q,m,4)) goto err;
383 if (!print(bp,"g:",x->g,m,4)) goto err;
384 ret=1;
385err:
386 if (m != NULL) Free((char *)m);
387 DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
388 return(ret);
389 }
390
391#endif /* !NO_DSA */
392