Modify DSA and DH keys to use a shared FFC_PARAMS struct
[oweals/openssl.git] / crypto / dsa / dsa_asn1.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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_local.h"
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/rand.h>
16 #include "crypto/asn1_dsa.h"
17
18 /* Override the default free and new methods */
19 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
20                   void *exarg)
21 {
22     if (operation == ASN1_OP_NEW_PRE) {
23         *pval = (ASN1_VALUE *)DSA_new();
24         if (*pval != NULL)
25             return 2;
26         return 0;
27     } else if (operation == ASN1_OP_FREE_PRE) {
28         DSA_free((DSA *)*pval);
29         *pval = NULL;
30         return 2;
31     }
32     return 1;
33 }
34
35 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
36         ASN1_EMBED(DSA, version, INT32),
37         ASN1_SIMPLE(DSA, params.p, BIGNUM),
38         ASN1_SIMPLE(DSA, params.q, BIGNUM),
39         ASN1_SIMPLE(DSA, params.g, BIGNUM),
40         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
41         ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
42 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
43
44 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPrivateKey, DSAPrivateKey)
45
46 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
47         ASN1_SIMPLE(DSA, params.p, BIGNUM),
48         ASN1_SIMPLE(DSA, params.q, BIGNUM),
49         ASN1_SIMPLE(DSA, params.g, BIGNUM),
50 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
51
52 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAparams, DSAparams)
53
54 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
55         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
56         ASN1_SIMPLE(DSA, params.p, BIGNUM),
57         ASN1_SIMPLE(DSA, params.q, BIGNUM),
58         ASN1_SIMPLE(DSA, params.g, BIGNUM)
59 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
60
61 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPublicKey, DSAPublicKey)
62
63 DSA *DSAparams_dup(const DSA *dsa)
64 {
65     return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
66 }