]>
Commit | Line | Data |
---|---|---|
a8da8918 UM |
1 | /* crypto/dsa/dsa_asn1.c */ |
2 | ||
3 | #include <stdio.h> | |
4 | #include "cryptlib.h" | |
ec577822 BM |
5 | #include <openssl/dsa.h> |
6 | #include <openssl/asn1.h> | |
7 | #include <openssl/asn1_mac.h> | |
a8da8918 UM |
8 | |
9 | DSA_SIG *DSA_SIG_new(void) | |
10 | { | |
11 | DSA_SIG *ret; | |
12 | ||
26a3a48d | 13 | ret = OPENSSL_malloc(sizeof(DSA_SIG)); |
a8da8918 UM |
14 | if (ret == NULL) |
15 | { | |
16 | DSAerr(DSA_F_DSA_SIG_NEW,ERR_R_MALLOC_FAILURE); | |
17 | return(NULL); | |
18 | } | |
19 | ret->r = NULL; | |
20 | ret->s = NULL; | |
21 | return(ret); | |
22 | } | |
23 | ||
6b691a5c | 24 | void DSA_SIG_free(DSA_SIG *r) |
a8da8918 UM |
25 | { |
26 | if (r == NULL) return; | |
27 | if (r->r) BN_clear_free(r->r); | |
28 | if (r->s) BN_clear_free(r->s); | |
26a3a48d | 29 | OPENSSL_free(r); |
a8da8918 UM |
30 | } |
31 | ||
a4aba800 | 32 | int i2d_DSA_SIG(const DSA_SIG *v, unsigned char **pp) |
a8da8918 UM |
33 | { |
34 | int t=0,len; | |
35 | ASN1_INTEGER rbs,sbs; | |
36 | unsigned char *p; | |
37 | ||
26a3a48d | 38 | rbs.data=OPENSSL_malloc(BN_num_bits(v->r)/8+1); |
a8da8918 UM |
39 | if (rbs.data == NULL) |
40 | { | |
41 | DSAerr(DSA_F_I2D_DSA_SIG, ERR_R_MALLOC_FAILURE); | |
42 | return(0); | |
43 | } | |
44 | rbs.type=V_ASN1_INTEGER; | |
45 | rbs.length=BN_bn2bin(v->r,rbs.data); | |
26a3a48d | 46 | sbs.data=OPENSSL_malloc(BN_num_bits(v->s)/8+1); |
a8da8918 UM |
47 | if (sbs.data == NULL) |
48 | { | |
26a3a48d | 49 | OPENSSL_free(rbs.data); |
a8da8918 UM |
50 | DSAerr(DSA_F_I2D_DSA_SIG, ERR_R_MALLOC_FAILURE); |
51 | return(0); | |
52 | } | |
53 | sbs.type=V_ASN1_INTEGER; | |
54 | sbs.length=BN_bn2bin(v->s,sbs.data); | |
55 | ||
56 | len=i2d_ASN1_INTEGER(&rbs,NULL); | |
57 | len+=i2d_ASN1_INTEGER(&sbs,NULL); | |
58 | ||
59 | if (pp) | |
60 | { | |
61 | p=*pp; | |
62 | ASN1_put_object(&p,1,len,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); | |
63 | i2d_ASN1_INTEGER(&rbs,&p); | |
64 | i2d_ASN1_INTEGER(&sbs,&p); | |
65 | } | |
66 | t=ASN1_object_size(1,len,V_ASN1_SEQUENCE); | |
26a3a48d RL |
67 | OPENSSL_free(rbs.data); |
68 | OPENSSL_free(sbs.data); | |
a8da8918 UM |
69 | return(t); |
70 | } | |
71 | ||
a4aba800 | 72 | DSA_SIG *d2i_DSA_SIG(DSA_SIG **a, const unsigned char **pp, long length) |
a8da8918 UM |
73 | { |
74 | int i=ERR_R_NESTED_ASN1_ERROR; | |
75 | ASN1_INTEGER *bs=NULL; | |
76 | M_ASN1_D2I_vars(a,DSA_SIG *,DSA_SIG_new); | |
77 | ||
78 | M_ASN1_D2I_Init(); | |
79 | M_ASN1_D2I_start_sequence(); | |
80 | M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); | |
81 | if ((ret->r=BN_bin2bn(bs->data,bs->length,ret->r)) == NULL) | |
82 | goto err_bn; | |
83 | M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); | |
84 | if ((ret->s=BN_bin2bn(bs->data,bs->length,ret->s)) == NULL) | |
85 | goto err_bn; | |
08e9c1af | 86 | M_ASN1_BIT_STRING_free(bs); |
a8da8918 UM |
87 | M_ASN1_D2I_Finish_2(a); |
88 | ||
89 | err_bn: | |
90 | i=ERR_R_BN_LIB; | |
91 | err: | |
92 | DSAerr(DSA_F_D2I_DSA_SIG,i); | |
93 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_SIG_free(ret); | |
08e9c1af | 94 | if (bs != NULL) M_ASN1_BIT_STRING_free(bs); |
a8da8918 UM |
95 | return(NULL); |
96 | } |