1999-04-09 16:24:32 +00:00
|
|
|
/* crypto/dsa/dsa_asn1.c */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/asn1_mac.h>
|
1999-04-09 16:24:32 +00:00
|
|
|
|
|
|
|
DSA_SIG *DSA_SIG_new(void)
|
|
|
|
{
|
|
|
|
DSA_SIG *ret;
|
|
|
|
|
2000-06-01 22:19:21 +00:00
|
|
|
ret = OPENSSL_malloc(sizeof(DSA_SIG));
|
1999-04-09 16:24:32 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
{
|
|
|
|
DSAerr(DSA_F_DSA_SIG_NEW,ERR_R_MALLOC_FAILURE);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
ret->r = NULL;
|
|
|
|
ret->s = NULL;
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
void DSA_SIG_free(DSA_SIG *r)
|
1999-04-09 16:24:32 +00:00
|
|
|
{
|
|
|
|
if (r == NULL) return;
|
|
|
|
if (r->r) BN_clear_free(r->r);
|
|
|
|
if (r->s) BN_clear_free(r->s);
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(r);
|
1999-04-09 16:24:32 +00:00
|
|
|
}
|
|
|
|
|
2000-11-07 13:54:39 +00:00
|
|
|
int i2d_DSA_SIG(const DSA_SIG *v, unsigned char **pp)
|
1999-04-09 16:24:32 +00:00
|
|
|
{
|
|
|
|
int t=0,len;
|
|
|
|
ASN1_INTEGER rbs,sbs;
|
|
|
|
unsigned char *p;
|
|
|
|
|
2000-06-01 22:19:21 +00:00
|
|
|
rbs.data=OPENSSL_malloc(BN_num_bits(v->r)/8+1);
|
1999-04-09 16:24:32 +00:00
|
|
|
if (rbs.data == NULL)
|
|
|
|
{
|
|
|
|
DSAerr(DSA_F_I2D_DSA_SIG, ERR_R_MALLOC_FAILURE);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
rbs.type=V_ASN1_INTEGER;
|
|
|
|
rbs.length=BN_bn2bin(v->r,rbs.data);
|
2000-06-01 22:19:21 +00:00
|
|
|
sbs.data=OPENSSL_malloc(BN_num_bits(v->s)/8+1);
|
1999-04-09 16:24:32 +00:00
|
|
|
if (sbs.data == NULL)
|
|
|
|
{
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(rbs.data);
|
1999-04-09 16:24:32 +00:00
|
|
|
DSAerr(DSA_F_I2D_DSA_SIG, ERR_R_MALLOC_FAILURE);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
sbs.type=V_ASN1_INTEGER;
|
|
|
|
sbs.length=BN_bn2bin(v->s,sbs.data);
|
|
|
|
|
|
|
|
len=i2d_ASN1_INTEGER(&rbs,NULL);
|
|
|
|
len+=i2d_ASN1_INTEGER(&sbs,NULL);
|
|
|
|
|
|
|
|
if (pp)
|
|
|
|
{
|
|
|
|
p=*pp;
|
|
|
|
ASN1_put_object(&p,1,len,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
|
|
|
|
i2d_ASN1_INTEGER(&rbs,&p);
|
|
|
|
i2d_ASN1_INTEGER(&sbs,&p);
|
|
|
|
}
|
|
|
|
t=ASN1_object_size(1,len,V_ASN1_SEQUENCE);
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(rbs.data);
|
|
|
|
OPENSSL_free(sbs.data);
|
1999-04-09 16:24:32 +00:00
|
|
|
return(t);
|
|
|
|
}
|
|
|
|
|
2000-11-07 13:54:39 +00:00
|
|
|
DSA_SIG *d2i_DSA_SIG(DSA_SIG **a, const unsigned char **pp, long length)
|
1999-04-09 16:24:32 +00:00
|
|
|
{
|
|
|
|
int i=ERR_R_NESTED_ASN1_ERROR;
|
|
|
|
ASN1_INTEGER *bs=NULL;
|
|
|
|
M_ASN1_D2I_vars(a,DSA_SIG *,DSA_SIG_new);
|
|
|
|
|
|
|
|
M_ASN1_D2I_Init();
|
|
|
|
M_ASN1_D2I_start_sequence();
|
|
|
|
M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
|
|
|
|
if ((ret->r=BN_bin2bn(bs->data,bs->length,ret->r)) == NULL)
|
|
|
|
goto err_bn;
|
|
|
|
M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
|
|
|
|
if ((ret->s=BN_bin2bn(bs->data,bs->length,ret->s)) == NULL)
|
|
|
|
goto err_bn;
|
1999-10-20 01:50:23 +00:00
|
|
|
M_ASN1_BIT_STRING_free(bs);
|
2000-11-19 14:14:52 +00:00
|
|
|
bs = NULL;
|
1999-04-09 16:24:32 +00:00
|
|
|
M_ASN1_D2I_Finish_2(a);
|
|
|
|
|
|
|
|
err_bn:
|
|
|
|
i=ERR_R_BN_LIB;
|
|
|
|
err:
|
|
|
|
DSAerr(DSA_F_D2I_DSA_SIG,i);
|
|
|
|
if ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_SIG_free(ret);
|
1999-10-20 01:50:23 +00:00
|
|
|
if (bs != NULL) M_ASN1_BIT_STRING_free(bs);
|
1999-04-09 16:24:32 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|