Skip to content

Commit a685991

Browse files
authored
Merge pull request #141 from mcr/master
instead of looking of NIDs and then using X509V3_EXT_nconf_nid,
2 parents fcda6cf + 9f15741 commit a685991

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

ext/openssl/ossl_x509ext.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,16 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
209209
int nid;
210210
VALUE rconf;
211211
CONF *conf;
212+
const char *oid_cstr = NULL;
212213

213214
rb_scan_args(argc, argv, "21", &oid, &value, &critical);
214-
StringValueCStr(oid);
215215
StringValue(value);
216216
if(NIL_P(critical)) critical = Qfalse;
217217

218-
nid = OBJ_ln2nid(RSTRING_PTR(oid));
219-
if(!nid) nid = OBJ_sn2nid(RSTRING_PTR(oid));
220-
if(!nid) ossl_raise(eX509ExtError, "unknown OID `%"PRIsVALUE"'", oid);
218+
oid_cstr = StringValueCStr(oid);
219+
nid = OBJ_ln2nid(oid_cstr);
220+
if (nid != NID_undef)
221+
oid_cstr = OBJ_nid2sn(nid);
221222

222223
valstr = rb_str_new2(RTEST(critical) ? "critical," : "");
223224
rb_str_append(valstr, value);
@@ -228,7 +229,12 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
228229
rconf = rb_iv_get(self, "@config");
229230
conf = NIL_P(rconf) ? NULL : GetConfig(rconf);
230231
X509V3_set_nconf(ctx, conf);
231-
ext = X509V3_EXT_nconf_nid(conf, ctx, nid, RSTRING_PTR(valstr));
232+
233+
#if OSSL_OPENSSL_PREREQ(1, 1, 0) || OSSL_IS_LIBRESSL
234+
ext = X509V3_EXT_nconf(conf, ctx, oid_cstr, RSTRING_PTR(valstr));
235+
#else
236+
ext = X509V3_EXT_nconf(conf, ctx, (char *)oid_cstr, RSTRING_PTR(valstr));
237+
#endif
232238
X509V3_set_ctx_nodb(ctx);
233239
if (!ext){
234240
ossl_raise(eX509ExtError, "%"PRIsVALUE" = %"PRIsVALUE, oid, valstr);

test/openssl/test_ossl.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ def test_error_data
6767
#
6868
# The generated message should look like:
6969
# "subjectAltName = IP:not.a.valid.ip.address: bad ip address (value=not.a.valid.ip.address)"
70+
# "subjectAltName = IP:not.a.valid.ip.address: error in extension (name=subjectAltName, value=IP:not.a.valid.ip.address)"
7071
ef = OpenSSL::X509::ExtensionFactory.new
71-
assert_raise_with_message(OpenSSL::X509::ExtensionError, /\(value=not.a.valid.ip.address\)/) {
72+
assert_raise_with_message(OpenSSL::X509::ExtensionError, /value=(IP:)?not.a.valid.ip.address\)/) {
7273
ef.create_ext("subjectAltName", "IP:not.a.valid.ip.address")
7374
}
7475
end

test/openssl/test_x509ext.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ def test_create_by_factory
7070
assert_match(%r{http://cps.example.com}, cp.value)
7171
end
7272

73+
def test_factory_create_extension_sn_ln
74+
ef = OpenSSL::X509::ExtensionFactory.new
75+
bc_sn = ef.create_extension("basicConstraints", "critical, CA:TRUE, pathlen:2")
76+
bc_ln = ef.create_extension("X509v3 Basic Constraints", "critical, CA:TRUE, pathlen:2")
77+
assert_equal(@basic_constraints.to_der, bc_sn.to_der)
78+
assert_equal(@basic_constraints.to_der, bc_ln.to_der)
79+
end
80+
81+
def test_factory_create_extension_oid
82+
ef = OpenSSL::X509::ExtensionFactory.new
83+
ef.config = OpenSSL::Config.parse(<<~_end_of_cnf_)
84+
[basic_constraints]
85+
cA = BOOLEAN:TRUE
86+
pathLenConstraint = INTEGER:2
87+
_end_of_cnf_
88+
bc_oid = ef.create_extension("2.5.29.19", "ASN1:SEQUENCE:basic_constraints", true)
89+
assert_equal(@basic_constraints.to_der, bc_oid.to_der)
90+
end
91+
7392
def test_dup
7493
ext = OpenSSL::X509::Extension.new(@basic_constraints.to_der)
7594
assert_equal(@basic_constraints.to_der, ext.to_der)

0 commit comments

Comments
 (0)