Skip to content

Commit 8db4667

Browse files
authored
Merge pull request #75 from Geod24/opaque
rsa: `rsa_method_st` and `rsa_st` are opaque since v1.1.0
2 parents e6a66aa + 7ef7f84 commit 8db4667

File tree

3 files changed

+112
-94
lines changed

3 files changed

+112
-94
lines changed

source/deimos/openssl/evp.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct evp_pkey_st
137137
union pkey_ {
138138
char* ptr;
139139
version(OPENSSL_NO_RSA) {} else {
140-
rsa_st* rsa; /* RSA */
140+
RSA* rsa; /* RSA */
141141
}
142142
version(OPENSSL_NO_DSA) {} else {
143143
dsa_st* dsa; /* DSA */
@@ -901,9 +901,9 @@ int EVP_PKEY_assign(EVP_PKEY* pkey,int type,void* key);
901901
void* EVP_PKEY_get0(EVP_PKEY* pkey);
902902

903903
version(OPENSSL_NO_RSA) {} else {
904-
import deimos.openssl.rsa; /*struct rsa_st;*/
905-
int EVP_PKEY_set1_RSA(EVP_PKEY* pkey,rsa_st* key);
906-
rsa_st* EVP_PKEY_get1_RSA(EVP_PKEY* pkey);
904+
import deimos.openssl.rsa;
905+
int EVP_PKEY_set1_RSA(EVP_PKEY* pkey, RSA* key);
906+
RSA* EVP_PKEY_get1_RSA(EVP_PKEY* pkey);
907907
}
908908
version(OPENSSL_NO_DSA) {} else {
909909
import deimos.openssl.dsa; /*struct dsa_st;*/

source/deimos/openssl/rsa.d

Lines changed: 106 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,13 @@
1-
/* crypto/rsa/rsa.h */
2-
/* Copyright (C) 1995-1998 Eric Young ([email protected])
3-
* All rights reserved.
1+
/**
2+
* Port of `openssl.rsa.h`
43
*
5-
* This package is an SSL implementation written
6-
* by Eric Young ([email protected]).
7-
* The implementation was written so as to conform with Netscapes SSL.
4+
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
85
*
9-
* This library is free for commercial and non-commercial use as long as
10-
* the following conditions are aheared to. The following conditions
11-
* apply to all code found in this distribution, be it the RC4, RSA,
12-
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
13-
* included with this distribution is covered by the same copyright terms
14-
* except that the holder is Tim Hudson ([email protected]).
15-
*
16-
* Copyright remains Eric Young's, and as such any Copyright notices in
17-
* the code are not to be removed.
18-
* If this package is used in a product, Eric Young should be given attribution
19-
* as the author of the parts of the library used.
20-
* This can be in the form of a textual message at program startup or
21-
* in documentation (online or textual) provided with the package.
22-
*
23-
* Redistribution and use in source and binary forms, with or without
24-
* modification, are permitted provided that the following conditions
25-
* are met:
26-
* 1. Redistributions of source code must retain the copyright
27-
* notice, this list of conditions and the following disclaimer.
28-
* 2. Redistributions in binary form must reproduce the above copyright
29-
* notice, this list of conditions and the following disclaimer in the
30-
* documentation and/or other materials provided with the distribution.
31-
* 3. All advertising materials mentioning features or use of this software
32-
* must display the following acknowledgement:
33-
* "This product includes cryptographic software written by
34-
* Eric Young ([email protected])"
35-
* The word 'cryptographic' can be left out if the rouines from the library
36-
* being used are not cryptographic related :-).
37-
* 4. If you include any Windows specific code (or a derivative thereof) from
38-
* the apps directory (application code) you must include an acknowledgement:
39-
* "This product includes software written by Tim Hudson ([email protected])"
40-
*
41-
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44-
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47-
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48-
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49-
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50-
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51-
* SUCH DAMAGE.
52-
*
53-
* The licence and distribution terms for any publically available version or
54-
* derivative of this code cannot be changed. i.e. this code cannot simply be
55-
* copied and put under another distribution licence
56-
* [including the GNU Public Licence.]
6+
* Licensed under the Apache License 2.0 (the "License"). You may not use
7+
* this file except in compliance with the License. You can obtain a copy
8+
* in the file LICENSE in the source distribution or at
9+
* https://www.openssl.org/source/license.html
5710
*/
58-
5911
module deimos.openssl.rsa;
6012

6113
import deimos.openssl._d_util;
@@ -81,31 +33,83 @@ version (OPENSSL_NO_RSA) {
8133
extern (C):
8234
nothrow:
8335

84-
/* Declared already in types.h */
85-
/* typedef rsa_st RSA; */
86-
/* typedef rsa_meth_st RSA_METHOD; */
36+
// The following aliases are derived from the `RSA_meth_*` functions' signatures
37+
// They are not present in the code, hence are `private`.
38+
private alias RSA_enc_dec_fn = extern(C) int function(int flen,
39+
const(ubyte)* from, ubyte* to, RSA* rsa, int padding);
40+
private alias RSA_modexp_fn = extern(C) int function(BIGNUM* r0,
41+
const(BIGNUM)* I, RSA* rsa, BN_CTX* ctx);
42+
private alias RSA_bn_modexp_fn = extern(C) int function(BIGNUM* r,
43+
const(BIGNUM)* a, const(BIGNUM)* p, const(BIGNUM)* m, BN_CTX* ctx,
44+
BN_MONT_CTX* m_ctx);
45+
private alias RSA_lifetime_fn = extern(C) int function(RSA* rsa);
46+
private alias RSA_sign_fn = extern(C) int function(int type,
47+
const(ubyte)* m, uint m_length, ubyte* sigret, uint* siglen, const(RSA)* rsa);
48+
private alias RSA_verify_fn = extern(C) int function(int dtype,
49+
const(ubyte)* m, uint m_length, const(ubyte)* sigret, uint* siglen,
50+
const(RSA)* rsa);
51+
private alias RSA_keygen_fn = extern(C) int function(RSA* rsa,
52+
int bits, BIGNUM* e, BN_GENCB* cb);
53+
54+
static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0))
55+
{
56+
// https://github.com/openssl/openssl/commit/b72c9121379a5de0c8be0d4e1a4a6b9495042621
57+
58+
RSA_METHOD* RSA_meth_new(const(char)* name, int flags);
59+
void RSA_meth_free(RSA_METHOD* meth);
60+
RSA_METHOD* RSA_meth_dup(const(RSA_METHOD)* meth);
61+
62+
const(char)* RSA_meth_get0_name(const(RSA_METHOD)* meth);
63+
int RSA_meth_set1_name(RSA_METHOD* meth, const(char)* name);
64+
65+
int RSA_meth_get_flags(RSA_METHOD* meth);
66+
int RSA_meth_set_flags(RSA_METHOD* meth, int flags);
67+
void* RSA_meth_get0_app_data(const(RSA_METHOD)* meth);
68+
int RSA_meth_set0_app_data(RSA_METHOD* meth, void *app_data);
69+
70+
RSA_enc_dec_fn RSA_meth_get_pub_enc(const(RSA_METHOD)* meth);
71+
int RSA_meth_set_pub_enc(RSA_METHOD* rsa, RSA_enc_dec_fn pub_enc);
72+
RSA_enc_dec_fn RSA_meth_get_pub_dec(const(RSA_METHOD)* meth);
73+
int RSA_meth_set_pub_dec(RSA_METHOD* rsa, RSA_enc_dec_fn pub_dec);
74+
75+
RSA_enc_dec_fn RSA_meth_get_priv_enc(const(RSA_METHOD)* meth);
76+
int RSA_meth_set_priv_enc(RSA_METHOD* rsa, RSA_enc_dec_fn priv_enc);
77+
RSA_enc_dec_fn RSA_meth_get_priv_dec(const(RSA_METHOD)* meth);
78+
int RSA_meth_set_priv_dec(RSA_METHOD* rsa, RSA_enc_dec_fn priv_dec);
79+
80+
RSA_modexp_fn RSA_meth_get_mod_exp(const(RSA_METHOD)* meth);
81+
int RSA_meth_set_mod_exp(RSA_METHOD* rsa, RSA_modexp_fn mod_exp);
82+
83+
RSA_bn_modexp_fn RSA_meth_get_bn_mod_exp(const(RSA_METHOD)* meth);
84+
int RSA_meth_set_bn_mod_exp(RSA_METHOD* rsa, RSA_bn_modexp_fn bn_mod_exp);
85+
86+
RSA_lifetime_fn RSA_meth_get_init(const(RSA_METHOD)* meth);
87+
int RSA_meth_set_init(RSA_METHOD* rsa, RSA_lifetime_fn init);
88+
RSA_lifetime_fn RSA_meth_get_finish(const(RSA_METHOD)* meth);
89+
int RSA_meth_set_finish(RSA_METHOD* rsa, RSA_lifetime_fn finish);
90+
91+
RSA_sign_fn RSA_meth_get_sign(const(RSA_METHOD)* meth);
92+
int RSA_meth_set_sign(RSA_METHOD* rsa, RSA_sign_fn sign);
93+
94+
RSA_verify_fn RSA_meth_get_verify(const(RSA_METHOD)* meth);
95+
int RSA_meth_set_verify(RSA_METHOD* rsa, RSA_verify_fn verify);
8796

97+
RSA_keygen_fn RSA_meth_get_keygen(const(RSA_METHOD)* meth);
98+
int RSA_meth_set_keygen(RSA_METHOD* rsa, RSA_keygen_fn keygen);
99+
}
100+
else
101+
{
88102
struct rsa_meth_st
89-
{
103+
{
90104
const(char)* name;
91-
ExternC!(int function(int flen,const(ubyte)* from,
92-
ubyte* to,
93-
RSA* rsa,int padding)) rsa_pub_enc;
94-
ExternC!(int function(int flen,const(ubyte)* from,
95-
ubyte* to,
96-
RSA* rsa,int padding)) rsa_pub_dec;
97-
ExternC!(int function(int flen,const(ubyte)* from,
98-
ubyte* to,
99-
RSA* rsa,int padding)) rsa_priv_enc;
100-
ExternC!(int function(int flen,const(ubyte)* from,
101-
ubyte* to,
102-
RSA* rsa,int padding)) rsa_priv_dec;
103-
ExternC!(int function(BIGNUM* r0,const(BIGNUM)* I,RSA* rsa,BN_CTX* ctx)) rsa_mod_exp; /* Can be null */
104-
ExternC!(int function(BIGNUM* r, const(BIGNUM)* a, const(BIGNUM)* p,
105-
const(BIGNUM)* m, BN_CTX* ctx,
106-
BN_MONT_CTX* m_ctx)) bn_mod_exp; /* Can be null */
107-
ExternC!(int function(RSA* rsa)) init_; /* called at new */
108-
ExternC!(int function(RSA* rsa)) finish; /* called at free */
105+
RSA_enc_dec_fn rsa_pub_enc;
106+
RSA_enc_dec_fn rsa_pub_dec;
107+
RSA_enc_dec_fn rsa_priv_enc;
108+
RSA_enc_dec_fn rsa_priv_dec;
109+
RSA_modexp_fn rsa_mod_exp; /* Can be null */
110+
RSA_bn_modexp_fn bn_mod_exp; /* Can be null */
111+
RSA_lifetime_fn init_; /* called at new */
112+
RSA_lifetime_fn finish; /* called at free */
109113
int flags; /* RSA_METHOD_FLAG_* things */
110114
char* app_data; /* may be needed! */
111115
/* New sign and verify functions: some libraries don't allow arbitrary data
@@ -115,22 +119,35 @@ struct rsa_meth_st
115119
* compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
116120
* option is set in 'flags'.
117121
*/
118-
ExternC!(int function(int type,
119-
const(ubyte)* m, uint m_length,
120-
ubyte* sigret, uint* siglen, const(RSA)* rsa)) rsa_sign;
121-
ExternC!(int function(int dtype,
122-
const(ubyte)* m, uint m_length,
123-
const(ubyte)* sigbuf, uint siglen,
124-
const(RSA)* rsa)) rsa_verify;
122+
RSA_sign_fn rsa_sign;
123+
RSA_verify_fn rsa_verify;
125124
/* If this callback is NULL, the builtin software RSA key-gen will be used. This
126125
* is for behavioural compatibility whilst the code gets rewired, but one day
127126
* it would be nice to assume there are no such things as "builtin software"
128127
* implementations. */
129-
ExternC!(int function(RSA* rsa, int bits, BIGNUM* e, BN_GENCB* cb)) rsa_keygen;
130-
};
128+
RSA_keygen_fn rsa_keygen;
129+
}
130+
}
131131

132+
static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0))
133+
{
134+
// https://github.com/openssl/openssl/commit/9862e9aa98ee1e38fbcef8d1dd5db0e750eb5e8d
135+
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
136+
int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
137+
int RSA_set0_crt_params(RSA *r,BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
138+
void RSA_get0_key(const RSA *r, BIGNUM **n, BIGNUM **e, BIGNUM **d);
139+
void RSA_get0_factors(const RSA *r, BIGNUM **p, BIGNUM **q);
140+
void RSA_get0_crt_params(const RSA *r,
141+
BIGNUM **dmp1, BIGNUM **dmq1, BIGNUM **iqmp);
142+
void RSA_clear_flags(RSA *r, int flags);
143+
int RSA_test_flags(const RSA *r, int flags);
144+
void RSA_set_flags(RSA *r, int flags);
145+
ENGINE *RSA_get0_engine(RSA *r);
146+
}
147+
else
148+
{
132149
struct rsa_st
133-
{
150+
{
134151
/* The first parameter is used to pickup errors where
135152
* this is passed instead of aEVP_PKEY, it is set to 0 */
136153
int pad;
@@ -161,7 +178,8 @@ struct rsa_st
161178
char* bignum_data;
162179
BN_BLINDING* blinding;
163180
BN_BLINDING* mt_blinding;
164-
};
181+
}
182+
}
165183

166184
// #ifndef OPENSSL_RSA_MAX_MODULUS_BITS
167185
enum OPENSSL_RSA_MAX_MODULUS_BITS = 16384;

source/deimos/openssl/types.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ alias dsa_st DSA;
113113
alias dsa_method DSA_METHOD;
114114

115115
import deimos.openssl.rsa;
116-
/*struct rsa_st;*/
116+
private struct rsa_st;
117117
alias rsa_st RSA;
118-
/*struct rsa_meth_st;*/
118+
private struct rsa_meth_st;
119119
alias rsa_meth_st RSA_METHOD;
120120

121121
import deimos.openssl.rand;

0 commit comments

Comments
 (0)