dsa_asn1.c revision 109998
1219019Sgabor/* dsa_asn1.c */
2219019Sgabor/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3219019Sgabor * project 2000.
4219019Sgabor */
5219019Sgabor/* ====================================================================
6219019Sgabor * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor *
12219019Sgabor * 1. Redistributions of source code must retain the above copyright
13219019Sgabor *    notice, this list of conditions and the following disclaimer.
14219019Sgabor *
15219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
16219019Sgabor *    notice, this list of conditions and the following disclaimer in
17219019Sgabor *    the documentation and/or other materials provided with the
18219019Sgabor *    distribution.
19219019Sgabor *
20219019Sgabor * 3. All advertising materials mentioning features or use of this
21219019Sgabor *    software must display the following acknowledgment:
22219019Sgabor *    "This product includes software developed by the OpenSSL Project
23219019Sgabor *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24219019Sgabor *
25219019Sgabor * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26219019Sgabor *    endorse or promote products derived from this software without
27219019Sgabor *    prior written permission. For written permission, please contact
28219019Sgabor *    licensing@OpenSSL.org.
29219019Sgabor *
30219019Sgabor * 5. Products derived from this software may not be called "OpenSSL"
31219019Sgabor *    nor may "OpenSSL" appear in their names without prior written
32219019Sgabor *    permission of the OpenSSL Project.
33219019Sgabor *
34219019Sgabor * 6. Redistributions of any form whatsoever must retain the following
35219019Sgabor *    acknowledgment:
36219019Sgabor *    "This product includes software developed by the OpenSSL Project
37219019Sgabor *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38219019Sgabor *
39219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40219019Sgabor * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42219019Sgabor * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43219019Sgabor * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44219019Sgabor * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45219019Sgabor * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46219019Sgabor * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48219019Sgabor * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49219019Sgabor * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50219019Sgabor * OF THE POSSIBILITY OF SUCH DAMAGE.
51219019Sgabor * ====================================================================
52219019Sgabor *
53219019Sgabor * This product includes cryptographic software written by Eric Young
54219019Sgabor * (eay@cryptsoft.com).  This product includes software written by Tim
55219019Sgabor * Hudson (tjh@cryptsoft.com).
56219019Sgabor *
57219019Sgabor */
58219019Sgabor
59219019Sgabor#include <stdio.h>
60219019Sgabor#include "cryptlib.h"
61219019Sgabor#include <openssl/dsa.h>
62219019Sgabor#include <openssl/asn1.h>
63219019Sgabor#include <openssl/asn1t.h>
64219019Sgabor
65219019Sgabor/* Override the default new methods */
66219019Sgaborstatic int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
67219019Sgabor{
68219019Sgabor	if(operation == ASN1_OP_NEW_PRE) {
69219019Sgabor		DSA_SIG *sig;
70219019Sgabor		sig = OPENSSL_malloc(sizeof(DSA_SIG));
71219019Sgabor		sig->r = NULL;
72219019Sgabor		sig->s = NULL;
73219019Sgabor		*pval = (ASN1_VALUE *)sig;
74219019Sgabor		if(sig) return 2;
75219019Sgabor		DSAerr(DSA_F_SIG_CB, ERR_R_MALLOC_FAILURE);
76219019Sgabor		return 0;
77219019Sgabor	}
78219019Sgabor	return 1;
79219019Sgabor}
80219019Sgabor
81219019SgaborASN1_SEQUENCE_cb(DSA_SIG, sig_cb) = {
82219019Sgabor	ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
83219019Sgabor	ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
84219019Sgabor} ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG)
85219019Sgabor
86219019SgaborIMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
87219019Sgabor
88219019Sgabor/* Override the default free and new methods */
89219019Sgaborstatic int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
90219019Sgabor{
91219019Sgabor	if(operation == ASN1_OP_NEW_PRE) {
92219019Sgabor		*pval = (ASN1_VALUE *)DSA_new();
93219019Sgabor		if(*pval) return 2;
94219019Sgabor		return 0;
95219019Sgabor	} else if(operation == ASN1_OP_FREE_PRE) {
96219019Sgabor		DSA_free((DSA *)*pval);
97219019Sgabor		*pval = NULL;
98219019Sgabor		return 2;
99219019Sgabor	}
100219019Sgabor	return 1;
101219019Sgabor}
102219019Sgabor
103219019SgaborASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
104219019Sgabor	ASN1_SIMPLE(DSA, version, LONG),
105219019Sgabor	ASN1_SIMPLE(DSA, p, BIGNUM),
106219019Sgabor	ASN1_SIMPLE(DSA, q, BIGNUM),
107219019Sgabor	ASN1_SIMPLE(DSA, g, BIGNUM),
108219019Sgabor	ASN1_SIMPLE(DSA, pub_key, BIGNUM),
109219019Sgabor	ASN1_SIMPLE(DSA, priv_key, BIGNUM)
110219019Sgabor} ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
111219019Sgabor
112219019SgaborIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
113219019Sgabor
114219019SgaborASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
115219019Sgabor	ASN1_SIMPLE(DSA, p, BIGNUM),
116219019Sgabor	ASN1_SIMPLE(DSA, q, BIGNUM),
117219019Sgabor	ASN1_SIMPLE(DSA, g, BIGNUM),
118219019Sgabor} ASN1_SEQUENCE_END_cb(DSA, DSAparams)
119219019Sgabor
120219019SgaborIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
121219019Sgabor
122219019Sgabor/* DSA public key is a bit trickier... its effectively a CHOICE type
123219019Sgabor * decided by a field called write_params which can either write out
124219019Sgabor * just the public key as an INTEGER or the parameters and public key
125219019Sgabor * in a SEQUENCE
126219019Sgabor */
127219019Sgabor
128219019SgaborASN1_SEQUENCE(dsa_pub_internal) = {
129219019Sgabor	ASN1_SIMPLE(DSA, pub_key, BIGNUM),
130219019Sgabor	ASN1_SIMPLE(DSA, p, BIGNUM),
131219019Sgabor	ASN1_SIMPLE(DSA, q, BIGNUM),
132219019Sgabor	ASN1_SIMPLE(DSA, g, BIGNUM)
133219019Sgabor} ASN1_SEQUENCE_END_name(DSA, dsa_pub_internal)
134219019Sgabor
135219019SgaborASN1_CHOICE_cb(DSAPublicKey, dsa_cb) = {
136219019Sgabor	ASN1_SIMPLE(DSA, pub_key, BIGNUM),
137219019Sgabor	ASN1_EX_COMBINE(0, 0, dsa_pub_internal)
138219019Sgabor} ASN1_CHOICE_END_cb(DSA, DSAPublicKey, write_params)
139219019Sgabor
140219019SgaborIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
141219019Sgabor