xref: /netbsd-src/crypto/external/bsd/openssl/dist/crypto/dsa/dsa_asn1.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1635165faSspz /*
2*b0d17251Schristos  * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
6c7da899bSchristos  * in the file LICENSE in the source distribution or at
7c7da899bSchristos  * https://www.openssl.org/source/license.html
8a89c9211Schristos  */
9a89c9211Schristos 
10*b0d17251Schristos /*
11*b0d17251Schristos  * DSA low level APIs are deprecated for public use, but still ok for
12*b0d17251Schristos  * internal use.
13*b0d17251Schristos  */
14*b0d17251Schristos #include "internal/deprecated.h"
15*b0d17251Schristos 
16a89c9211Schristos #include <stdio.h>
17c7da899bSchristos #include "internal/cryptlib.h"
187d004720Schristos #include "dsa_local.h"
19a89c9211Schristos #include <openssl/asn1.h>
20a89c9211Schristos #include <openssl/asn1t.h>
214e3dcb23Sspz #include <openssl/rand.h>
22*b0d17251Schristos #include "crypto/asn1_dsa.h"
23c7da899bSchristos 
24a89c9211Schristos /* Override the default free and new methods */
dsa_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)25a89c9211Schristos static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
26a89c9211Schristos                   void *exarg)
27a89c9211Schristos {
28a89c9211Schristos     if (operation == ASN1_OP_NEW_PRE) {
29a89c9211Schristos         *pval = (ASN1_VALUE *)DSA_new();
30c7da899bSchristos         if (*pval != NULL)
31635165faSspz             return 2;
32a89c9211Schristos         return 0;
33a89c9211Schristos     } else if (operation == ASN1_OP_FREE_PRE) {
34a89c9211Schristos         DSA_free((DSA *)*pval);
35a89c9211Schristos         *pval = NULL;
36a89c9211Schristos         return 2;
37a89c9211Schristos     }
38a89c9211Schristos     return 1;
39a89c9211Schristos }
40a89c9211Schristos 
41a89c9211Schristos ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
4213d40330Schristos         ASN1_EMBED(DSA, version, INT32),
43*b0d17251Schristos         ASN1_SIMPLE(DSA, params.p, BIGNUM),
44*b0d17251Schristos         ASN1_SIMPLE(DSA, params.q, BIGNUM),
45*b0d17251Schristos         ASN1_SIMPLE(DSA, params.g, BIGNUM),
46a89c9211Schristos         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
47c7da899bSchristos         ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
48c7da899bSchristos } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
49a89c9211Schristos 
50*b0d17251Schristos IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPrivateKey, DSAPrivateKey)
51a89c9211Schristos 
52a89c9211Schristos ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
53*b0d17251Schristos         ASN1_SIMPLE(DSA, params.p, BIGNUM),
54*b0d17251Schristos         ASN1_SIMPLE(DSA, params.q, BIGNUM),
55*b0d17251Schristos         ASN1_SIMPLE(DSA, params.g, BIGNUM),
56c7da899bSchristos } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
57a89c9211Schristos 
58*b0d17251Schristos IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAparams, DSAparams)
59a89c9211Schristos 
60c7da899bSchristos ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
61a89c9211Schristos         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
62*b0d17251Schristos         ASN1_SIMPLE(DSA, params.p, BIGNUM),
63*b0d17251Schristos         ASN1_SIMPLE(DSA, params.q, BIGNUM),
64*b0d17251Schristos         ASN1_SIMPLE(DSA, params.g, BIGNUM)
65c7da899bSchristos } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
66a89c9211Schristos 
67*b0d17251Schristos IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPublicKey, DSAPublicKey)
68cef2ee70Schristos 
69*b0d17251Schristos DSA *DSAparams_dup(const DSA *dsa)
70cef2ee70Schristos {
71cef2ee70Schristos     return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
72cef2ee70Schristos }
73