dsa_asn1.c 5.8 KB
Newer Older
1 2 3
/*
 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
 * 2000.
D
 
Dr. Stephen Henson 已提交
4 5
 */
/* ====================================================================
6
 * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
D
 
Dr. Stephen Henson 已提交
7 8 9 10 11 12
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
D
 
Dr. Stephen Henson 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */
58 59

#include <stdio.h>
60
#include "internal/cryptlib.h"
61 62
#include <openssl/dsa.h>
#include <openssl/asn1.h>
D
 
Dr. Stephen Henson 已提交
63
#include <openssl/asn1t.h>
64
#include <openssl/rand.h>
D
Dr. Stephen Henson 已提交
65
#include "dsa_locl.h"
66

D
Dr. Stephen Henson 已提交
67
ASN1_SEQUENCE(DSA_SIG) = {
68 69
        ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
        ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
D
Dr. Stephen Henson 已提交
70
} static_ASN1_SEQUENCE_END(DSA_SIG)
D
 
Dr. Stephen Henson 已提交
71

72
IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
D
 
Dr. Stephen Henson 已提交
73

D
Dr. Stephen Henson 已提交
74 75 76 77 78 79
void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, DSA_SIG *sig)
{
    *pr = sig->r;
    *ps = sig->s;
}

D
 
Dr. Stephen Henson 已提交
80
/* Override the default free and new methods */
81
static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
82
                  void *exarg)
83
{
84 85
    if (operation == ASN1_OP_NEW_PRE) {
        *pval = (ASN1_VALUE *)DSA_new();
86
        if (*pval != NULL)
87 88 89 90 91 92 93 94
            return 2;
        return 0;
    } else if (operation == ASN1_OP_FREE_PRE) {
        DSA_free((DSA *)*pval);
        *pval = NULL;
        return 2;
    }
    return 1;
95 96
}

D
 
Dr. Stephen Henson 已提交
97
ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
98 99 100 101 102
        ASN1_SIMPLE(DSA, version, LONG),
        ASN1_SIMPLE(DSA, p, BIGNUM),
        ASN1_SIMPLE(DSA, q, BIGNUM),
        ASN1_SIMPLE(DSA, g, BIGNUM),
        ASN1_SIMPLE(DSA, pub_key, BIGNUM),
R
Rich Salz 已提交
103
        ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
104
} static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
105

D
 
Dr. Stephen Henson 已提交
106
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
107

D
 
Dr. Stephen Henson 已提交
108
ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
109 110 111
        ASN1_SIMPLE(DSA, p, BIGNUM),
        ASN1_SIMPLE(DSA, q, BIGNUM),
        ASN1_SIMPLE(DSA, g, BIGNUM),
112
} static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
113

D
 
Dr. Stephen Henson 已提交
114
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
115

116
ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
117 118 119 120
        ASN1_SIMPLE(DSA, pub_key, BIGNUM),
        ASN1_SIMPLE(DSA, p, BIGNUM),
        ASN1_SIMPLE(DSA, q, BIGNUM),
        ASN1_SIMPLE(DSA, g, BIGNUM)
121
} static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
D
 
Dr. Stephen Henson 已提交
122 123

IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
D
Dr. Stephen Henson 已提交
124 125

DSA *DSAparams_dup(DSA *dsa)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
    return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
}

int DSA_sign(int type, const unsigned char *dgst, int dlen,
             unsigned char *sig, unsigned int *siglen, DSA *dsa)
{
    DSA_SIG *s;
    RAND_seed(dgst, dlen);
    s = DSA_do_sign(dgst, dlen, dsa);
    if (s == NULL) {
        *siglen = 0;
        return (0);
    }
    *siglen = i2d_DSA_SIG(s, &sig);
    DSA_SIG_free(s);
    return (1);
}
144 145

/* data has already been hashed (probably with SHA or SHA-1). */
146 147
/*-
 * returns
148 149 150 151 152
 *      1: correct signature
 *      0: incorrect signature
 *     -1: error
 */
int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
               const unsigned char *sigbuf, int siglen, DSA *dsa)
{
    DSA_SIG *s;
    const unsigned char *p = sigbuf;
    unsigned char *der = NULL;
    int derlen = -1;
    int ret = -1;

    s = DSA_SIG_new();
    if (s == NULL)
        return (ret);
    if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
        goto err;
    /* Ensure signature uses DER and doesn't have trailing garbage */
    derlen = i2d_DSA_SIG(s, &der);
    if (derlen != siglen || memcmp(sigbuf, der, derlen))
        goto err;
    ret = DSA_do_verify(dgst, dgst_len, s, dsa);
 err:
R
Rich Salz 已提交
172
    OPENSSL_clear_free(der, derlen);
173 174 175
    DSA_SIG_free(s);
    return (ret);
}