Skip to content

Commit 4db2605

Browse files
authored
Merge pull request #978 from botovq/opaque-asn1-string
Treat ASN1_STRING as opaque
2 parents 77f730c + a41cf28 commit 4db2605

File tree

7 files changed

+17
-15
lines changed

7 files changed

+17
-15
lines changed

ext/openssl/ossl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ ossl_buf2str(char *buf, int len)
122122
}
123123

124124
void
125-
ossl_bin2hex(unsigned char *in, char *out, size_t inlen)
125+
ossl_bin2hex(const unsigned char *in, char *out, size_t inlen)
126126
{
127127
const char *hex = "0123456789abcdef";
128128
size_t i;

ext/openssl/ossl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ do{\
131131
* Convert binary string to hex string. The caller is responsible for
132132
* ensuring out has (2 * len) bytes of capacity.
133133
*/
134-
void ossl_bin2hex(unsigned char *in, char *out, size_t len);
134+
void ossl_bin2hex(const unsigned char *in, char *out, size_t len);
135135

136136
/*
137137
* Our default PEM callback

ext/openssl/ossl_asn1.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ ossl_time_split(VALUE time, time_t *sec, int *days)
114114
VALUE
115115
asn1str_to_str(const ASN1_STRING *str)
116116
{
117-
return rb_str_new((const char *)str->data, str->length);
117+
return rb_str_new((const char *)ASN1_STRING_get0_data(str),
118+
ASN1_STRING_length(str));
118119
}
119120

120121
/*
@@ -129,7 +130,7 @@ asn1integer_to_num(const ASN1_INTEGER *ai)
129130
if (!ai) {
130131
ossl_raise(rb_eTypeError, "ASN1_INTEGER is NULL!");
131132
}
132-
if (ai->type == V_ASN1_ENUMERATED)
133+
if (ASN1_STRING_type(ai) == V_ASN1_ENUMERATED)
133134
/* const_cast: workaround for old OpenSSL */
134135
bn = ASN1_ENUMERATED_to_BN((ASN1_ENUMERATED *)ai, NULL);
135136
else

ext/openssl/ossl_ns_spki.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,12 @@ ossl_spki_get_challenge(VALUE self)
230230
NETSCAPE_SPKI *spki;
231231

232232
GetSPKI(self, spki);
233-
if (spki->spkac->challenge->length <= 0) {
233+
if (ASN1_STRING_length(spki->spkac->challenge) <= 0) {
234234
OSSL_Debug("Challenge.length <= 0?");
235235
return rb_str_new(0, 0);
236236
}
237237

238-
return rb_str_new((const char *)spki->spkac->challenge->data,
239-
spki->spkac->challenge->length);
238+
return asn1str_to_str(spki->spkac->challenge);
240239
}
241240

242241
/*

ext/openssl/ossl_ocsp.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,8 +1550,9 @@ ossl_ocspcid_get_issuer_name_hash(VALUE self)
15501550
GetOCSPCertId(self, id);
15511551
OCSP_id_get0_info(&name_hash, NULL, NULL, NULL, id);
15521552

1553-
ret = rb_str_new(NULL, name_hash->length * 2);
1554-
ossl_bin2hex(name_hash->data, RSTRING_PTR(ret), name_hash->length);
1553+
ret = rb_str_new(NULL, ASN1_STRING_length(name_hash) * 2);
1554+
ossl_bin2hex(ASN1_STRING_get0_data(name_hash), RSTRING_PTR(ret),
1555+
ASN1_STRING_length(name_hash));
15551556

15561557
return ret;
15571558
}
@@ -1573,8 +1574,9 @@ ossl_ocspcid_get_issuer_key_hash(VALUE self)
15731574
GetOCSPCertId(self, id);
15741575
OCSP_id_get0_info(NULL, NULL, &key_hash, NULL, id);
15751576

1576-
ret = rb_str_new(NULL, key_hash->length * 2);
1577-
ossl_bin2hex(key_hash->data, RSTRING_PTR(ret), key_hash->length);
1577+
ret = rb_str_new(NULL, ASN1_STRING_length(key_hash) * 2);
1578+
ossl_bin2hex(ASN1_STRING_get0_data(key_hash), RSTRING_PTR(ret),
1579+
ASN1_STRING_length(key_hash));
15781580

15791581
return ret;
15801582
}

ext/openssl/ossl_ts.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ ossl_ts_req_get_msg_imprint(VALUE self)
259259
mi = TS_REQ_get_msg_imprint(req);
260260
hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
261261

262-
ret = rb_str_new((const char *)hashed_msg->data, hashed_msg->length);
262+
ret = asn1str_to_str(hashed_msg);
263263

264264
return ret;
265265
}
@@ -470,7 +470,7 @@ ossl_ts_req_to_der(VALUE self)
470470
ossl_raise(eTimestampError, "Message imprint missing algorithm");
471471

472472
hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
473-
if (!hashed_msg->length)
473+
if (!ASN1_STRING_length(hashed_msg))
474474
ossl_raise(eTimestampError, "Message imprint missing hashed message");
475475

476476
return asn1_to_der((void *)req, (int (*)(void *, unsigned char **))i2d_TS_REQ);
@@ -981,7 +981,7 @@ ossl_ts_token_info_get_msg_imprint(VALUE self)
981981
GetTSTokenInfo(self, info);
982982
mi = TS_TST_INFO_get_msg_imprint(info);
983983
hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
984-
ret = rb_str_new((const char *)hashed_msg->data, hashed_msg->length);
984+
ret = asn1str_to_str(hashed_msg);
985985

986986
return ret;
987987
}

ext/openssl/ossl_x509ext.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ ossl_x509ext_get_value_der(VALUE obj)
402402
if ((value = X509_EXTENSION_get_data(ext)) == NULL)
403403
ossl_raise(eX509ExtError, NULL);
404404

405-
return rb_str_new((const char *)value->data, value->length);
405+
return asn1str_to_str(value);
406406
}
407407

408408
static VALUE

0 commit comments

Comments
 (0)