1/*
2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19/* crypto/dsa/dsa_asn1.c */
20
21#include <stdio.h>
22#include "cryptlib.h"
23#include <openssl/dsa.h>
24#include <openssl/asn1.h>
25#ifndef	_OPENSSL_APPLE_CDSA_
26#include <openssl/asn1_mac.h>
27#endif
28
29DSA_SIG *DSA_SIG_new(void)
30{
31	DSA_SIG *ret;
32
33	ret = Malloc(sizeof(DSA_SIG));
34	if (ret == NULL)
35		{
36		DSAerr(DSA_F_DSA_SIG_NEW,ERR_R_MALLOC_FAILURE);
37		return(NULL);
38		}
39	ret->r = NULL;
40	ret->s = NULL;
41	return(ret);
42}
43
44void DSA_SIG_free(DSA_SIG *r)
45{
46	if (r == NULL) return;
47	if (r->r) BN_clear_free(r->r);
48	if (r->s) BN_clear_free(r->s);
49	Free(r);
50}
51
52#ifndef	_OPENSSL_APPLE_CDSA_
53
54int i2d_DSA_SIG(DSA_SIG *v, unsigned char **pp)
55{
56	int t=0,len;
57	ASN1_INTEGER rbs,sbs;
58	unsigned char *p;
59
60	rbs.data=Malloc(BN_num_bits(v->r)/8+1);
61	if (rbs.data == NULL)
62		{
63		DSAerr(DSA_F_I2D_DSA_SIG, ERR_R_MALLOC_FAILURE);
64		return(0);
65		}
66	rbs.type=V_ASN1_INTEGER;
67	rbs.length=BN_bn2bin(v->r,rbs.data);
68	sbs.data=Malloc(BN_num_bits(v->s)/8+1);
69	if (sbs.data == NULL)
70		{
71		Free(rbs.data);
72		DSAerr(DSA_F_I2D_DSA_SIG, ERR_R_MALLOC_FAILURE);
73		return(0);
74		}
75	sbs.type=V_ASN1_INTEGER;
76	sbs.length=BN_bn2bin(v->s,sbs.data);
77
78	len=i2d_ASN1_INTEGER(&rbs,NULL);
79	len+=i2d_ASN1_INTEGER(&sbs,NULL);
80
81	if (pp)
82		{
83		p=*pp;
84		ASN1_put_object(&p,1,len,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
85		i2d_ASN1_INTEGER(&rbs,&p);
86		i2d_ASN1_INTEGER(&sbs,&p);
87		}
88	t=ASN1_object_size(1,len,V_ASN1_SEQUENCE);
89	Free(rbs.data);
90	Free(sbs.data);
91	return(t);
92}
93
94DSA_SIG *d2i_DSA_SIG(DSA_SIG **a, unsigned char **pp, long length)
95{
96	int i=ERR_R_NESTED_ASN1_ERROR;
97	ASN1_INTEGER *bs=NULL;
98	M_ASN1_D2I_vars(a,DSA_SIG *,DSA_SIG_new);
99
100	M_ASN1_D2I_Init();
101	M_ASN1_D2I_start_sequence();
102	M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
103	if ((ret->r=BN_bin2bn(bs->data,bs->length,ret->r)) == NULL)
104		goto err_bn;
105	M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
106	if ((ret->s=BN_bin2bn(bs->data,bs->length,ret->s)) == NULL)
107		goto err_bn;
108	M_ASN1_BIT_STRING_free(bs);
109	M_ASN1_D2I_Finish_2(a);
110
111err_bn:
112	i=ERR_R_BN_LIB;
113err:
114	DSAerr(DSA_F_D2I_DSA_SIG,i);
115	if ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_SIG_free(ret);
116	if (bs != NULL) M_ASN1_BIT_STRING_free(bs);
117	return(NULL);
118}
119
120#endif	/* _OPENSSL_APPLE_CDSA_ */
121