]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
d2e9e320 | 2 | * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved. |
9d6b1ce6 | 3 | * |
3cdbea65 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
d2e9e320 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
9d6b1ce6 | 8 | */ |
a8da8918 UM |
9 | |
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
706457b7 | 12 | #include "dsa_local.h" |
ec577822 | 13 | #include <openssl/asn1.h> |
9d6b1ce6 | 14 | #include <openssl/asn1t.h> |
f7a2afa6 | 15 | #include <openssl/rand.h> |
25f2138b | 16 | #include "crypto/asn1_dsa.h" |
8cc44d97 DSH |
17 | |
18 | DSA_SIG *DSA_SIG_new(void) | |
19 | { | |
20 | DSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); | |
21 | if (sig == NULL) | |
22 | DSAerr(DSA_F_DSA_SIG_NEW, ERR_R_MALLOC_FAILURE); | |
23 | return sig; | |
24 | } | |
25 | ||
26 | void DSA_SIG_free(DSA_SIG *sig) | |
27 | { | |
28 | if (sig == NULL) | |
29 | return; | |
30 | BN_clear_free(sig->r); | |
31 | BN_clear_free(sig->s); | |
32 | OPENSSL_free(sig); | |
33 | } | |
9d6b1ce6 | 34 | |
54846b7c DM |
35 | DSA_SIG *d2i_DSA_SIG(DSA_SIG **psig, const unsigned char **ppin, long len) |
36 | { | |
37 | DSA_SIG *sig; | |
38 | ||
39 | if (len < 0) | |
40 | return NULL; | |
41 | if (psig != NULL && *psig != NULL) { | |
42 | sig = *psig; | |
43 | } else { | |
44 | sig = DSA_SIG_new(); | |
45 | if (sig == NULL) | |
46 | return NULL; | |
47 | } | |
48 | if (sig->r == NULL) | |
49 | sig->r = BN_new(); | |
50 | if (sig->s == NULL) | |
51 | sig->s = BN_new(); | |
52 | if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) { | |
53 | if (psig == NULL || *psig == NULL) | |
54 | DSA_SIG_free(sig); | |
55 | return NULL; | |
56 | } | |
57 | if (psig != NULL && *psig == NULL) | |
58 | *psig = sig; | |
59 | return sig; | |
60 | } | |
61 | ||
62 | int i2d_DSA_SIG(const DSA_SIG *sig, unsigned char **ppout) | |
63 | { | |
b8805834 | 64 | BUF_MEM *buf = NULL; |
54846b7c | 65 | size_t encoded_len; |
b8805834 | 66 | WPACKET pkt; |
54846b7c | 67 | |
b8805834 MC |
68 | if (ppout == NULL) { |
69 | if (!WPACKET_init_null(&pkt, 0)) | |
54846b7c | 70 | return -1; |
b8805834 MC |
71 | } else if (*ppout == NULL) { |
72 | if ((buf = BUF_MEM_new()) == NULL | |
73 | || !WPACKET_init_len(&pkt, buf, 0)) { | |
74 | BUF_MEM_free(buf); | |
54846b7c | 75 | return -1; |
b8805834 | 76 | } |
54846b7c | 77 | } else { |
b8805834 MC |
78 | if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0)) |
79 | return -1; | |
54846b7c | 80 | } |
b8805834 MC |
81 | |
82 | if (!encode_der_dsa_sig(&pkt, sig->r, sig->s) | |
83 | || !WPACKET_get_total_written(&pkt, &encoded_len) | |
84 | || !WPACKET_finish(&pkt)) { | |
85 | BUF_MEM_free(buf); | |
86 | WPACKET_cleanup(&pkt); | |
54846b7c DM |
87 | return -1; |
88 | } | |
b8805834 MC |
89 | |
90 | if (ppout != NULL) { | |
91 | if (*ppout == NULL) { | |
92 | *ppout = (unsigned char *)buf->data; | |
93 | buf->data = NULL; | |
94 | BUF_MEM_free(buf); | |
95 | } else { | |
96 | *ppout += encoded_len; | |
97 | } | |
98 | } | |
99 | ||
54846b7c DM |
100 | return (int)encoded_len; |
101 | } | |
102 | ||
9267c11b | 103 | void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
706a13f1 | 104 | { |
9267c11b EK |
105 | if (pr != NULL) |
106 | *pr = sig->r; | |
107 | if (ps != NULL) | |
108 | *ps = sig->s; | |
706a13f1 DSH |
109 | } |
110 | ||
7ca3ea22 | 111 | int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
6a571a18 | 112 | { |
bbaa9dd8 TS |
113 | if (r == NULL || s == NULL) |
114 | return 0; | |
6a571a18 TS |
115 | BN_clear_free(sig->r); |
116 | BN_clear_free(sig->s); | |
117 | sig->r = r; | |
118 | sig->s = s; | |
119 | return 1; | |
120 | } | |
121 | ||
9d6b1ce6 | 122 | /* Override the default free and new methods */ |
24484759 | 123 | static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
0f113f3e | 124 | void *exarg) |
a8da8918 | 125 | { |
0f113f3e MC |
126 | if (operation == ASN1_OP_NEW_PRE) { |
127 | *pval = (ASN1_VALUE *)DSA_new(); | |
90945fa3 | 128 | if (*pval != NULL) |
0f113f3e MC |
129 | return 2; |
130 | return 0; | |
131 | } else if (operation == ASN1_OP_FREE_PRE) { | |
132 | DSA_free((DSA *)*pval); | |
133 | *pval = NULL; | |
134 | return 2; | |
135 | } | |
136 | return 1; | |
a8da8918 UM |
137 | } |
138 | ||
9d6b1ce6 | 139 | ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = { |
9612e157 | 140 | ASN1_EMBED(DSA, version, INT32), |
0f113f3e MC |
141 | ASN1_SIMPLE(DSA, p, BIGNUM), |
142 | ASN1_SIMPLE(DSA, q, BIGNUM), | |
143 | ASN1_SIMPLE(DSA, g, BIGNUM), | |
144 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | |
74924dcb | 145 | ASN1_SIMPLE(DSA, priv_key, CBIGNUM) |
df2ee0e2 | 146 | } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey) |
a8da8918 | 147 | |
9fdcc21f | 148 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPrivateKey, DSAPrivateKey) |
a8da8918 | 149 | |
9d6b1ce6 | 150 | ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = { |
0f113f3e MC |
151 | ASN1_SIMPLE(DSA, p, BIGNUM), |
152 | ASN1_SIMPLE(DSA, q, BIGNUM), | |
153 | ASN1_SIMPLE(DSA, g, BIGNUM), | |
df2ee0e2 | 154 | } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams) |
a8da8918 | 155 | |
9fdcc21f | 156 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAparams, DSAparams) |
a8da8918 | 157 | |
987157f6 | 158 | ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = { |
0f113f3e MC |
159 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), |
160 | ASN1_SIMPLE(DSA, p, BIGNUM), | |
161 | ASN1_SIMPLE(DSA, q, BIGNUM), | |
162 | ASN1_SIMPLE(DSA, g, BIGNUM) | |
987157f6 | 163 | } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey) |
9d6b1ce6 | 164 | |
9fdcc21f | 165 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPublicKey, DSAPublicKey) |
f4274da1 | 166 | |
9fdcc21f | 167 | DSA *DSAparams_dup(const DSA *dsa) |
0f113f3e MC |
168 | { |
169 | return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa); | |
170 | } | |
171 | ||
172 | int DSA_sign(int type, const unsigned char *dgst, int dlen, | |
173 | unsigned char *sig, unsigned int *siglen, DSA *dsa) | |
174 | { | |
175 | DSA_SIG *s; | |
75e2c877 | 176 | |
0f113f3e MC |
177 | s = DSA_do_sign(dgst, dlen, dsa); |
178 | if (s == NULL) { | |
179 | *siglen = 0; | |
26a7d938 | 180 | return 0; |
0f113f3e MC |
181 | } |
182 | *siglen = i2d_DSA_SIG(s, &sig); | |
183 | DSA_SIG_free(s); | |
208fb891 | 184 | return 1; |
0f113f3e | 185 | } |
f7a2afa6 DSH |
186 | |
187 | /* data has already been hashed (probably with SHA or SHA-1). */ | |
1d97c843 TH |
188 | /*- |
189 | * returns | |
f7a2afa6 DSH |
190 | * 1: correct signature |
191 | * 0: incorrect signature | |
192 | * -1: error | |
193 | */ | |
194 | int DSA_verify(int type, const unsigned char *dgst, int dgst_len, | |
0f113f3e MC |
195 | const unsigned char *sigbuf, int siglen, DSA *dsa) |
196 | { | |
197 | DSA_SIG *s; | |
198 | const unsigned char *p = sigbuf; | |
199 | unsigned char *der = NULL; | |
200 | int derlen = -1; | |
201 | int ret = -1; | |
202 | ||
203 | s = DSA_SIG_new(); | |
204 | if (s == NULL) | |
26a7d938 | 205 | return ret; |
0f113f3e MC |
206 | if (d2i_DSA_SIG(&s, &p, siglen) == NULL) |
207 | goto err; | |
208 | /* Ensure signature uses DER and doesn't have trailing garbage */ | |
209 | derlen = i2d_DSA_SIG(s, &der); | |
210 | if (derlen != siglen || memcmp(sigbuf, der, derlen)) | |
211 | goto err; | |
212 | ret = DSA_do_verify(dgst, dgst_len, s, dsa); | |
213 | err: | |
4b45c6e5 | 214 | OPENSSL_clear_free(der, derlen); |
0f113f3e | 215 | DSA_SIG_free(s); |
26a7d938 | 216 | return ret; |
0f113f3e | 217 | } |