Fix: PEM_read_bio_PrivateKey with no-ui / no-stdio
[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 struct DSA_SIG_st {
18     BIGNUM *r;
19     BIGNUM *s;
20 };
21
22 ASN1_SEQUENCE(DSA_SIG) = {
23         ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
24         ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
25 } static_ASN1_SEQUENCE_END(DSA_SIG)
26
27 IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
28
29 void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, const DSA_SIG *sig)
30 {
31     *pr = sig->r;
32     *ps = sig->s;
33 }
34
35 /* Override the default free and new methods */
36 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
37                   void *exarg)
38 {
39     if (operation == ASN1_OP_NEW_PRE) {
40         *pval = (ASN1_VALUE *)DSA_new();
41         if (*pval != NULL)
42             return 2;
43         return 0;
44     } else if (operation == ASN1_OP_FREE_PRE) {
45         DSA_free((DSA *)*pval);
46         *pval = NULL;
47         return 2;
48     }
49     return 1;
50 }
51
52 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
53         ASN1_SIMPLE(DSA, version, LONG),
54         ASN1_SIMPLE(DSA, p, BIGNUM),
55         ASN1_SIMPLE(DSA, q, BIGNUM),
56         ASN1_SIMPLE(DSA, g, BIGNUM),
57         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
58         ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
59 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
60
61 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
62
63 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
64         ASN1_SIMPLE(DSA, p, BIGNUM),
65         ASN1_SIMPLE(DSA, q, BIGNUM),
66         ASN1_SIMPLE(DSA, g, BIGNUM),
67 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
68
69 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
70
71 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
72         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
73         ASN1_SIMPLE(DSA, p, BIGNUM),
74         ASN1_SIMPLE(DSA, q, BIGNUM),
75         ASN1_SIMPLE(DSA, g, BIGNUM)
76 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
77
78 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
79
80 DSA *DSAparams_dup(DSA *dsa)
81 {
82     return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
83 }
84
85 int DSA_sign(int type, const unsigned char *dgst, int dlen,
86              unsigned char *sig, unsigned int *siglen, DSA *dsa)
87 {
88     DSA_SIG *s;
89     RAND_seed(dgst, dlen);
90     s = DSA_do_sign(dgst, dlen, dsa);
91     if (s == NULL) {
92         *siglen = 0;
93         return (0);
94     }
95     *siglen = i2d_DSA_SIG(s, &sig);
96     DSA_SIG_free(s);
97     return (1);
98 }
99
100 /* data has already been hashed (probably with SHA or SHA-1). */
101 /*-
102  * returns
103  *      1: correct signature
104  *      0: incorrect signature
105  *     -1: error
106  */
107 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
108                const unsigned char *sigbuf, int siglen, DSA *dsa)
109 {
110     DSA_SIG *s;
111     const unsigned char *p = sigbuf;
112     unsigned char *der = NULL;
113     int derlen = -1;
114     int ret = -1;
115
116     s = DSA_SIG_new();
117     if (s == NULL)
118         return (ret);
119     if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
120         goto err;
121     /* Ensure signature uses DER and doesn't have trailing garbage */
122     derlen = i2d_DSA_SIG(s, &der);
123     if (derlen != siglen || memcmp(sigbuf, der, derlen))
124         goto err;
125     ret = DSA_do_verify(dgst, dgst_len, s, dsa);
126  err:
127     OPENSSL_clear_free(der, derlen);
128     DSA_SIG_free(s);
129     return (ret);
130 }