dsa_asn1.c 3.7 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
D
 
Dr. Stephen Henson 已提交
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
D
 
Dr. Stephen Henson 已提交
8
 */
9 10

#include <stdio.h>
11
#include "internal/cryptlib.h"
M
Matt Caswell 已提交
12
#include "dsa_locl.h"
13
#include <openssl/asn1.h>
D
 
Dr. Stephen Henson 已提交
14
#include <openssl/asn1t.h>
15
#include <openssl/rand.h>
16 17 18 19 20

struct DSA_SIG_st {
    BIGNUM *r;
    BIGNUM *s;
};
21

D
Dr. Stephen Henson 已提交
22
ASN1_SEQUENCE(DSA_SIG) = {
23 24
        ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
        ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
D
Dr. Stephen Henson 已提交
25
} static_ASN1_SEQUENCE_END(DSA_SIG)
D
 
Dr. Stephen Henson 已提交
26

27
IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
D
 
Dr. Stephen Henson 已提交
28

D
Dr. Stephen Henson 已提交
29
void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, const DSA_SIG *sig)
D
Dr. Stephen Henson 已提交
30 31 32 33 34
{
    *pr = sig->r;
    *ps = sig->s;
}

35
int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
36
{
37 38
    if (r == NULL || s == NULL)
        return 0;
39 40 41 42 43 44 45
    BN_clear_free(sig->r);
    BN_clear_free(sig->s);
    sig->r = r;
    sig->s = s;
    return 1;
}

D
 
Dr. Stephen Henson 已提交
46
/* Override the default free and new methods */
47
static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48
                  void *exarg)
49
{
50 51
    if (operation == ASN1_OP_NEW_PRE) {
        *pval = (ASN1_VALUE *)DSA_new();
52
        if (*pval != NULL)
53 54 55 56 57 58 59 60
            return 2;
        return 0;
    } else if (operation == ASN1_OP_FREE_PRE) {
        DSA_free((DSA *)*pval);
        *pval = NULL;
        return 2;
    }
    return 1;
61 62
}

D
 
Dr. Stephen Henson 已提交
63
ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
64 65 66 67 68
        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 已提交
69
        ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
70
} static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
71

D
 
Dr. Stephen Henson 已提交
72
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
73

D
 
Dr. Stephen Henson 已提交
74
ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
75 76 77
        ASN1_SIMPLE(DSA, p, BIGNUM),
        ASN1_SIMPLE(DSA, q, BIGNUM),
        ASN1_SIMPLE(DSA, g, BIGNUM),
78
} static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
79

D
 
Dr. Stephen Henson 已提交
80
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
81

82
ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
83 84 85 86
        ASN1_SIMPLE(DSA, pub_key, BIGNUM),
        ASN1_SIMPLE(DSA, p, BIGNUM),
        ASN1_SIMPLE(DSA, q, BIGNUM),
        ASN1_SIMPLE(DSA, g, BIGNUM)
87
} static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
D
 
Dr. Stephen Henson 已提交
88 89

IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
D
Dr. Stephen Henson 已提交
90 91

DSA *DSAparams_dup(DSA *dsa)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
    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);
}
110 111

/* data has already been hashed (probably with SHA or SHA-1). */
112 113
/*-
 * returns
114 115 116 117 118
 *      1: correct signature
 *      0: incorrect signature
 *     -1: error
 */
int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
               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 已提交
138
    OPENSSL_clear_free(der, derlen);
139 140 141
    DSA_SIG_free(s);
    return (ret);
}