]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_asn1.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / dh / dh_asn1.c
CommitLineData
0f113f3e 1/*
aa6bb135 2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
e76f935e 3 *
e38873f5 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
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
e76f935e
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
9d6b1ce6 12#include <openssl/bn.h>
706457b7 13#include "dh_local.h"
9d6b1ce6
DSH
14#include <openssl/objects.h>
15#include <openssl/asn1t.h>
e76f935e 16
9d6b1ce6 17/* Override the default free and new methods */
24484759 18static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e 19 void *exarg)
e76f935e 20{
0f113f3e
MC
21 if (operation == ASN1_OP_NEW_PRE) {
22 *pval = (ASN1_VALUE *)DH_new();
90945fa3 23 if (*pval != NULL)
0f113f3e
MC
24 return 2;
25 return 0;
26 } else if (operation == ASN1_OP_FREE_PRE) {
27 DH_free((DH *)*pval);
28 *pval = NULL;
29 return 2;
8b84b075
RL
30 } else if (operation == ASN1_OP_D2I_POST) {
31 ((DH *)*pval)->dirty_cnt++;
0f113f3e
MC
32 }
33 return 1;
e76f935e
DSH
34}
35
9d6b1ce6 36ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
0f113f3e
MC
37 ASN1_SIMPLE(DH, p, BIGNUM),
38 ASN1_SIMPLE(DH, g, BIGNUM),
9612e157 39 ASN1_OPT_EMBED(DH, length, ZINT32),
599eccfc 40} ASN1_SEQUENCE_END_cb(DH, DHparams)
9d6b1ce6 41
9fdcc21f 42IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
f4274da1 43
0f113f3e
MC
44/*
45 * Internal only structures for handling X9.42 DH: this gets translated to or
46 * from a DH structure straight away.
afb14cda
DSH
47 */
48
0f113f3e
MC
49typedef struct {
50 ASN1_BIT_STRING *seed;
51 BIGNUM *counter;
52} int_dhvparams;
53
54typedef struct {
55 BIGNUM *p;
56 BIGNUM *q;
57 BIGNUM *g;
58 BIGNUM *j;
59 int_dhvparams *vparams;
60} int_dhx942_dh;
afb14cda
DSH
61
62ASN1_SEQUENCE(DHvparams) = {
0f113f3e
MC
63 ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
64 ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
df2ee0e2 65} static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
afb14cda
DSH
66
67ASN1_SEQUENCE(DHxparams) = {
0f113f3e
MC
68 ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
69 ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
70 ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
71 ASN1_OPT(int_dhx942_dh, j, BIGNUM),
72 ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
df2ee0e2 73} static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
afb14cda 74
0f113f3e
MC
75int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
76 const unsigned char **pp, long length);
77int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
afb14cda 78
9fdcc21f 79IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
afb14cda 80
0d4fb843 81/* Application public function: read in X9.42 DH parameters into DH structure */
afb14cda 82
0f113f3e
MC
83DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
84{
85 int_dhx942_dh *dhx = NULL;
86 DH *dh = NULL;
87 dh = DH_new();
90945fa3 88 if (dh == NULL)
0f113f3e
MC
89 return NULL;
90 dhx = d2i_int_dhx(NULL, pp, length);
90945fa3 91 if (dhx == NULL) {
0f113f3e
MC
92 DH_free(dh);
93 return NULL;
94 }
95
96 if (a) {
d6407083 97 DH_free(*a);
0f113f3e
MC
98 *a = dh;
99 }
100
101 dh->p = dhx->p;
102 dh->q = dhx->q;
103 dh->g = dhx->g;
104 dh->j = dhx->j;
105
106 if (dhx->vparams) {
107 dh->seed = dhx->vparams->seed->data;
108 dh->seedlen = dhx->vparams->seed->length;
109 dh->counter = dhx->vparams->counter;
110 dhx->vparams->seed->data = NULL;
111 ASN1_BIT_STRING_free(dhx->vparams->seed);
112 OPENSSL_free(dhx->vparams);
113 dhx->vparams = NULL;
114 }
115
116 OPENSSL_free(dhx);
117 return dh;
118}
119
120int i2d_DHxparams(const DH *dh, unsigned char **pp)
121{
122 int_dhx942_dh dhx;
123 int_dhvparams dhv;
124 ASN1_BIT_STRING bs;
125 dhx.p = dh->p;
126 dhx.g = dh->g;
127 dhx.q = dh->q;
128 dhx.j = dh->j;
129 if (dh->counter && dh->seed && dh->seedlen > 0) {
130 bs.flags = ASN1_STRING_FLAG_BITS_LEFT;
131 bs.data = dh->seed;
132 bs.length = dh->seedlen;
133 dhv.seed = &bs;
134 dhv.counter = dh->counter;
135 dhx.vparams = &dhv;
136 } else
137 dhx.vparams = NULL;
138
139 return i2d_int_dhx(&dhx, pp);
140}