Class: OpenSSL::X509::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/openssl/x509.rb,
ossl_x509ext.c

Instance Method Summary collapse

Constructor Details

#OpenSSL::X509::Extension.new(der) ⇒ Object #OpenSSL::X509::Extension.new(oid, value) ⇒ Object #OpenSSL::X509::Extension.new(oid, value, critical) ⇒ Object

Creates an X509 extension.

The extension may be created from der data or from an extension oid and value. The oid may be either an OID or an extension name. If critical is true the extension is marked critical.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'ossl_x509ext.c', line 272

static VALUE
ossl_x509ext_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE oid, value, critical;
    const unsigned char *p;
    X509_EXTENSION *ext, *x;

    GetX509Ext(self, ext);
    if(rb_scan_args(argc, argv, "12", &oid, &value, &critical) == 1){
	oid = ossl_to_der_if_possible(oid);
	StringValue(oid);
	p = (unsigned char *)RSTRING_PTR(oid);
	x = d2i_X509_EXTENSION(&ext, &p, RSTRING_LEN(oid));
	DATA_PTR(self) = ext;
	if(!x)
	    ossl_raise(eX509ExtError, NULL);
	return self;
    }
    rb_funcall(self, rb_intern("oid="), 1, oid);
    rb_funcall(self, rb_intern("value="), 1, value);
    if(argc > 2) rb_funcall(self, rb_intern("critical="), 1, critical);

    return self;
}

Instance Method Details

#==(other) ⇒ Object



44
45
46
47
# File 'lib/openssl/x509.rb', line 44

def ==(other)
  return false unless Extension === other
  to_der == other.to_der
end

#critical=(flag) ⇒ Object



354
355
356
357
358
359
360
361
362
363
# File 'ossl_x509ext.c', line 354

static VALUE
ossl_x509ext_set_critical(VALUE self, VALUE flag)
{
    X509_EXTENSION *ext;

    GetX509Ext(self, ext);
    X509_EXTENSION_set_critical(ext, RTEST(flag) ? 1 : 0);

    return flag;
}

#critical?Boolean

Returns:

  • (Boolean)


405
406
407
408
409
410
411
412
# File 'ossl_x509ext.c', line 405

static VALUE
ossl_x509ext_get_critical(VALUE obj)
{
    X509_EXTENSION *ext;

    GetX509Ext(obj, ext);
    return X509_EXTENSION_get_critical(ext) ? Qtrue : Qfalse;
}

#initialize_copy(other) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'ossl_x509ext.c', line 297

static VALUE
ossl_x509ext_initialize_copy(VALUE self, VALUE other)
{
    X509_EXTENSION *ext, *ext_other, *ext_new;

    rb_check_frozen(self);
    GetX509Ext(self, ext);
    GetX509Ext(other, ext_other);

    ext_new = X509_EXTENSION_dup(ext_other);
    if (!ext_new)
	ossl_raise(eX509ExtError, "X509_EXTENSION_dup");

    SetX509Ext(self, ext_new);
    X509_EXTENSION_free(ext);

    return self;
}

#oidObject



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'ossl_x509ext.c', line 365

static VALUE
ossl_x509ext_get_oid(VALUE obj)
{
    X509_EXTENSION *ext;
    ASN1_OBJECT *extobj;
    BIO *out;
    VALUE ret;
    int nid;

    GetX509Ext(obj, ext);
    extobj = X509_EXTENSION_get_object(ext);
    if ((nid = OBJ_obj2nid(extobj)) != NID_undef)
	ret = rb_str_new2(OBJ_nid2sn(nid));
    else{
	if (!(out = BIO_new(BIO_s_mem())))
	    ossl_raise(eX509ExtError, NULL);
	i2a_ASN1_OBJECT(out, extobj);
	ret = ossl_membio2str(out);
    }

    return ret;
}

#oid=(oid) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'ossl_x509ext.c', line 316

static VALUE
ossl_x509ext_set_oid(VALUE self, VALUE oid)
{
    X509_EXTENSION *ext;
    ASN1_OBJECT *obj;

    GetX509Ext(self, ext);
    obj = OBJ_txt2obj(StringValueCStr(oid), 0);
    if (!obj)
	ossl_raise(eX509ExtError, "OBJ_txt2obj");
    if (!X509_EXTENSION_set_object(ext, obj)) {
	ASN1_OBJECT_free(obj);
	ossl_raise(eX509ExtError, "X509_EXTENSION_set_object");
    }
    ASN1_OBJECT_free(obj);

    return oid;
}

#to_aObject



60
61
62
# File 'lib/openssl/x509.rb', line 60

def to_a
  [ self.oid, self.value, self.critical? ]
end

#to_derObject



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'ossl_x509ext.c', line 414

static VALUE
ossl_x509ext_to_der(VALUE obj)
{
    X509_EXTENSION *ext;
    unsigned char *p;
    long len;
    VALUE str;

    GetX509Ext(obj, ext);
    if((len = i2d_X509_EXTENSION(ext, NULL)) <= 0)
	ossl_raise(eX509ExtError, NULL);
    str = rb_str_new(0, len);
    p = (unsigned char *)RSTRING_PTR(str);
    if(i2d_X509_EXTENSION(ext, &p) < 0)
	ossl_raise(eX509ExtError, NULL);
    ossl_str_adjust(str, p);

    return str;
}

#to_hObject

“value”=>value, “critical”=>true|false



56
57
58
# File 'lib/openssl/x509.rb', line 56

def to_h # {"oid"=>sn|ln, "value"=>value, "critical"=>true|false}
  {"oid"=>self.oid,"value"=>self.value,"critical"=>self.critical?}
end

#to_sObject

“oid = critical, value”



49
50
51
52
53
54
# File 'lib/openssl/x509.rb', line 49

def to_s # "oid = critical, value"
  str = self.oid
  str << " = "
  str << "critical, " if self.critical?
  str << self.value.gsub(/\n/, ", ")
end

#valueObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'ossl_x509ext.c', line 388

static VALUE
ossl_x509ext_get_value(VALUE obj)
{
    X509_EXTENSION *ext;
    BIO *out;
    VALUE ret;

    GetX509Ext(obj, ext);
    if (!(out = BIO_new(BIO_s_mem())))
	ossl_raise(eX509ExtError, NULL);
    if (!X509V3_EXT_print(out, ext, 0, 0))
	ASN1_STRING_print(out, (ASN1_STRING *)X509_EXTENSION_get_data(ext));
    ret = ossl_membio2str(out);

    return ret;
}

#value=(data) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'ossl_x509ext.c', line 335

static VALUE
ossl_x509ext_set_value(VALUE self, VALUE data)
{
    X509_EXTENSION *ext;
    ASN1_OCTET_STRING *asn1s;

    GetX509Ext(self, ext);
    data = ossl_to_der_if_possible(data);
    StringValue(data);
    asn1s = X509_EXTENSION_get_data(ext);

    if (!ASN1_OCTET_STRING_set(asn1s, (unsigned char *)RSTRING_PTR(data),
			       RSTRING_LENINT(data))) {
	ossl_raise(eX509ExtError, "ASN1_OCTET_STRING_set");
    }

    return data;
}