1109998Smarkm/* dsa_asn1.c */
2280297Sjkim/*
3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4280297Sjkim * 2000.
5109998Smarkm */
6109998Smarkm/* ====================================================================
7238405Sjkim * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
8109998Smarkm *
9109998Smarkm * Redistribution and use in source and binary forms, with or without
10109998Smarkm * modification, are permitted provided that the following conditions
11109998Smarkm * are met:
12109998Smarkm *
13109998Smarkm * 1. Redistributions of source code must retain the above copyright
14280297Sjkim *    notice, this list of conditions and the following disclaimer.
15109998Smarkm *
16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
17109998Smarkm *    notice, this list of conditions and the following disclaimer in
18109998Smarkm *    the documentation and/or other materials provided with the
19109998Smarkm *    distribution.
20109998Smarkm *
21109998Smarkm * 3. All advertising materials mentioning features or use of this
22109998Smarkm *    software must display the following acknowledgment:
23109998Smarkm *    "This product includes software developed by the OpenSSL Project
24109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25109998Smarkm *
26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27109998Smarkm *    endorse or promote products derived from this software without
28109998Smarkm *    prior written permission. For written permission, please contact
29109998Smarkm *    licensing@OpenSSL.org.
30109998Smarkm *
31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
32109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
33109998Smarkm *    permission of the OpenSSL Project.
34109998Smarkm *
35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
36109998Smarkm *    acknowledgment:
37109998Smarkm *    "This product includes software developed by the OpenSSL Project
38109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39109998Smarkm *
40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
52109998Smarkm * ====================================================================
53109998Smarkm *
54109998Smarkm * This product includes cryptographic software written by Eric Young
55109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
56109998Smarkm * Hudson (tjh@cryptsoft.com).
57109998Smarkm *
58109998Smarkm */
5955714Skris
6055714Skris#include <stdio.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/dsa.h>
6355714Skris#include <openssl/asn1.h>
64109998Smarkm#include <openssl/asn1t.h>
65205128Ssimon#include <openssl/rand.h>
6655714Skris
67109998Smarkm/* Override the default new methods */
68238405Sjkimstatic int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
69280297Sjkim                  void *exarg)
7055714Skris{
71280297Sjkim    if (operation == ASN1_OP_NEW_PRE) {
72280297Sjkim        DSA_SIG *sig;
73280297Sjkim        sig = OPENSSL_malloc(sizeof(DSA_SIG));
74280297Sjkim        if (!sig) {
75280297Sjkim            DSAerr(DSA_F_SIG_CB, ERR_R_MALLOC_FAILURE);
76280297Sjkim            return 0;
77280297Sjkim        }
78280297Sjkim        sig->r = NULL;
79280297Sjkim        sig->s = NULL;
80280297Sjkim        *pval = (ASN1_VALUE *)sig;
81280297Sjkim        return 2;
82280297Sjkim    }
83280297Sjkim    return 1;
8455714Skris}
8555714Skris
86109998SmarkmASN1_SEQUENCE_cb(DSA_SIG, sig_cb) = {
87280297Sjkim        ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
88280297Sjkim        ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
89109998Smarkm} ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG)
90109998Smarkm
91238405SjkimIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA_SIG, DSA_SIG, DSA_SIG)
92109998Smarkm
93109998Smarkm/* Override the default free and new methods */
94238405Sjkimstatic int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
95280297Sjkim                  void *exarg)
9655714Skris{
97280297Sjkim    if (operation == ASN1_OP_NEW_PRE) {
98280297Sjkim        *pval = (ASN1_VALUE *)DSA_new();
99280297Sjkim        if (*pval)
100280297Sjkim            return 2;
101280297Sjkim        return 0;
102280297Sjkim    } else if (operation == ASN1_OP_FREE_PRE) {
103280297Sjkim        DSA_free((DSA *)*pval);
104280297Sjkim        *pval = NULL;
105280297Sjkim        return 2;
106280297Sjkim    }
107280297Sjkim    return 1;
10855714Skris}
10955714Skris
110109998SmarkmASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
111280297Sjkim        ASN1_SIMPLE(DSA, version, LONG),
112280297Sjkim        ASN1_SIMPLE(DSA, p, BIGNUM),
113280297Sjkim        ASN1_SIMPLE(DSA, q, BIGNUM),
114280297Sjkim        ASN1_SIMPLE(DSA, g, BIGNUM),
115280297Sjkim        ASN1_SIMPLE(DSA, pub_key, BIGNUM),
116280297Sjkim        ASN1_SIMPLE(DSA, priv_key, BIGNUM)
117109998Smarkm} ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
11855714Skris
119109998SmarkmIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
12055714Skris
121109998SmarkmASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
122280297Sjkim        ASN1_SIMPLE(DSA, p, BIGNUM),
123280297Sjkim        ASN1_SIMPLE(DSA, q, BIGNUM),
124280297Sjkim        ASN1_SIMPLE(DSA, g, BIGNUM),
125109998Smarkm} ASN1_SEQUENCE_END_cb(DSA, DSAparams)
12655714Skris
127109998SmarkmIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
12855714Skris
129280297Sjkim/*
130280297Sjkim * DSA public key is a bit trickier... its effectively a CHOICE type decided
131280297Sjkim * by a field called write_params which can either write out just the public
132280297Sjkim * key as an INTEGER or the parameters and public key in a SEQUENCE
133109998Smarkm */
13455714Skris
135109998SmarkmASN1_SEQUENCE(dsa_pub_internal) = {
136280297Sjkim        ASN1_SIMPLE(DSA, pub_key, BIGNUM),
137280297Sjkim        ASN1_SIMPLE(DSA, p, BIGNUM),
138280297Sjkim        ASN1_SIMPLE(DSA, q, BIGNUM),
139280297Sjkim        ASN1_SIMPLE(DSA, g, BIGNUM)
140109998Smarkm} ASN1_SEQUENCE_END_name(DSA, dsa_pub_internal)
14155714Skris
142109998SmarkmASN1_CHOICE_cb(DSAPublicKey, dsa_cb) = {
143280297Sjkim        ASN1_SIMPLE(DSA, pub_key, BIGNUM),
144280297Sjkim        ASN1_EX_COMBINE(0, 0, dsa_pub_internal)
145109998Smarkm} ASN1_CHOICE_END_cb(DSA, DSAPublicKey, write_params)
146109998Smarkm
147109998SmarkmIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
148194206Ssimon
149238405SjkimDSA *DSAparams_dup(DSA *dsa)
150280297Sjkim{
151280297Sjkim    return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
152280297Sjkim}
153238405Sjkim
154280297Sjkimint DSA_sign(int type, const unsigned char *dgst, int dlen,
155280297Sjkim             unsigned char *sig, unsigned int *siglen, DSA *dsa)
156280297Sjkim{
157280297Sjkim    DSA_SIG *s;
158280297Sjkim    RAND_seed(dgst, dlen);
159280297Sjkim    s = DSA_do_sign(dgst, dlen, dsa);
160280297Sjkim    if (s == NULL) {
161280297Sjkim        *siglen = 0;
162280297Sjkim        return (0);
163280297Sjkim    }
164280297Sjkim    *siglen = i2d_DSA_SIG(s, &sig);
165280297Sjkim    DSA_SIG_free(s);
166280297Sjkim    return (1);
167280297Sjkim}
168194206Ssimon
169194206Ssimon/* data has already been hashed (probably with SHA or SHA-1). */
170280297Sjkim/*-
171280297Sjkim * returns
172194206Ssimon *      1: correct signature
173194206Ssimon *      0: incorrect signature
174194206Ssimon *     -1: error
175194206Ssimon */
176194206Ssimonint DSA_verify(int type, const unsigned char *dgst, int dgst_len,
177280297Sjkim               const unsigned char *sigbuf, int siglen, DSA *dsa)
178280297Sjkim{
179280297Sjkim    DSA_SIG *s;
180280297Sjkim    const unsigned char *p = sigbuf;
181280297Sjkim    unsigned char *der = NULL;
182280297Sjkim    int derlen = -1;
183280297Sjkim    int ret = -1;
184194206Ssimon
185280297Sjkim    s = DSA_SIG_new();
186280297Sjkim    if (s == NULL)
187280297Sjkim        return (ret);
188280297Sjkim    if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
189280297Sjkim        goto err;
190280297Sjkim    /* Ensure signature uses DER and doesn't have trailing garbage */
191280297Sjkim    derlen = i2d_DSA_SIG(s, &der);
192280297Sjkim    if (derlen != siglen || memcmp(sigbuf, der, derlen))
193280297Sjkim        goto err;
194280297Sjkim    ret = DSA_do_verify(dgst, dgst_len, s, dsa);
195280297Sjkim err:
196280297Sjkim    if (derlen > 0) {
197280297Sjkim        OPENSSL_cleanse(der, derlen);
198280297Sjkim        OPENSSL_free(der);
199280297Sjkim    }
200280297Sjkim    DSA_SIG_free(s);
201280297Sjkim    return (ret);
202280297Sjkim}
203