Class: OpenSSL::X509::Certificate

Inherits:
Object
  • Object
show all
Includes:
Marshal, Extension::AuthorityInfoAccess, Extension::AuthorityKeyIdentifier, Extension::CRLDistributionPoints, Extension::SubjectKeyIdentifier
Defined in:
ext/openssl/ossl_x509cert.c,
lib/openssl/x509.rb,
ext/openssl/ossl_x509cert.c

Overview

Implementation of an X.509 certificate as specified in RFC 5280. Provides access to a certificate’s attributes and allows certificates to be read from a string, but also supports the creation of new certificates from scratch.

Reading a certificate from a file

Certificate is capable of handling DER-encoded certificates and certificates encoded in OpenSSL’s PEM format.

raw = File.binread "cert.cer" # DER- or PEM-encoded
certificate = OpenSSL::X509::Certificate.new raw

Saving a certificate to a file

A certificate may be encoded in DER format

cert = ...
File.open("cert.cer", "wb") { |f| f.print cert.to_der }

or in PEM format

cert = ...
File.open("cert.pem", "wb") { |f| f.print cert.to_pem }

X.509 certificates are associated with a private/public key pair, typically a RSA, DSA or ECC key (see also OpenSSL::PKey::RSA, OpenSSL::PKey::DSA and OpenSSL::PKey::EC), the public key itself is stored within the certificate and can be accessed in form of an OpenSSL::PKey. Certificates are typically used to be able to associate some form of identity with a key pair, for example web servers serving pages over HTTPs use certificates to authenticate themselves to the user.

The public key infrastructure (PKI) model relies on trusted certificate authorities (“root CAs”) that issue these certificates, so that end users need to base their trust just on a selected few authorities that themselves again vouch for subordinate CAs issuing their certificates to end users.

The OpenSSL::X509 module provides the tools to set up an independent PKI, similar to scenarios where the ‘openssl’ command line tool is used for issuing certificates in a private PKI.

Creating a root CA certificate and an end-entity certificate

First, we need to create a “self-signed” root certificate. To do so, we need to generate a key first. Please note that the choice of “1” as a serial number is considered a security flaw for real certificates. Secure choices are integers in the two-digit byte range and ideally not sequential but secure random numbers, steps omitted here to keep the example concise.

root_key = OpenSSL::PKey::RSA.new 2048 # the CA's public/private key
root_ca = OpenSSL::X509::Certificate.new
root_ca.version = 2 # cf. RFC 5280 - to make it a "v3" certificate
root_ca.serial = 1
root_ca.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby CA"
root_ca.issuer = root_ca.subject # root CA's are "self-signed"
root_ca.public_key = root_key.public_key
root_ca.not_before = Time.now
root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60 # 2 years validity
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = root_ca
ef.issuer_certificate = root_ca
root_ca.add_extension(ef.create_extension("basicConstraints","CA:TRUE",true))
root_ca.add_extension(ef.create_extension("keyUsage","keyCertSign, cRLSign", true))
root_ca.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
root_ca.add_extension(ef.create_extension("authorityKeyIdentifier","keyid:always",false))
root_ca.sign(root_key, OpenSSL::Digest.new('SHA256'))

The next step is to create the end-entity certificate using the root CA certificate.

key = OpenSSL::PKey::RSA.new 2048
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = 2
cert.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby certificate"
cert.issuer = root_ca.subject # root CA is the issuer
cert.public_key = key.public_key
cert.not_before = Time.now
cert.not_after = cert.not_before + 1 * 365 * 24 * 60 * 60 # 1 years validity
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = root_ca
cert.add_extension(ef.create_extension("keyUsage","digitalSignature", true))
cert.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
cert.sign(root_key, OpenSSL::Digest.new('SHA256'))

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extension::AuthorityInfoAccess

#ca_issuer_uris, #ocsp_uris

Methods included from Extension::Helpers

#find_extension

Methods included from Extension::CRLDistributionPoints

#crl_uris

Methods included from Extension::AuthorityKeyIdentifier

#authority_key_identifier

Methods included from Extension::SubjectKeyIdentifier

#subject_key_identifier

Methods included from Marshal

#_dump, included

Constructor Details

#newObject #new(string) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'ext/openssl/ossl_x509cert.c', line 110

static VALUE
ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
{
    BIO *in;
    X509 *x509, *x509_orig = RTYPEDDATA_DATA(self);
    VALUE arg;

    rb_check_frozen(self);
    if (rb_scan_args(argc, argv, "01", &arg) == 0) {
        /* create just empty X509Cert */
        return self;
    }
    arg = ossl_to_der_if_possible(arg);
    in = ossl_obj2bio(&arg);
    x509 = d2i_X509_bio(in, NULL);
    if (!x509) {
        OSSL_BIO_reset(in);
        x509 = PEM_read_bio_X509(in, NULL, NULL, NULL);
    }
    BIO_free(in);
    if (!x509)
        ossl_raise(eX509CertError, "PEM_read_bio_X509");

    RTYPEDDATA_DATA(self) = x509;
    X509_free(x509_orig);

    return self;
}

Class Method Details

.OpenSSL::X509::Certificate.load(string) ⇒ Array .OpenSSL::X509::Certificate.load(file) ⇒ Array

Read the chained certificates from the given input. Supports both PEM and DER encoded certificates.

PEM is a text format and supports more than one certificate.

DER is a binary format and only supports one certificate.

If the file is empty, or contains only unrelated data, an OpenSSL::X509::CertificateError exception will be raised.

Overloads:

  • .OpenSSL::X509::Certificate.load(string) ⇒ Array

    Returns:

    • (Array)
  • .OpenSSL::X509::Certificate.load(file) ⇒ Array

    Returns:

    • (Array)


867
868
869
870
871
872
873
# File 'ext/openssl/ossl_x509cert.c', line 867

static VALUE
ossl_x509_load(VALUE klass, VALUE buffer)
{
    BIO *in = ossl_obj2bio(&buffer);

    return rb_ensure(load_chained_certificates, (VALUE)in, load_chained_certificates_ensure, (VALUE)in);
}

.load_file(path) ⇒ Object



369
370
371
# File 'lib/openssl/x509.rb', line 369

def self.load_file(path)
  load(File.binread(path))
end

Instance Method Details

#==(cert2) ⇒ Object

Compares the two certificates. Note that this takes into account all fields, not just the issuer name and the serial number.

This method uses X509_cmp() from OpenSSL, which compares certificates based on their cached DER encodings. The comparison can be unreliable if a certificate is incomplete.

See also the man page X509_cmp(3).



681
682
683
684
685
686
687
688
689
690
691
692
# File 'ext/openssl/ossl_x509cert.c', line 681

static VALUE
ossl_x509_eq(VALUE self, VALUE other)
{
    X509 *a, *b;

    GetX509(self, a);
    if (!rb_obj_is_kind_of(other, cX509Cert))
        return Qfalse;
    GetX509(other, b);

    return !X509_cmp(a, b) ? Qtrue : Qfalse;
}

#add_extension(extension) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'ext/openssl/ossl_x509cert.c', line 653

static VALUE
ossl_x509_add_extension(VALUE self, VALUE extension)
{
    X509 *x509;
    X509_EXTENSION *ext;

    GetX509(self, x509);
    ext = GetX509ExtPtr(extension);
    if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
        ossl_raise(eX509CertError, NULL);
    }

    return extension;
}

#check_private_key(key) ⇒ Object

Returns true if key is the corresponding private key to the Subject Public Key Information, false otherwise.



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'ext/openssl/ossl_x509cert.c', line 581

static VALUE
ossl_x509_check_private_key(VALUE self, VALUE key)
{
    X509 *x509;
    EVP_PKEY *pkey;

    /* not needed private key, but should be */
    pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
    GetX509(self, x509);
    if (!X509_check_private_key(x509, pkey)) {
        ossl_clear_error();
        return Qfalse;
    }

    return Qtrue;
}

#extensionsArray

Returns:

  • (Array)


602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'ext/openssl/ossl_x509cert.c', line 602

static VALUE
ossl_x509_get_extensions(VALUE self)
{
    X509 *x509;
    int count, i;
    VALUE ary;

    GetX509(self, x509);
    count = X509_get_ext_count(x509);
    ary = rb_ary_new_capa(count);
    for (i=0; i<count; i++) {
        const X509_EXTENSION *ext = X509_get_ext(x509, i);
        rb_ary_push(ary, ossl_x509ext_new(ext));
    }

    return ary;
}

#extensions=(ary) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'ext/openssl/ossl_x509cert.c', line 624

static VALUE
ossl_x509_set_extensions(VALUE self, VALUE ary)
{
    X509 *x509;
    X509_EXTENSION *ext;
    long i;

    Check_Type(ary, T_ARRAY);
    /* All ary's members should be X509Extension */
    for (i=0; i<RARRAY_LEN(ary); i++) {
        OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Ext);
    }
    GetX509(self, x509);
    for (i = X509_get_ext_count(x509); i > 0; i--)
        X509_EXTENSION_free(X509_delete_ext(x509, 0));
    for (i=0; i<RARRAY_LEN(ary); i++) {
        ext = GetX509ExtPtr(RARRAY_AREF(ary, i));
        if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext */
            ossl_raise(eX509CertError, "X509_add_ext");
        }
    }

    return ary;
}

#initialize_copy(other) ⇒ Object

:nodoc:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/openssl/ossl_x509cert.c', line 140

static VALUE
ossl_x509_copy(VALUE self, VALUE other)
{
    X509 *a, *b, *x509;

    rb_check_frozen(self);
    if (self == other) return self;

    GetX509(self, a);
    GetX509(other, b);

    x509 = X509_dup(b);
    if (!x509) ossl_raise(eX509CertError, NULL);

    DATA_PTR(self) = x509;
    X509_free(a);

    return self;
}

#inspectObject



349
350
351
352
353
354
355
356
# File 'lib/openssl/x509.rb', line 349

def inspect
  "#<#{self.class}: " \
    "subject=#{subject.inspect}, " \
    "issuer=#{issuer.inspect}, " \
    "serial=#{serial.inspect}, " \
    "not_before=#{not_before.inspect rescue "(error)"}, " \
    "not_after=#{not_after.inspect rescue "(error)"}>"
end

#issuerObject



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/openssl/ossl_x509cert.c', line 380

static VALUE
ossl_x509_get_issuer(VALUE self)
{
    X509 *x509;
    const X509_NAME *name;

    GetX509(self, x509);
    if(!(name = X509_get_issuer_name(x509))) { /* NO DUP - don't free! */
        ossl_raise(eX509CertError, NULL);
    }

    return ossl_x509name_new(name);
}

#issuer=(name) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
# File 'ext/openssl/ossl_x509cert.c', line 398

static VALUE
ossl_x509_set_issuer(VALUE self, VALUE issuer)
{
    X509 *x509;

    GetX509(self, x509);
    if (!X509_set_issuer_name(x509, GetX509NamePtr(issuer))) { /* DUPs name */
        ossl_raise(eX509CertError, NULL);
    }

    return issuer;
}

#not_afterTime

Returns:

  • (Time)


454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'ext/openssl/ossl_x509cert.c', line 454

static VALUE
ossl_x509_get_not_after(VALUE self)
{
    X509 *x509;
    const ASN1_TIME *asn1time;

    GetX509(self, x509);
    if (!(asn1time = X509_get0_notAfter(x509))) {
        ossl_raise(eX509CertError, NULL);
    }

    return asn1time_to_time(asn1time);
}

#not_after=(time) ⇒ Time

Returns:

  • (Time)


472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'ext/openssl/ossl_x509cert.c', line 472

static VALUE
ossl_x509_set_not_after(VALUE self, VALUE time)
{
    X509 *x509;
    ASN1_TIME *asn1time;

    GetX509(self, x509);
    asn1time = ossl_x509_time_adjust(NULL, time);
    if (!X509_set1_notAfter(x509, asn1time)) {
        ASN1_TIME_free(asn1time);
        ossl_raise(eX509CertError, "X509_set_notAfter");
    }
    ASN1_TIME_free(asn1time);

    return time;
}

#not_beforeTime

Returns:

  • (Time)


415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'ext/openssl/ossl_x509cert.c', line 415

static VALUE
ossl_x509_get_not_before(VALUE self)
{
    X509 *x509;
    const ASN1_TIME *asn1time;

    GetX509(self, x509);
    if (!(asn1time = X509_get0_notBefore(x509))) {
        ossl_raise(eX509CertError, NULL);
    }

    return asn1time_to_time(asn1time);
}

#not_before=(time) ⇒ Time

Returns:

  • (Time)


433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'ext/openssl/ossl_x509cert.c', line 433

static VALUE
ossl_x509_set_not_before(VALUE self, VALUE time)
{
    X509 *x509;
    ASN1_TIME *asn1time;

    GetX509(self, x509);
    asn1time = ossl_x509_time_adjust(NULL, time);
    if (!X509_set1_notBefore(x509, asn1time)) {
        ASN1_TIME_free(asn1time);
        ossl_raise(eX509CertError, "X509_set_notBefore");
    }
    ASN1_TIME_free(asn1time);

    return time;
}

#pretty_print(q) ⇒ Object



358
359
360
361
362
363
364
365
366
367
# File 'lib/openssl/x509.rb', line 358

def pretty_print(q)
  q.object_group(self) {
    q.breakable
    q.text 'subject='; q.pp self.subject; q.text ','; q.breakable
    q.text 'issuer='; q.pp self.issuer; q.text ','; q.breakable
    q.text 'serial='; q.pp self.serial; q.text ','; q.breakable
    q.text 'not_before='; q.pp self.not_before; q.text ','; q.breakable
    q.text 'not_after='; q.pp self.not_after
  }
end

#public_keyObject



493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'ext/openssl/ossl_x509cert.c', line 493

static VALUE
ossl_x509_get_public_key(VALUE self)
{
    X509 *x509;
    EVP_PKEY *pkey;

    GetX509(self, x509);
    if (!(pkey = X509_get_pubkey(x509))) { /* adds an reference */
        ossl_raise(eX509CertError, NULL);
    }

    return ossl_pkey_wrap(pkey);
}

#public_key=(key) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'ext/openssl/ossl_x509cert.c', line 511

static VALUE
ossl_x509_set_public_key(VALUE self, VALUE key)
{
    X509 *x509;
    EVP_PKEY *pkey;

    GetX509(self, x509);
    pkey = GetPKeyPtr(key);
    ossl_pkey_check_public_key(pkey);
    if (!X509_set_pubkey(x509, pkey))
        ossl_raise(eX509CertError, "X509_set_pubkey");
    return key;
}

#serialInteger

Returns:



294
295
296
297
298
299
300
301
302
# File 'ext/openssl/ossl_x509cert.c', line 294

static VALUE
ossl_x509_get_serial(VALUE self)
{
    X509 *x509;

    GetX509(self, x509);

    return asn1integer_to_num(X509_get_serialNumber(x509));
}

#serial=(integer) ⇒ Integer

Returns:



308
309
310
311
312
313
314
315
316
317
# File 'ext/openssl/ossl_x509cert.c', line 308

static VALUE
ossl_x509_set_serial(VALUE self, VALUE num)
{
    X509 *x509;

    GetX509(self, x509);
    X509_set_serialNumber(x509, num_to_asn1integer(num, X509_get_serialNumber(x509)));

    return num;
}

#sign(key, digest) ⇒ self

Returns:

  • (self)


529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'ext/openssl/ossl_x509cert.c', line 529

static VALUE
ossl_x509_sign(VALUE self, VALUE key, VALUE digest)
{
    X509 *x509;
    EVP_PKEY *pkey;
    const EVP_MD *md;
    VALUE md_holder;

    pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
    /* NULL needed for some key types, e.g. Ed25519 */
    md = NIL_P(digest) ? NULL : ossl_evp_md_fetch(digest, &md_holder);
    GetX509(self, x509);
    if (!X509_sign(x509, pkey, md))
        ossl_raise(eX509CertError, "X509_sign");

    return self;
}

#signature_algorithmString

Returns the signature algorithm used to sign this certificate. This returns the algorithm name found in the TBSCertificate structure, not the outer Certificate structure.

Returns the long name of the signature algorithm, or the dotted decimal notation if OpenSSL does not define a long name for it.

Returns:

  • (String)


330
331
332
333
334
335
336
337
338
339
# File 'ext/openssl/ossl_x509cert.c', line 330

static VALUE
ossl_x509_get_signature_algorithm(VALUE self)
{
    X509 *x509;
    const ASN1_OBJECT *obj;

    GetX509(self, x509);
    X509_ALGOR_get0(&obj, NULL, NULL, X509_get0_tbs_sigalg(x509));
    return ossl_asn1obj_to_string_long_name(obj);
}

#subjectObject



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'ext/openssl/ossl_x509cert.c', line 345

static VALUE
ossl_x509_get_subject(VALUE self)
{
    X509 *x509;
    const X509_NAME *name;

    GetX509(self, x509);
    if (!(name = X509_get_subject_name(x509))) { /* NO DUP - don't free! */
        ossl_raise(eX509CertError, NULL);
    }

    return ossl_x509name_new(name);
}

#subject=(name) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/openssl/ossl_x509cert.c', line 363

static VALUE
ossl_x509_set_subject(VALUE self, VALUE subject)
{
    X509 *x509;

    GetX509(self, x509);
    if (!X509_set_subject_name(x509, GetX509NamePtr(subject))) { /* DUPs name */
        ossl_raise(eX509CertError, NULL);
    }

    return subject;
}

#tbs_bytesString

Returns the DER-encoded bytes of the certificate’s to be signed certificate. This is mainly useful for validating embedded certificate transparency signatures.

Returns:

  • (String)


701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'ext/openssl/ossl_x509cert.c', line 701

static VALUE
ossl_x509_tbs_bytes(VALUE self)
{
    X509 *x509;
    int len;
    unsigned char *p0;
    VALUE str;

    GetX509(self, x509);
    len = i2d_re_X509_tbs(x509, NULL);
    if (len <= 0) {
        ossl_raise(eX509CertError, "i2d_re_X509_tbs");
    }
    str = rb_str_new(NULL, len);
    p0 = (unsigned char *)RSTRING_PTR(str);
    if (i2d_re_X509_tbs(x509, &p0) <= 0) {
        ossl_raise(eX509CertError, "i2d_re_X509_tbs");
    }
    ossl_str_adjust(str, p0);

    return str;
}

#to_derString

Returns:

  • (String)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/openssl/ossl_x509cert.c', line 164

static VALUE
ossl_x509_to_der(VALUE self)
{
    X509 *x509;
    VALUE str;
    long len;
    unsigned char *p;

    GetX509(self, x509);
    if ((len = i2d_X509(x509, NULL)) <= 0)
        ossl_raise(eX509CertError, NULL);
    str = rb_str_new(0, len);
    p = (unsigned char *)RSTRING_PTR(str);
    if (i2d_X509(x509, &p) <= 0)
        ossl_raise(eX509CertError, NULL);
    ossl_str_adjust(str, p);

    return str;
}

#to_pemString Also known as: to_s

Returns:

  • (String)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/openssl/ossl_x509cert.c', line 188

static VALUE
ossl_x509_to_pem(VALUE self)
{
    X509 *x509;
    BIO *out;
    VALUE str;

    GetX509(self, x509);
    out = BIO_new(BIO_s_mem());
    if (!out) ossl_raise(eX509CertError, NULL);

    if (!PEM_write_bio_X509(out, x509)) {
        BIO_free(out);
        ossl_raise(eX509CertError, NULL);
    }
    str = ossl_membio2str(out);

    return str;
}

#to_textString

Returns:

  • (String)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/openssl/ossl_x509cert.c', line 212

static VALUE
ossl_x509_to_text(VALUE self)
{
    X509 *x509;
    BIO *out;
    VALUE str;

    GetX509(self, x509);

    out = BIO_new(BIO_s_mem());
    if (!out) ossl_raise(eX509CertError, NULL);

    if (!X509_print(out, x509)) {
        BIO_free(out);
        ossl_raise(eX509CertError, NULL);
    }
    str = ossl_membio2str(out);

    return str;
}

#verify(key) ⇒ Object

Verifies the signature of the certificate, with the public key key. key must be an instance of OpenSSL::PKey.



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'ext/openssl/ossl_x509cert.c', line 554

static VALUE
ossl_x509_verify(VALUE self, VALUE key)
{
    X509 *x509;
    EVP_PKEY *pkey;

    GetX509(self, x509);
    pkey = GetPKeyPtr(key);
    ossl_pkey_check_public_key(pkey);
    switch (X509_verify(x509, pkey)) {
      case 1:
        return Qtrue;
      case 0:
        ossl_clear_error();
        return Qfalse;
      default:
        ossl_raise(eX509CertError, NULL);
    }
}

#versionInteger

Returns:



259
260
261
262
263
264
265
266
267
# File 'ext/openssl/ossl_x509cert.c', line 259

static VALUE
ossl_x509_get_version(VALUE self)
{
    X509 *x509;

    GetX509(self, x509);

    return LONG2NUM(X509_get_version(x509));
}

#version=(integer) ⇒ Integer

Returns:



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'ext/openssl/ossl_x509cert.c', line 273

static VALUE
ossl_x509_set_version(VALUE self, VALUE version)
{
    X509 *x509;
    long ver;

    if ((ver = NUM2LONG(version)) < 0) {
        ossl_raise(eX509CertError, "version must be >= 0!");
    }
    GetX509(self, x509);
    if (!X509_set_version(x509, ver)) {
        ossl_raise(eX509CertError, NULL);
    }

    return version;
}