Skip to content

Commit b5f79f7

Browse files
committed
ts: avoid using OpenSSL::PKCS7's internals
Internals of OpenSSL::PKCS7 should be kept within ossl_pkcs7.c. Add a new ossl_pkcs7_new() function for duplicating and wrapping an OpenSSL PKCS7 object in OpenSSL::PKCS7. This follows the convention used by other ossl_*_new() functions.
1 parent 0201f23 commit b5f79f7

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

ext/openssl/ossl_pkcs7.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
*/
1010
#include "ossl.h"
1111

12+
#define NewPKCS7(klass) \
13+
TypedData_Wrap_Struct((klass), &ossl_pkcs7_type, 0)
14+
#define SetPKCS7(obj, pkcs7) do { \
15+
if (!(pkcs7)) { \
16+
ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \
17+
} \
18+
RTYPEDDATA_DATA(obj) = (pkcs7); \
19+
} while (0)
20+
#define GetPKCS7(obj, pkcs7) do { \
21+
TypedData_Get_Struct((obj), PKCS7, &ossl_pkcs7_type, (pkcs7)); \
22+
if (!(pkcs7)) { \
23+
ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \
24+
} \
25+
} while (0)
26+
1227
#define NewPKCS7si(klass) \
1328
TypedData_Wrap_Struct((klass), &ossl_pkcs7_signer_info_type, 0)
1429
#define SetPKCS7si(obj, p7si) do { \
@@ -49,25 +64,39 @@
4964
/*
5065
* Classes
5166
*/
52-
VALUE cPKCS7;
53-
VALUE cPKCS7Signer;
54-
VALUE cPKCS7Recipient;
55-
VALUE ePKCS7Error;
67+
static VALUE cPKCS7;
68+
static VALUE cPKCS7Signer;
69+
static VALUE cPKCS7Recipient;
70+
static VALUE ePKCS7Error;
5671

5772
static void
5873
ossl_pkcs7_free(void *ptr)
5974
{
6075
PKCS7_free(ptr);
6176
}
6277

63-
const rb_data_type_t ossl_pkcs7_type = {
78+
static const rb_data_type_t ossl_pkcs7_type = {
6479
"OpenSSL/PKCS7",
6580
{
6681
0, ossl_pkcs7_free,
6782
},
6883
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
6984
};
7085

86+
VALUE
87+
ossl_pkcs7_new(PKCS7 *p7)
88+
{
89+
PKCS7 *new;
90+
VALUE obj = NewPKCS7(cPKCS7);
91+
92+
new = PKCS7_dup(p7);
93+
if (!new)
94+
ossl_raise(ePKCS7Error, "PKCS7_dup");
95+
SetPKCS7(obj, new);
96+
97+
return obj;
98+
}
99+
71100
static void
72101
ossl_pkcs7_signer_info_free(void *ptr)
73102
{

ext/openssl/ossl_pkcs7.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,7 @@
1010
#if !defined(_OSSL_PKCS7_H_)
1111
#define _OSSL_PKCS7_H_
1212

13-
#define NewPKCS7(klass) \
14-
TypedData_Wrap_Struct((klass), &ossl_pkcs7_type, 0)
15-
#define SetPKCS7(obj, pkcs7) do { \
16-
if (!(pkcs7)) { \
17-
ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \
18-
} \
19-
RTYPEDDATA_DATA(obj) = (pkcs7); \
20-
} while (0)
21-
#define GetPKCS7(obj, pkcs7) do { \
22-
TypedData_Get_Struct((obj), PKCS7, &ossl_pkcs7_type, (pkcs7)); \
23-
if (!(pkcs7)) { \
24-
ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \
25-
} \
26-
} while (0)
27-
28-
extern const rb_data_type_t ossl_pkcs7_type;
29-
extern VALUE cPKCS7;
30-
extern VALUE cPKCS7Signer;
31-
extern VALUE cPKCS7Recipient;
32-
extern VALUE ePKCS7Error;
33-
13+
VALUE ossl_pkcs7_new(PKCS7 *p7);
3414
void Init_ossl_pkcs7(void);
3515

3616
#endif /* _OSSL_PKCS7_H_ */

ext/openssl/ossl_ts.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -691,21 +691,12 @@ static VALUE
691691
ossl_ts_resp_get_token(VALUE self)
692692
{
693693
TS_RESP *resp;
694-
PKCS7 *p7, *copy;
695-
VALUE obj;
694+
PKCS7 *p7;
696695

697696
GetTSResponse(self, resp);
698697
if (!(p7 = TS_RESP_get_token(resp)))
699698
return Qnil;
700-
701-
obj = NewPKCS7(cPKCS7);
702-
703-
if (!(copy = PKCS7_dup(p7)))
704-
ossl_raise(eTimestampError, NULL);
705-
706-
SetPKCS7(obj, copy);
707-
708-
return obj;
699+
return ossl_pkcs7_new(p7);
709700
}
710701

711702
/*

0 commit comments

Comments
 (0)