Import of old SSLeay release: SSLeay 0.8.1b
[openssl.git] / crypto / asn1 / t_pkey.c
1 /* crypto/asn1/t_pkey.c */
2 /* Copyright (C) 1995-1997 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
78 static int print(BIO *fp,char *str,BIGNUM *num,
79                 unsigned char *buf,int off);
80 #else
81 static int print();
82 #endif
83
84 #ifndef NO_RSA
85 #ifndef WIN16
86 int RSA_print_fp(fp,x,off)
87 FILE *fp;
88 RSA *x;
89 int 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
106 int RSA_print(bp,x,off)
107 BIO *bp;
108 RSA *x;
109 int 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;
149 err:
150         if (m != NULL) Free((char *)m);
151         return(ret);
152         }
153 #endif /* NO_RSA */
154
155 #ifndef NO_DSA
156 #ifndef WIN16
157 int DSA_print_fp(fp,x,off)
158 FILE *fp;
159 DSA *x;
160 int 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
177 int DSA_print(bp,x,off)
178 BIO *bp;
179 DSA *x;
180 int 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;
226 err:
227         if (m != NULL) Free((char *)m);
228         return(ret);
229         }
230 #endif /* !NO_DSA */
231
232 static int print(bp,number,num,buf,off)
233 BIO *bp;
234 char *number;
235 BIGNUM *num;
236 unsigned char *buf;
237 int 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 WIN16
287 int DHparams_print_fp(fp,x)
288 FILE *fp;
289 DH *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
306 int DHparams_print(bp,x)
307 BIO *bp;
308 DH *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 err:
333         if (m != NULL) Free((char *)m);
334         DHerr(DH_F_DHPARAMS_PRINT,reason);
335         return(ret);
336         }
337 #endif
338
339 #ifndef NO_DSA
340 #ifndef WIN16
341 int DSAparams_print_fp(fp,x)
342 FILE *fp;
343 DSA *x;
344         {
345         BIO *b;
346         int ret;
347
348         if ((b=BIO_new(BIO_s_file())) == NULL)
349                 {
350                 DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
351                 return(0);
352                 }
353         BIO_set_fp(b,fp,BIO_NOCLOSE);
354         ret=DSAparams_print(b, x);
355         BIO_free(b);
356         return(ret);
357         }
358 #endif
359
360 int DSAparams_print(bp,x)
361 BIO *bp;
362 DSA *x;
363         {
364         unsigned char *m=NULL;
365         int reason=ERR_R_BUF_LIB,i,ret=0;
366
367         i=BN_num_bytes(x->p);
368         m=(unsigned char *)Malloc((unsigned int)i+10);
369         if (m == NULL)
370                 {
371                 reason=ERR_R_MALLOC_FAILURE;
372                 goto err;
373                 }
374
375         if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
376                 BN_num_bits(x->p)) <= 0)
377                 goto err;
378         if (!print(bp,"p:",x->p,m,4)) goto err;
379         if (!print(bp,"q:",x->q,m,4)) goto err;
380         if (!print(bp,"g:",x->g,m,4)) goto err;
381         ret=1;
382 err:
383         if (m != NULL) Free((char *)m);
384         DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
385         return(ret);
386         }
387
388 #endif /* !NO_DSA */
389