]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_asn1.c
540d01fdce3b6983a26b057bad1f26288e9ed1c4
[thirdparty/openssl.git] / crypto / dsa / dsa_asn1.c
1 /*
2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
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
8 */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "dsa_locl.h"
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/rand.h>
16
17 ASN1_SEQUENCE(DSA_SIG) = {
18 ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
19 ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
20 } static_ASN1_SEQUENCE_END(DSA_SIG)
21
22 IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
23
24 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
25 {
26 if (pr != NULL)
27 *pr = sig->r;
28 if (ps != NULL)
29 *ps = sig->s;
30 }
31
32 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
33 {
34 if (r == NULL || s == NULL)
35 return 0;
36 BN_clear_free(sig->r);
37 BN_clear_free(sig->s);
38 sig->r = r;
39 sig->s = s;
40 return 1;
41 }
42
43 /* Override the default free and new methods */
44 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
45 void *exarg)
46 {
47 if (operation == ASN1_OP_NEW_PRE) {
48 *pval = (ASN1_VALUE *)DSA_new();
49 if (*pval != NULL)
50 return 2;
51 return 0;
52 } else if (operation == ASN1_OP_FREE_PRE) {
53 DSA_free((DSA *)*pval);
54 *pval = NULL;
55 return 2;
56 }
57 return 1;
58 }
59
60 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
61 ASN1_SIMPLE(DSA, version, LONG),
62 ASN1_SIMPLE(DSA, p, BIGNUM),
63 ASN1_SIMPLE(DSA, q, BIGNUM),
64 ASN1_SIMPLE(DSA, g, BIGNUM),
65 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
66 ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
67 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
68
69 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
70
71 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
72 ASN1_SIMPLE(DSA, p, BIGNUM),
73 ASN1_SIMPLE(DSA, q, BIGNUM),
74 ASN1_SIMPLE(DSA, g, BIGNUM),
75 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
76
77 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
78
79 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
80 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
81 ASN1_SIMPLE(DSA, p, BIGNUM),
82 ASN1_SIMPLE(DSA, q, BIGNUM),
83 ASN1_SIMPLE(DSA, g, BIGNUM)
84 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
85
86 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
87
88 DSA *DSAparams_dup(DSA *dsa)
89 {
90 return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
91 }
92
93 int DSA_sign(int type, const unsigned char *dgst, int dlen,
94 unsigned char *sig, unsigned int *siglen, DSA *dsa)
95 {
96 DSA_SIG *s;
97 RAND_seed(dgst, dlen);
98 s = DSA_do_sign(dgst, dlen, dsa);
99 if (s == NULL) {
100 *siglen = 0;
101 return (0);
102 }
103 *siglen = i2d_DSA_SIG(s, &sig);
104 DSA_SIG_free(s);
105 return (1);
106 }
107
108 /* data has already been hashed (probably with SHA or SHA-1). */
109 /*-
110 * returns
111 * 1: correct signature
112 * 0: incorrect signature
113 * -1: error
114 */
115 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
116 const unsigned char *sigbuf, int siglen, DSA *dsa)
117 {
118 DSA_SIG *s;
119 const unsigned char *p = sigbuf;
120 unsigned char *der = NULL;
121 int derlen = -1;
122 int ret = -1;
123
124 s = DSA_SIG_new();
125 if (s == NULL)
126 return (ret);
127 if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
128 goto err;
129 /* Ensure signature uses DER and doesn't have trailing garbage */
130 derlen = i2d_DSA_SIG(s, &der);
131 if (derlen != siglen || memcmp(sigbuf, der, derlen))
132 goto err;
133 ret = DSA_do_verify(dgst, dgst_len, s, dsa);
134 err:
135 OPENSSL_clear_free(der, derlen);
136 DSA_SIG_free(s);
137 return (ret);
138 }