@@ -167,7 +167,7 @@ public X509Name(Ruby runtime, RubyClass type) {
167
167
168
168
private final List <ASN1ObjectIdentifier > oids ;
169
169
private final List <ASN1Encodable > values ; // <ASN1String>
170
- private final List <RubyInteger > types ;
170
+ private final List <Integer > types ;
171
171
172
172
private transient X500Name name ;
173
173
private transient X500Name canonicalName ;
@@ -239,17 +239,14 @@ private void addType(final Ruby runtime, final ASN1Encodable value) {
239
239
this .name = null ; // NOTE: each fromX factory calls this ...
240
240
this .canonicalName = null ;
241
241
final Integer type = ASN1 .typeId (value );
242
- if ( type == null ) {
242
+ if (type == null ) {
243
243
warn (runtime .getCurrentContext (), this + " addType() could not resolve type for: " +
244
244
value + " (" + (value == null ? "" : value .getClass ().getName ()) + ")" );
245
- ((List ) this .types ).add ( runtime .getNil () );
246
- }
247
- else {
248
- this .types .add ( runtime .newFixnum ( type .intValue () ) );
249
245
}
246
+ this .types .add (type );
250
247
}
251
248
252
- private void addEntry (ASN1ObjectIdentifier oid , RubyString value , RubyInteger type ) throws RuntimeException {
249
+ private void addEntry (ASN1ObjectIdentifier oid , RubyString value , final int type ) throws RuntimeException {
253
250
this .name = null ;
254
251
this .canonicalName = null ;
255
252
this .oids .add (oid );
@@ -279,9 +276,7 @@ public IRubyObject initialize(final ThreadContext context, IRubyObject dn, IRuby
279
276
if ( dn instanceof RubyArray ) {
280
277
RubyArray ary = (RubyArray ) dn ;
281
278
282
- final RubyClass _Name = _Name (runtime );
283
-
284
- if ( template .isNil () ) template = _Name .getConstant ("OBJECT_TYPE_TEMPLATE" );
279
+ if (template .isNil ()) template = _Name (runtime ).getConstant ("OBJECT_TYPE_TEMPLATE" );
285
280
286
281
for (int i = 0 ; i < ary .size (); i ++) {
287
282
IRubyObject obj = ary .eltOk (i );
@@ -297,8 +292,7 @@ public IRubyObject initialize(final ThreadContext context, IRubyObject dn, IRuby
297
292
value = arr .size () > 1 ? arr .eltOk (1 ) : context .nil ;
298
293
type = arr .size () > 2 ? arr .eltOk (2 ) : context .nil ;
299
294
300
- if (type .isNil ()) type = template .callMethod (context , "[]" , name );
301
- if (type .isNil ()) type = _Name .getConstant ("DEFAULT_OBJECT_TYPE" );
295
+ if (type .isNil ()) type = getDefaultType (context , name , template );
302
296
303
297
add_entry (context , name , value , type );
304
298
}
@@ -343,7 +337,7 @@ else if ( obj instanceof ASN1Set ) {
343
337
344
338
@ JRubyMethod
345
339
public IRubyObject add_entry (ThreadContext context , IRubyObject oid , IRubyObject value ) {
346
- return add_entry (context , oid , value , null );
340
+ return add_entry (context , oid , value , context . nil );
347
341
}
348
342
349
343
@ JRubyMethod
@@ -353,7 +347,7 @@ public IRubyObject add_entry(final ThreadContext context,
353
347
354
348
final RubyString oidStr = oid .asString ();
355
349
356
- if ( type == null || type .isNil () ) type = getDefaultType (context , oidStr );
350
+ if ( type .isNil () ) type = getDefaultType (context , oidStr );
357
351
358
352
final ASN1ObjectIdentifier objectId ;
359
353
try {
@@ -365,22 +359,30 @@ public IRubyObject add_entry(final ThreadContext context,
365
359
// NOTE: won't reach here :
366
360
if ( objectId == null ) throw newNameError (runtime , "invalid field name" );
367
361
362
+ final int typeInt = type .convertToInteger ().getIntValue ();
363
+ if (ASN1 .typeClassSafe (typeInt ) == null ) {
364
+ throw newNameError (runtime , "invalid type: " + typeInt );
365
+ }
366
+
368
367
try {
369
- addEntry (objectId , value .asString (), ( RubyInteger ) type );
368
+ addEntry (objectId , value .asString (), typeInt );
370
369
}
371
370
catch (RuntimeException e ) {
371
+ debugStackTrace (runtime , e );
372
372
String msg = e .getMessage (); // X509DefaultEntryConverted: "can't recode value for oid " + oid.getId()
373
373
throw newNameError (runtime , msg == null ? "invalid value" : msg , e );
374
374
}
375
375
return this ;
376
376
}
377
377
378
- private static IRubyObject getDefaultType (final ThreadContext context , final RubyString oid ) {
379
- IRubyObject template = _Name (context .runtime ).getConstant ("OBJECT_TYPE_TEMPLATE" );
380
- if ( template instanceof RubyHash ) {
381
- return ((RubyHash ) template ).op_aref (context , oid );
382
- }
383
- return template .callMethod (context , "[]" , oid );
378
+ private IRubyObject getDefaultType (final ThreadContext context , final RubyString oid ) {
379
+ return getDefaultType (context , oid , _Name (context .runtime ).getConstant ("OBJECT_TYPE_TEMPLATE" ));
380
+ }
381
+
382
+ private IRubyObject getDefaultType (final ThreadContext context , final IRubyObject oid , final IRubyObject template ) {
383
+ final IRubyObject type = template instanceof RubyHash ?
384
+ ((RubyHash ) template ).op_aref (context , oid ) : template .callMethod (context , "[]" , oid );
385
+ return type .isNil () ? _Name (context .runtime ).getConstant ("DEFAULT_OBJECT_TYPE" ) : type ;
384
386
}
385
387
386
388
@ SuppressWarnings ("unchecked" )
@@ -496,19 +498,18 @@ public RubyArray to_a() {
496
498
final Ruby runtime = getRuntime ();
497
499
final RubyArray entries = runtime .newArray ( oids .size () );
498
500
final Iterator <ASN1ObjectIdentifier > oidsIter = oids .iterator ();
499
- @ SuppressWarnings ("unchecked" )
500
- final Iterator <Object > valuesIter = (Iterator ) values .iterator ();
501
- final Iterator <RubyInteger > typesIter = types .iterator ();
501
+ final Iterator <ASN1Encodable > valuesIter = values .iterator ();
502
+ final Iterator <Integer > typesIter = types .iterator ();
502
503
while ( oidsIter .hasNext () ) {
503
504
final ASN1ObjectIdentifier oid = oidsIter .next ();
504
505
String oName = name (runtime , oid );
505
506
if ( oName == null ) oName = oid .toString ();
506
507
final String value = valuesIter .next ().toString ();
507
- final IRubyObject type = typesIter .next ();
508
+ final Integer type = typesIter .next ();
508
509
final IRubyObject [] entry = new IRubyObject [] {
509
510
StringHelper .newUTF8String (runtime , oName ),
510
511
StringHelper .newUTF8String (runtime , value ),
511
- type
512
+ type == null ? runtime . getNil () : runtime . newFixnum ( type )
512
513
};
513
514
entries .append ( runtime .newArrayNoCopy (entry ) );
514
515
}
0 commit comments