Skip to content

Commit ebb505f

Browse files
rheniumjunaruga
authored andcommitted
asn1: use new ASN1_BIT_STRING accessor functions with OpenSSL 4.0
ASN1_STRING has been made opaque in OpenSSL's master branch. Use the new accessor functions instead of accessing fields directly. Other uses of ASN1_STRING fields were already updated in <#978>. This patch converts the remaining ones, which require the new functions added in OpenSSL 4.0 and were not available at that time.
1 parent faad7a0 commit ebb505f

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

ext/openssl/extconf.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def find_openssl_library
169169
# added in 3.5.0
170170
have_func("SSL_get0_peer_signature_name(NULL, NULL)", ssl_h)
171171

172+
# added in 4.0.0
173+
have_func("ASN1_BIT_STRING_set1(NULL, NULL, 0, 0)", "openssl/asn1.h")
174+
172175
Logging::message "=== Checking done. ===\n"
173176

174177
# Append flags from environment variables.

ext/openssl/openssl_missing.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,27 @@
2929
# define EVP_PKEY_eq(a, b) EVP_PKEY_cmp(a, b)
3030
#endif
3131

32+
/* added in 4.0.0 */
33+
#ifndef HAVE_ASN1_BIT_STRING_SET1
34+
static inline int
35+
ASN1_BIT_STRING_set1(ASN1_BIT_STRING *bitstr, const uint8_t *data,
36+
size_t length, int unused_bits)
37+
{
38+
if (length > INT_MAX || !ASN1_STRING_set(bitstr, data, (int)length))
39+
return 0;
40+
bitstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
41+
bitstr->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits;
42+
return 1;
43+
}
44+
45+
static inline int
46+
ASN1_BIT_STRING_get_length(const ASN1_BIT_STRING *bitstr, size_t *length,
47+
int *unused_bits)
48+
{
49+
*length = bitstr->length;
50+
*unused_bits = bitstr->flags & 0x07;
51+
return 1;
52+
}
53+
#endif
54+
3255
#endif /* _OSSL_OPENSSL_MISSING_H_ */

ext/openssl/ossl_asn1.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,19 @@ obj_to_asn1int(VALUE obj)
228228
}
229229

230230
static ASN1_BIT_STRING*
231-
obj_to_asn1bstr(VALUE obj, long unused_bits)
231+
obj_to_asn1bstr(VALUE obj, int unused_bits)
232232
{
233233
ASN1_BIT_STRING *bstr;
234234

235235
if (unused_bits < 0 || unused_bits > 7)
236236
ossl_raise(eASN1Error, "unused_bits for a bitstring value must be in "\
237237
"the range 0 to 7");
238238
StringValue(obj);
239-
if(!(bstr = ASN1_BIT_STRING_new()))
240-
ossl_raise(eASN1Error, NULL);
241-
ASN1_BIT_STRING_set(bstr, (unsigned char *)RSTRING_PTR(obj), RSTRING_LENINT(obj));
242-
bstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear */
243-
bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits;
239+
if (!(bstr = ASN1_BIT_STRING_new()))
240+
ossl_raise(eASN1Error, "ASN1_BIT_STRING_new");
241+
if (!ASN1_BIT_STRING_set1(bstr, (uint8_t *)RSTRING_PTR(obj),
242+
RSTRING_LEN(obj), unused_bits))
243+
ossl_raise(eASN1Error, "ASN1_BIT_STRING_set1");
244244

245245
return bstr;
246246
}
@@ -364,22 +364,25 @@ decode_int(unsigned char* der, long length)
364364
}
365365

366366
static VALUE
367-
decode_bstr(unsigned char* der, long length, long *unused_bits)
367+
decode_bstr(unsigned char* der, long length, int *unused_bits)
368368
{
369369
ASN1_BIT_STRING *bstr;
370370
const unsigned char *p;
371-
long len;
371+
size_t len;
372372
VALUE ret;
373+
int state;
373374

374375
p = der;
375-
if(!(bstr = d2i_ASN1_BIT_STRING(NULL, &p, length)))
376-
ossl_raise(eASN1Error, NULL);
377-
len = bstr->length;
378-
*unused_bits = 0;
379-
if(bstr->flags & ASN1_STRING_FLAG_BITS_LEFT)
380-
*unused_bits = bstr->flags & 0x07;
381-
ret = rb_str_new((const char *)bstr->data, len);
376+
if (!(bstr = d2i_ASN1_BIT_STRING(NULL, &p, length)))
377+
ossl_raise(eASN1Error, "d2i_ASN1_BIT_STRING");
378+
if (!ASN1_BIT_STRING_get_length(bstr, &len, unused_bits)) {
379+
ASN1_BIT_STRING_free(bstr);
380+
ossl_raise(eASN1Error, "ASN1_BIT_STRING_get_length");
381+
}
382+
ret = ossl_str_new((const char *)ASN1_STRING_get0_data(bstr), len, &state);
382383
ASN1_BIT_STRING_free(bstr);
384+
if (state)
385+
rb_jump_tag(state);
383386

384387
return ret;
385388
}
@@ -763,7 +766,7 @@ int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag,
763766
{
764767
VALUE value, asn1data;
765768
unsigned char *p;
766-
long flag = 0;
769+
int flag = 0;
767770

768771
p = *pp;
769772

@@ -820,7 +823,7 @@ int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag,
820823
asn1data = rb_obj_alloc(klass);
821824
ossl_asn1_initialize(4, args, asn1data);
822825
if(tag == V_ASN1_BIT_STRING){
823-
rb_ivar_set(asn1data, sivUNUSED_BITS, LONG2NUM(flag));
826+
rb_ivar_set(asn1data, sivUNUSED_BITS, INT2NUM(flag));
824827
}
825828
}
826829
else {

0 commit comments

Comments
 (0)