Skip to content

Commit bf5aec4

Browse files
committed
port necessary dsa functions to openssl 1.0.2
1 parent c21f8af commit bf5aec4

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed

dsa.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ func GenerateDSAParameters(L, N int) (DSAParameters, error) {
8181
switch vMajor {
8282
case 1:
8383
dsa := getDSA(pkey)
84-
C.go_openssl_DSA_get0_pqg(dsa, &p, &q, &g)
84+
if vMinor == 0 {
85+
C.go_openssl_DSA_get0_pqg_backport(dsa, &p, &q, &g)
86+
} else {
87+
C.go_openssl_DSA_get0_pqg(dsa, &p, &q, &g)
88+
}
8589
case 3:
8690
defer func() {
8791
C.go_openssl_BN_free(p)
@@ -142,7 +146,11 @@ func GenerateKeyDSA(params DSAParameters) (*PrivateKeyDSA, error) {
142146
switch vMajor {
143147
case 1:
144148
dsa := getDSA(pkey)
145-
C.go_openssl_DSA_get0_key(dsa, &y, &x)
149+
if vMinor == 0 {
150+
C.go_openssl_DSA_get0_key_backport(dsa, &y, &x)
151+
} else {
152+
C.go_openssl_DSA_get0_key(dsa, &y, &x)
153+
}
146154
case 3:
147155
defer func() {
148156
C.go_openssl_BN_clear_free(x)
@@ -187,7 +195,13 @@ func newDSA1(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
187195
}
188196
dsa := C.go_openssl_DSA_new()
189197
p, q, g := bigToBN(params.P), bigToBN(params.Q), bigToBN(params.G)
190-
if C.go_openssl_DSA_set0_pqg(dsa, p, q, g) != 1 {
198+
var ret C.int
199+
if vMinor == 0 {
200+
ret = C.go_openssl_DSA_set0_pqg_backport(dsa, p, q, g)
201+
} else {
202+
ret = C.go_openssl_DSA_set0_pqg(dsa, p, q, g)
203+
}
204+
if ret != 1 {
191205
C.go_openssl_BN_free(p)
192206
C.go_openssl_BN_free(q)
193207
C.go_openssl_BN_free(g)
@@ -196,7 +210,12 @@ func newDSA1(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
196210
}
197211
if Y != nil {
198212
pub, priv := bigToBN(Y), bigToBN(X)
199-
if C.go_openssl_DSA_set0_key(dsa, pub, priv) != 1 {
213+
if vMinor == 0 {
214+
ret = C.go_openssl_DSA_set0_key_backport(dsa, pub, priv)
215+
} else {
216+
ret = C.go_openssl_DSA_set0_key(dsa, pub, priv)
217+
}
218+
if ret != 1 {
200219
C.go_openssl_BN_free(pub)
201220
C.go_openssl_BN_clear_free(priv)
202221
C.go_openssl_DSA_free(dsa)

goopenssl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ int go_openssl_version_patch(void* handle);
2828
int go_openssl_thread_setup(void);
2929
void go_openssl_load_functions(void* handle, unsigned int major, unsigned int minor, unsigned int patch);
3030
const GO_EVP_MD_PTR go_openssl_EVP_md5_sha1_backport(void);
31+
void go_openssl_DSA_get0_pqg_backport(const GO_DSA_PTR d, GO_BIGNUM_PTR *p, GO_BIGNUM_PTR *q, GO_BIGNUM_PTR *g);
32+
int go_openssl_DSA_set0_pqg_backport(GO_DSA_PTR d, GO_BIGNUM_PTR p, GO_BIGNUM_PTR q, GO_BIGNUM_PTR g);
33+
void go_openssl_DSA_get0_key_backport(const GO_DSA_PTR d, GO_BIGNUM_PTR *pub_key, GO_BIGNUM_PTR *priv_key);
34+
int go_openssl_DSA_set0_key_backport(GO_DSA_PTR d, GO_BIGNUM_PTR pub_key, GO_BIGNUM_PTR priv_key);
3135

3236
// Define pointers to all the used OpenSSL functions.
3337
// Calling C function pointers from Go is currently not supported.

port_dsa.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// The following is a partial backport of crypto/dsa/dsa_pmeth.c,
2+
// commit cbc8a839959418d8a2c2e3ec6bdf394852c9501e on the
3+
// OpenSSL_1_1_0-stable branch. The ctrl function has been removed.
4+
5+
#include "goopenssl.h"
6+
7+
struct dsa_st
8+
{
9+
int _ignored0;
10+
long _ignored1;
11+
int _ignored2;
12+
GO_BIGNUM_PTR p;
13+
GO_BIGNUM_PTR q;
14+
GO_BIGNUM_PTR g;
15+
GO_BIGNUM_PTR pub_key;
16+
GO_BIGNUM_PTR priv_key;
17+
};
18+
19+
20+
void go_openssl_DSA_get0_pqg_backport(const GO_DSA_PTR dsa,
21+
GO_BIGNUM_PTR *p, GO_BIGNUM_PTR *q, GO_BIGNUM_PTR *g)
22+
{
23+
const struct dsa_st *d = dsa;
24+
if (p != NULL)
25+
*p = d->p;
26+
if (q != NULL)
27+
*q = d->q;
28+
if (g != NULL)
29+
*g = d->g;
30+
}
31+
32+
int go_openssl_DSA_set0_pqg_backport(GO_DSA_PTR dsa,
33+
GO_BIGNUM_PTR p, GO_BIGNUM_PTR q, GO_BIGNUM_PTR g)
34+
{
35+
struct dsa_st *d = dsa;
36+
if ((d->p == NULL && p == NULL)
37+
|| (d->q == NULL && q == NULL)
38+
|| (d->g == NULL && g == NULL))
39+
return 0;
40+
41+
if (p != NULL) {
42+
go_openssl_BN_free(d->p);
43+
d->p = p;
44+
}
45+
if (q != NULL) {
46+
go_openssl_BN_free(d->q);
47+
d->q = q;
48+
}
49+
if (g != NULL) {
50+
go_openssl_BN_free(d->g);
51+
d->g = g;
52+
}
53+
54+
return 1;
55+
}
56+
57+
void go_openssl_DSA_get0_key_backport(const GO_DSA_PTR dsa,
58+
GO_BIGNUM_PTR *pub_key, GO_BIGNUM_PTR *priv_key)
59+
{
60+
const struct dsa_st *d = dsa;
61+
if (pub_key != NULL)
62+
*pub_key = d->pub_key;
63+
if (priv_key != NULL)
64+
*priv_key = d->priv_key;
65+
}
66+
67+
int go_openssl_DSA_set0_key_backport(GO_DSA_PTR dsa, GO_BIGNUM_PTR pub_key, GO_BIGNUM_PTR priv_key)
68+
{
69+
struct dsa_st *d = dsa;
70+
if (d->pub_key == NULL && pub_key == NULL)
71+
return 0;
72+
73+
if (pub_key != NULL) {
74+
go_openssl_BN_free(d->pub_key);
75+
d->pub_key = pub_key;
76+
}
77+
if (priv_key != NULL) {
78+
go_openssl_BN_free(d->priv_key);
79+
d->priv_key = priv_key;
80+
}
81+
82+
return 1;
83+
}

0 commit comments

Comments
 (0)