t_pkey.c revision 55714
155714Skris/* crypto/asn1/t_pkey.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/buffer.h>
6255714Skris#include <openssl/bn.h>
6355714Skris#ifndef NO_RSA
6455714Skris#include <openssl/rsa.h>
6555714Skris#endif
6655714Skris#ifndef NO_DH
6755714Skris#include <openssl/dh.h>
6855714Skris#endif
6955714Skris#ifndef NO_DSA
7055714Skris#include <openssl/dsa.h>
7155714Skris#endif
7255714Skris
7355714Skrisstatic int print(BIO *fp,const char *str,BIGNUM *num,
7455714Skris		unsigned char *buf,int off);
7555714Skris#ifndef NO_RSA
7655714Skris#ifndef NO_FP_API
7755714Skrisint RSA_print_fp(FILE *fp, RSA *x, int off)
7855714Skris        {
7955714Skris        BIO *b;
8055714Skris        int ret;
8155714Skris
8255714Skris        if ((b=BIO_new(BIO_s_file())) == NULL)
8355714Skris		{
8455714Skris		RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
8555714Skris                return(0);
8655714Skris		}
8755714Skris        BIO_set_fp(b,fp,BIO_NOCLOSE);
8855714Skris        ret=RSA_print(b,x,off);
8955714Skris        BIO_free(b);
9055714Skris        return(ret);
9155714Skris        }
9255714Skris#endif
9355714Skris
9455714Skrisint RSA_print(BIO *bp, RSA *x, int off)
9555714Skris	{
9655714Skris	char str[128];
9755714Skris	const char *s;
9855714Skris	unsigned char *m=NULL;
9955714Skris	int i,ret=0;
10055714Skris
10155714Skris	i=RSA_size(x);
10255714Skris	m=(unsigned char *)Malloc((unsigned int)i+10);
10355714Skris	if (m == NULL)
10455714Skris		{
10555714Skris		RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
10655714Skris		goto err;
10755714Skris		}
10855714Skris
10955714Skris	if (off)
11055714Skris		{
11155714Skris		if (off > 128) off=128;
11255714Skris		memset(str,' ',off);
11355714Skris		}
11455714Skris	if (x->d != NULL)
11555714Skris		{
11655714Skris		if (off && (BIO_write(bp,str,off) <= 0)) goto err;
11755714Skris		if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
11855714Skris			<= 0) goto err;
11955714Skris		}
12055714Skris
12155714Skris	if (x->d == NULL)
12255714Skris		sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
12355714Skris	else
12455714Skris		strcpy(str,"modulus:");
12555714Skris	if (!print(bp,str,x->n,m,off)) goto err;
12655714Skris	s=(x->d == NULL)?"Exponent:":"publicExponent:";
12755714Skris	if (!print(bp,s,x->e,m,off)) goto err;
12855714Skris	if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
12955714Skris	if (!print(bp,"prime1:",x->p,m,off)) goto err;
13055714Skris	if (!print(bp,"prime2:",x->q,m,off)) goto err;
13155714Skris	if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
13255714Skris	if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
13355714Skris	if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
13455714Skris	ret=1;
13555714Skriserr:
13655714Skris	if (m != NULL) Free((char *)m);
13755714Skris	return(ret);
13855714Skris	}
13955714Skris#endif /* NO_RSA */
14055714Skris
14155714Skris#ifndef NO_DSA
14255714Skris#ifndef NO_FP_API
14355714Skrisint DSA_print_fp(FILE *fp, DSA *x, int off)
14455714Skris	{
14555714Skris	BIO *b;
14655714Skris	int ret;
14755714Skris
14855714Skris	if ((b=BIO_new(BIO_s_file())) == NULL)
14955714Skris		{
15055714Skris		DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
15155714Skris		return(0);
15255714Skris		}
15355714Skris	BIO_set_fp(b,fp,BIO_NOCLOSE);
15455714Skris	ret=DSA_print(b,x,off);
15555714Skris	BIO_free(b);
15655714Skris	return(ret);
15755714Skris	}
15855714Skris#endif
15955714Skris
16055714Skrisint DSA_print(BIO *bp, DSA *x, int off)
16155714Skris	{
16255714Skris	char str[128];
16355714Skris	unsigned char *m=NULL;
16455714Skris	int i,ret=0;
16555714Skris	BIGNUM *bn=NULL;
16655714Skris
16755714Skris	if (x->p != NULL)
16855714Skris		bn=x->p;
16955714Skris	else if (x->priv_key != NULL)
17055714Skris		bn=x->priv_key;
17155714Skris	else if (x->pub_key != NULL)
17255714Skris		bn=x->pub_key;
17355714Skris
17455714Skris	/* larger than needed but what the hell :-) */
17555714Skris	if (bn != NULL)
17655714Skris		i=BN_num_bytes(bn)*2;
17755714Skris	else
17855714Skris		i=256;
17955714Skris	m=(unsigned char *)Malloc((unsigned int)i+10);
18055714Skris	if (m == NULL)
18155714Skris		{
18255714Skris		DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
18355714Skris		goto err;
18455714Skris		}
18555714Skris
18655714Skris	if (off)
18755714Skris		{
18855714Skris		if (off > 128) off=128;
18955714Skris		memset(str,' ',off);
19055714Skris		}
19155714Skris	if (x->priv_key != NULL)
19255714Skris		{
19355714Skris		if (off && (BIO_write(bp,str,off) <= 0)) goto err;
19455714Skris		if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
19555714Skris			<= 0) goto err;
19655714Skris		}
19755714Skris
19855714Skris	if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
19955714Skris		goto err;
20055714Skris	if ((x->pub_key  != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
20155714Skris		goto err;
20255714Skris	if ((x->p != NULL) && !print(bp,"P:   ",x->p,m,off)) goto err;
20355714Skris	if ((x->q != NULL) && !print(bp,"Q:   ",x->q,m,off)) goto err;
20455714Skris	if ((x->g != NULL) && !print(bp,"G:   ",x->g,m,off)) goto err;
20555714Skris	ret=1;
20655714Skriserr:
20755714Skris	if (m != NULL) Free((char *)m);
20855714Skris	return(ret);
20955714Skris	}
21055714Skris#endif /* !NO_DSA */
21155714Skris
21255714Skrisstatic int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
21355714Skris	     int off)
21455714Skris	{
21555714Skris	int n,i;
21655714Skris	char str[128];
21755714Skris	const char *neg;
21855714Skris
21955714Skris	if (num == NULL) return(1);
22055714Skris	neg=(num->neg)?"-":"";
22155714Skris	if (off)
22255714Skris		{
22355714Skris		if (off > 128) off=128;
22455714Skris		memset(str,' ',off);
22555714Skris		if (BIO_write(bp,str,off) <= 0) return(0);
22655714Skris		}
22755714Skris
22855714Skris	if (BN_num_bytes(num) <= BN_BYTES)
22955714Skris		{
23055714Skris		if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
23155714Skris			(unsigned long)num->d[0],neg,(unsigned long)num->d[0])
23255714Skris			<= 0) return(0);
23355714Skris		}
23455714Skris	else
23555714Skris		{
23655714Skris		buf[0]=0;
23755714Skris		if (BIO_printf(bp,"%s%s",number,
23855714Skris			(neg[0] == '-')?" (Negative)":"") <= 0)
23955714Skris			return(0);
24055714Skris		n=BN_bn2bin(num,&buf[1]);
24155714Skris
24255714Skris		if (buf[1] & 0x80)
24355714Skris			n++;
24455714Skris		else	buf++;
24555714Skris
24655714Skris		for (i=0; i<n; i++)
24755714Skris			{
24855714Skris			if ((i%15) == 0)
24955714Skris				{
25055714Skris				str[0]='\n';
25155714Skris				memset(&(str[1]),' ',off+4);
25255714Skris				if (BIO_write(bp,str,off+1+4) <= 0) return(0);
25355714Skris				}
25455714Skris			if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
25555714Skris				<= 0) return(0);
25655714Skris			}
25755714Skris		if (BIO_write(bp,"\n",1) <= 0) return(0);
25855714Skris		}
25955714Skris	return(1);
26055714Skris	}
26155714Skris
26255714Skris#ifndef NO_DH
26355714Skris#ifndef NO_FP_API
26455714Skrisint DHparams_print_fp(FILE *fp, DH *x)
26555714Skris        {
26655714Skris        BIO *b;
26755714Skris        int ret;
26855714Skris
26955714Skris        if ((b=BIO_new(BIO_s_file())) == NULL)
27055714Skris		{
27155714Skris		DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
27255714Skris                return(0);
27355714Skris		}
27455714Skris        BIO_set_fp(b,fp,BIO_NOCLOSE);
27555714Skris        ret=DHparams_print(b, x);
27655714Skris        BIO_free(b);
27755714Skris        return(ret);
27855714Skris        }
27955714Skris#endif
28055714Skris
28155714Skrisint DHparams_print(BIO *bp, DH *x)
28255714Skris	{
28355714Skris	unsigned char *m=NULL;
28455714Skris	int reason=ERR_R_BUF_LIB,i,ret=0;
28555714Skris
28655714Skris	i=BN_num_bytes(x->p);
28755714Skris	m=(unsigned char *)Malloc((unsigned int)i+10);
28855714Skris	if (m == NULL)
28955714Skris		{
29055714Skris		reason=ERR_R_MALLOC_FAILURE;
29155714Skris		goto err;
29255714Skris		}
29355714Skris
29455714Skris	if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
29555714Skris		BN_num_bits(x->p)) <= 0)
29655714Skris		goto err;
29755714Skris	if (!print(bp,"prime:",x->p,m,4)) goto err;
29855714Skris	if (!print(bp,"generator:",x->g,m,4)) goto err;
29955714Skris	if (x->length != 0)
30055714Skris		{
30155714Skris		if (BIO_printf(bp,"    recomented-private-length: %d bits\n",
30255714Skris			(int)x->length) <= 0) goto err;
30355714Skris		}
30455714Skris	ret=1;
30555714Skris	if (0)
30655714Skris		{
30755714Skriserr:
30855714Skris		DHerr(DH_F_DHPARAMS_PRINT,reason);
30955714Skris		}
31055714Skris	if (m != NULL) Free((char *)m);
31155714Skris	return(ret);
31255714Skris	}
31355714Skris#endif
31455714Skris
31555714Skris#ifndef NO_DSA
31655714Skris#ifndef NO_FP_API
31755714Skrisint DSAparams_print_fp(FILE *fp, DSA *x)
31855714Skris        {
31955714Skris        BIO *b;
32055714Skris        int ret;
32155714Skris
32255714Skris        if ((b=BIO_new(BIO_s_file())) == NULL)
32355714Skris		{
32455714Skris		DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
32555714Skris                return(0);
32655714Skris		}
32755714Skris        BIO_set_fp(b,fp,BIO_NOCLOSE);
32855714Skris        ret=DSAparams_print(b, x);
32955714Skris        BIO_free(b);
33055714Skris        return(ret);
33155714Skris        }
33255714Skris#endif
33355714Skris
33455714Skrisint DSAparams_print(BIO *bp, DSA *x)
33555714Skris	{
33655714Skris	unsigned char *m=NULL;
33755714Skris	int reason=ERR_R_BUF_LIB,i,ret=0;
33855714Skris
33955714Skris	i=BN_num_bytes(x->p);
34055714Skris	m=(unsigned char *)Malloc((unsigned int)i+10);
34155714Skris	if (m == NULL)
34255714Skris		{
34355714Skris		reason=ERR_R_MALLOC_FAILURE;
34455714Skris		goto err;
34555714Skris		}
34655714Skris
34755714Skris	if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
34855714Skris		BN_num_bits(x->p)) <= 0)
34955714Skris		goto err;
35055714Skris	if (!print(bp,"p:",x->p,m,4)) goto err;
35155714Skris	if (!print(bp,"q:",x->q,m,4)) goto err;
35255714Skris	if (!print(bp,"g:",x->g,m,4)) goto err;
35355714Skris	ret=1;
35455714Skriserr:
35555714Skris	if (m != NULL) Free((char *)m);
35655714Skris	DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
35755714Skris	return(ret);
35855714Skris	}
35955714Skris
36055714Skris#endif /* !NO_DSA */
36155714Skris
362