Class: OpenSSL::X509::Extension (Ruby 2.2.3)
Description: Class : OpenSSL::X509::Extension - Ruby 2.2.3

- Home
- Core 2.2.3
- Std-lib 2.2.3
- Downloads

Support for the Ruby 2.2 series has ended. See here for reference.

### Home Classes Methods

### In Files

- openssl/lib/openssl/x509.rb
- openssl/ossl_ssl_session.c

### Parent

Object

### Methods

- ::new
- #critical=
- #critical?
- #oid
- #oid=
- #to_a
- #to_der
- #to_h
- #to_s
- #value
- #value=

### Class/Module Index

Quicksearch

- Integer
- OpenSSL::ASN1
- OpenSSL::ASN1::ASN1Data
- OpenSSL::ASN1::ASN1Error
- OpenSSL::ASN1::Constructive
- OpenSSL::ASN1::ObjectId
- OpenSSL::ASN1::Primitive
- OpenSSL::BN
- OpenSSL::BNError
- OpenSSL::Buffering
- OpenSSL::Cipher
- OpenSSL::Cipher::Cipher
- OpenSSL::Cipher::CipherError
- OpenSSL::Config
- OpenSSL::ConfigError
- OpenSSL::Digest
- OpenSSL::Digest::DigestError
- OpenSSL::Engine
- OpenSSL::Engine::EngineError
- OpenSSL::HMAC
- OpenSSL::HMACError
- OpenSSL::Netscape
- OpenSSL::Netscape::SPKI
- OpenSSL::Netscape::SPKIError
- OpenSSL::OCSP
- OpenSSL::OCSP::BasicResponse
- OpenSSL::OCSP::CertificateId
- OpenSSL::OCSP::OCSPError
- OpenSSL::OCSP::Request
- OpenSSL::OCSP::Response
- OpenSSL::OpenSSLError
- OpenSSL::PKCS12
- OpenSSL::PKCS12::PKCS12Error
- OpenSSL::PKCS5
- OpenSSL::PKCS5::PKCS5Error
- OpenSSL::PKCS7
- OpenSSL::PKCS7::PKCS7Error
- OpenSSL::PKCS7::RecipientInfo
- OpenSSL::PKCS7::SignerInfo
- OpenSSL::PKey
- OpenSSL::PKey::DH
- OpenSSL::PKey::DHError
- OpenSSL::PKey::DSA
- OpenSSL::PKey::DSAError
- OpenSSL::PKey::EC
- OpenSSL::PKey::EC::Group
- OpenSSL::PKey::EC::Group::Error
- OpenSSL::PKey::EC::Point
- OpenSSL::PKey::EC::Point::Error
- OpenSSL::PKey::ECError
- OpenSSL::PKey::PKey
- OpenSSL::PKey::PKeyError
- OpenSSL::PKey::RSA
- OpenSSL::PKey::RSAError
- OpenSSL::Random
- OpenSSL::Random::RandomError
- OpenSSL::SSL
- OpenSSL::SSL::Nonblock
- OpenSSL::SSL::SSLContext
- OpenSSL::SSL::SSLError
- OpenSSL::SSL::SSLErrorWaitReadable
- OpenSSL::SSL::SSLErrorWaitWritable
- OpenSSL::SSL::SSLServer
- OpenSSL::SSL::SSLSocket
- OpenSSL::SSL::Session
- OpenSSL::SSL::Session::SessionError
- OpenSSL::SSL::SocketForwarder
- OpenSSL::X509
- OpenSSL::X509::Attribute
- OpenSSL::X509::AttributeError
- OpenSSL::X509::CRL
- OpenSSL::X509::CRLError
- OpenSSL::X509::Certificate
- OpenSSL::X509::CertificateError
- OpenSSL::X509::Extension
- OpenSSL::X509::ExtensionError
- OpenSSL::X509::ExtensionFactory
- OpenSSL::X509::Name
- OpenSSL::X509::Name::RFC2253DN
- OpenSSL::X509::NameError
- OpenSSL::X509::Request
- OpenSSL::X509::RequestError
- OpenSSL::X509::Revoked
- OpenSSL::X509::RevokedError
- OpenSSL::X509::Store
- OpenSSL::X509::StoreContext
- OpenSSL::X509::StoreError
- unknown

No matching classes.

# OpenSSL::X509::Extension

### Public Class Methods

OpenSSL::X509::Extension.new asn1 click to toggle source
OpenSSL::X509::Extension.new name, value
OpenSSL::X509::Extension.new name, value, critical

Creates an X509 extension.

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

                   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;
    }

### Public Instance Methods

critical=(p1) click to toggle source

                   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?() click to toggle source

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

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

oid() click to toggle source

                   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=(p1) click to toggle source

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

        s = StringValuePtr(oid);
        obj = OBJ_txt2obj(s, 0);
        if(!obj) obj = OBJ_txt2obj(s, 1);
        if(!obj) ossl_raise(eX509ExtError, NULL);
        GetX509Ext(self, ext);
        X509_EXTENSION_set_object(ext, obj);

        return oid;
    }

to_a() click to toggle source

                   # File openssl/lib/openssl/x509.rb, line 61
    def to_a
      [ self.oid, self.value, self.critical? ]
    end

to_der() click to toggle source

                   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_h() click to toggle source

                   # File openssl/lib/openssl/x509.rb, line 57
    def to_h # {"oid"=>sn|ln, "value"=>value, "critical"=>true|false}
      {"oid"=>self.oid,"value"=>self.value,"critical"=>self.critical?}
    end

to_s() click to toggle source

                   # File openssl/lib/openssl/x509.rb, line 50
    def to_s # "oid = critical, value"
      str = self.oid
      str << " = "
      str << "critical, " if self.critical?
      str << self.value.gsub(/\n/, ", ")
    end

value() click to toggle source

                   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))
            M_ASN1_OCTET_STRING_print(out, ext->value);
        ret = ossl_membio2str(out);

        return ret;
    }

value=(p1) click to toggle source

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

        data = ossl_to_der_if_possible(data);
        StringValue(data);
        if(!(s = OPENSSL_malloc(RSTRING_LEN(data))))
            ossl_raise(eX509ExtError, "malloc error");
        memcpy(s, RSTRING_PTR(data), RSTRING_LEN(data));
        if(!(asn1s = ASN1_OCTET_STRING_new())){
            OPENSSL_free(s);
            ossl_raise(eX509ExtError, NULL);
        }
        if(!M_ASN1_OCTET_STRING_set(asn1s, s, RSTRING_LENINT(data))){
            OPENSSL_free(s);
            ASN1_OCTET_STRING_free(asn1s);
            ossl_raise(eX509ExtError, NULL);
        }
        OPENSSL_free(s);
        GetX509Ext(self, ext);
        X509_EXTENSION_set_data(ext, asn1s);

        return data;
    }