Ruby gem for managing the core functions outlined in RFC-3280 for PKI
Latest commit 6dd483b Nov 30, 2016 @cchandler committed on GitHub Merge pull request #47 from elijh/noactivesupport
remove dependency on ActiveRecord and ActiveSupport
Failed to load latest commit information.
lib Add proper Date calculations Jun 21, 2016
spec Add proper Date calculations Jun 21, 2016
.gitignore add coveralls Jan 11, 2014
Gemfile remove ActiveModel dependency Jun 21, 2016
README.rdoc add coveralls Jan 11, 2014
Rakefile remove ActiveModel dependency Jun 21, 2016
VERSION.yml Version bump to 0.2.0 Sep 16, 2012
certificate_authority.gemspec rerun rake gemspec:generate Jun 21, 2016


CertificateAuthority - Because it shouldn't be this damned complicated

Build Status Code Climate Code Coverage

This is meant to provide a (more) programmer-friendly implementation of all the basic functionality contained in RFC-3280 to implement your own certificate authority.

You can generate root certificates, intermediate certificates, and terminal certificates. You can also generate/manage Certificate Revocation Lists (CRLs) and Online Certificate Status Protocol (OCSP) messages.

Because this library is built using the native Ruby bindings for OpenSSL it also supports PKCS#11 cryptographic hardware for secure maintenance of private key materials.

So you want to maintain a certificate authority root

Let's suppose hypothetically you want to be able to issue and manage your own certificates. This section is meant to outline the basic functions you'll need(optionally want) to support. Not everyone is out to be in total compliance with WebTrust or Mozilla’s rules for CA inclusion.

The three primary elements to be aware of are:

Certificate Authority

These are the functions primarily related to the signing, issuance, and revocation of certificates.

Registration Authority

These are the functions primarily related to registering and requesting certificates and vetting of the entities requesting certification.

Validation Authority

These are the functions related to verifying the status of certificates out in the wild. Mostly CRLs and OCSP related functions.

Establishing a new root in software

Let's look at a complete example for generating a new root certificate. Assuming that you don't have a PKCS#11 hardware token available (lists coming…) we'll have to store this safe.

Generating a self-signed root certificate is fairly easy:

require 'certificate_authority'
root =
root.subject.common_name= ""
root.signing_entity = true
signing_profile = {"extensions" => {"keyUsage" => {"usage" => ["critical", "keyCertSign"] }} }

The required elements for the gem at this time are a common name for the subject and a serial number for the certificate. Since this is our self-signed root we're going to give it the first serial available of 1. Because certificate_authority is not designed to manage the issuance lifecycle you'll be expected to store serial numbers yourself.

Next, after taking care of required fields, we will require key material for the new certificate. There's a convenience method made available on the key_material object for generating new keys. The private key will be available in:


and the public key:


Make sure to save the private key somewhere safe!

Lastly, we declare that the certificate we're about to sign is itself a signing entity so we can continue on and sign other certificates.

Creating a new intermediate

Maybe you don't want to actually sign certificates with your super-secret root certificate. This is actually how a good number of most public certificate authorities do it. Rather than sign with the primary root, they generate an intermediate root that is then responsible for signing the final certificates. If you wanted to create an intermediate root certificate you would do something like the following:

intermediate =
intermediate.subject.common_name= "My snazzy intermediate!"
intermediate.signing_entity = true
intermediate.parent = root
signing_profile = {"extensions" => {"keyUsage" => {"usage" => ["critical", "keyCertSign"] }} }

All we have to do is create another certificate like we did with the root. In this example we gave it the next available serial number, which for us, was 2. We then generate (and save!) key material for this new entity. Even the signing_entity is set to true so this certificate can sign other certificates. The difference here is that the parent field is set to the root. Going forward, whatever entity you want to sign a certificate, you set that entity to be the parent. In this case, our root will be responsible for signing this intermediate when we call sign!.

Creating new certificates (in general)

Now that we have a root certificate (and possibly an intermediate) we can sign end-user certificates. It is, perhaps unsurprisingly, similar to all the others:

plain_cert =
plain_cert.subject.common_name= ""
plain_cert.parent = root # or intermediate

That's all there is to it! In this example we generate the key material ourselves, but it's possible for the end-user to generate certificate signing request (CSR) that we can then parse and consume automatically (coming soon). To get the PEM formatted certificate for the user you would need to call:


to get the certificate body.

Signing Profiles

Creating basic certificates is all well and good, but maybe you want more signing control. certificate_authority supports the idea of signing profiles. These are hashes containing values that sign! will use to merge in additional control options for setting extensions on the certificate.

Here's an example of a full signing profile for most of the common V3 extensions:

 signing_profile = {
   "extensions" => {
     "basicConstraints" => {"ca" => false},
     "crlDistributionPoints" => {"uri" => "" },
     "subjectKeyIdentifier" => {},
     "authorityKeyIdentifier" => {},
     "authorityInfoAccess" => {"ocsp" => ["http://youFillThisOut/ocsp/"] },
     "keyUsage" => {"usage" => ["digitalSignature","nonRepudiation"] },
     "extendedKeyUsage" => {"usage" => [ "serverAuth","clientAuth"]},
     "subjectAltName" => {"uris" => ["http://subdomains.youFillThisOut/"]},
     "certificatePolicies" => {
     "policy_identifier" => "", "cps_uris" => ["", ""], 
       "user_notice" => {
        "explicit_text" => "Explicit Text Here", 
       "organization" => "Organization name",
       "notice_numbers" => "1,2,3,4"

Using a signing profile is done this way:


At that point all the configuration options will be merged into the extensions.

Basic Constraints

The basic constraints extension allows you to control whether or not a certificate can sign other certificates.


If this value is true then this certificate has the authority to sign additional certificates.


This is the maximum length of the chain-of-trust. For instance, if an intermediate certificate has a pathlen of 1 then it can sign additional certificates, but it cannot create another signing entity because the total chain-of-trust would have a length greater than 1.

CRL Distribution Points

This extension controls where a conformant client can go to obtain a list of certificate revocation information. At this point certificate_authority only supports a list of URIs. The formal RFC however provides for the ability to provide a URI and an issuer identifier that allows a different signing entity to generate/sign the CRL.


The URI in subject alternative name format of the URI endpoint. Example: “

Subject Key Identifier

This extension is required to be present, but doesn't offer any configurable parameters. Directly from the RFC:

The subject key identifier extension provides a means of identifying
certificates that contain a particular public key.

To facilitate certification path construction, this extension MUST
appear in all conforming CA certificates, that is, all certificates
including the basic constraints extension (section where
the value of cA is TRUE.  The value of the subject key identifier
MUST be the value placed in the key identifier field of the Authority
Key Identifier extension (section of certificates issued by
the subject of this certificate.

Authority Key Identifier

Just like the subject key identifier, this is required under most circumstances and doesn't contain any meaningful configuration options. From the RFC:

The keyIdentifier field of the authorityKeyIdentifier extension MUST
be included in all certificates generated by conforming CAs to
facilitate certification path construction.  There is one exception;
where a CA distributes its public key in the form of a "self-signed"
certificate, the authority key identifier MAY be omitted.  The
signature on a self-signed certificate is generated with the private
key associated with the certificate's subject public key.  (This
proves that the issuer possesses both the public and private keys.)
In this case, the subject and authority key identifiers would be
identical, but only the subject key identifier is needed for
certification path building.

Authority Info Access

The authority info access extension allows a CA to sign a certificate with information a client can use to get up-to-the-minute status information on a signed certificate. This takes the form of an OCSP (Online Certificate Status Protocol) endpoints.


This is an array of URIs specifying possible endpoints that will be able to provide a signed response. certificate_authority has an OCSP message handler for parsing OCSP requests and generating OCSP signed responses.

Key Usage

This extension contains a list of the functions this certificate is allowed to participate in.


An array of OIDs in string format. The acceptable values are specified by OpenSSL and are: digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyAgreement, keyCertSign, cRLSign, encipherOnly and decipherOnly.

Extended Key Usage

This one is like key usage, but allows for certain application specific purposes. It's generally only present in end-user certificates.


An array of OIDs in string format. The only ones with practical significance at this point are: serverAuth, clientAuth, and codeSigning.

Subject Alternative Name

If the certificate needs to work for multiple domains then you can specify the others for which it is valid in the subject alternative name field.


An array of full URIs for other common names this certificate should be valid for. For instance, if you want and to share the same cert you would place both in the uris attribute of the subject alternative name.

Certificate Policies

This is one of the most esoteric of the extensions. This allows a conformant certificate authority to embed signing policy information into the certificate body. Public certificate authorities are required to maintain a Certificate Practice Statement in accordance with RFC 2527.

These CPSs define what vetting criteria and maintenance practices are required to issue, maintain, and revoke a certificate. While it might be overkill for private certificates, if you wanted to make an actual public CA you would need to put together a practice statement and embed it in certificates you issue.


This is an arbitrary OID (that you make up!) that uniquely represents the policy you are enforcing for whatever kind of certificate this is meant to be.


This is an array of URIs where a client or human can go to get information related to your certification practice.


This is a nested field containing explicit human readable text if you want to embed a notice in the certificate body related to certification practices. It contains nested attributes of explicit_text for the notice, organization and notice_numbers. Refer to the RFC for specific implications of how these are set, but whether or not browsers implement the correct specified behavior for their presence is another issue.

Certificate Signing Requests (CSRs)

If you want certificate requestors to be able to request certificates without moving the private key you'll need to generate a CSR and submit it to the certificate authority.

Here's an example of using certificate_authority to generate a CSR.

csr =
dn =
dn.common_name = "localhost"
csr.distinguished_name = dn
k =
csr.key_material = k
csr.digest = "SHA256"

Similarly, reading a CSR in is as simple as providing the PEM formatted version to SigningRequest.from_x509_csr.

csr = CertificateAuthority::SigningRequest.from_x509_csr(@pem_csr)

Once you have the CSR in the form of a SigningRequest you can transform it to a Certificate with to_cert. At this point it works just like any other certificate. You'll have to provide a serial number to actually sign it.

Certificate Revocation Lists (CRLs)

Revocation lists let clients know when a certificate in the wild should no longer be trusted.

Like end-user certificates, CRLs have to be signed by a signing authority to be valid. Additionally, you will need to furnish a nextUpdate value that indicates to the client when it should look for updates to the CRL and how long it should consider a cached value valid.

Ideally you would place the result CRL somewhere generally accessible on the Internet and reference the URI in the crlDistributionPoints extension on issued certificates.

crl =
crl << certificate # Some CertificateAuthority::Certificate
crl << serial_number # Also works with plain CertificateAuthority::SerialNumber
crl.parent = root_certificate # A valid root
crl.next_update = (60 * 60 * 10) # 10 Hours

OCSP Support

OCSP is the Online Certificate Status Protocol. It provides a mechanism to query an authority to see if a certificate is still valid without downloading an entire CRL. To use this mechanism you provide a URI in the Authority Information Access extension. If a client wishes to check the validity of a certificate they can query this endpoint. This request will only contain serial numbers, so you'll need to uniquely identify your authority in the AIA path.

If a client sends you a DER encoded OCSP request you can read it out via OCSPRequestReader

ocsp_request_reader = CertificateAuthority::OCSPRequestReader.from_der(@ocsp_request.to_der)

Then, you can construct a response like this

response_builder = CertificateAuthority::OCSPResponseBuilder.from_request_reader(ocsp_request_reader)
response_builder.parent = root
response = response_builder.build_response # Returns OpenSSL::OCSP::Response

The response builder will copy a (possible) nonce from the request. By default, the OCSPResponseBuilder will say that every certificate is GOOD. You should definitely override this if you plan on revoking certificates. If you want to override this you'll need to supply a proc/lambda that takes a serial number and returns an array of status and reason.

response_builder = CertificateAuthority::OCSPResponseBuilder.from_request_reader(ocsp_request_reader)
response_builder.verification_mechanism = lambda {|certid|
response_builder.parent = root
response = response_builder.build_response # Response will say everything is revoked for unspecified reasons

Lastly, you can configure a nextUpdate time in the response. This is the length of time for which a client may consider this response valid. The default is 15 minutes.

response_builder.next_update = 30 * 60 # 30 minutes

PKCS#11 Support

If you happen to have a PKCS#11 compliant hardware token you can use certificate_authority to maintain private key materials in hardware security modules. At this point the scope of operating that hardware is out of scope of this README but it's there and it is supported.

To configure a certificate to utilize PKCS#11 instead of in memory keys all you need to do is:

root =
root.subject.common_name= ""
root.signing_entity = true

key_material_in_hardware =
key_material_in_hardware.token_id = "46"
key_material_in_hardware.pkcs11_lib = "/usr/lib/"
key_material_in_hardware.openssl_pkcs11_engine_lib = "/usr/lib/engines/" = "11111111"

root.key_material = key_material_in_hardware

Your current version of OpenSSL must include dynamic engine support and you will need to have OpenSSL PKCS#11 engine support. You will also require the actual PKCS#11 driver from the hardware manufacturer. As of today the only tokens I've gotten to work are:


Released by Aladdin (now SafeNet Inc.). I have only had success with the version 4 and 5 (32 bit only) copy of the driver. The newer authentication client released by SafeNet appears to be completely broken for interacting with the tokens outside of SafeNet's own tools. If anyone has a different experience I'd like to hear from you.

ACS CryptoMate

Also a 32-bit only driver. You'll have to jump through some hoops to get the Linux PKCS#11 driver but it works surprisingly well. It also appears to support symmetric key operations in hardware.

Your company

Do you make a PKCS#11 device? I'd love to get it working but I probably can't afford your device. Get in touch with me and if you're willing to loan me one for a week I can get it listed.

Also of note, I have gotten these to work with 32-bit copies of Ubuntu 10.10 and pre-Snow Leopard versions of OS X. If you are running Snow Leopard you're out of luck since none of the companies I've contacted make a 64 bit driver.

Hopefully in the future

  • More PKCS#11 hardware (I need driver support from the manufacturers)


  • Support for working with CSRs to request & issue certificates

  • OCSP support

Misc notes

  • Firefox will complain about root/intermediate certificates unless both digitalSignature and keyEncipherment are specified as keyUsage attributes. Thanks diogomonica

Special thanks and Contributions

  • Diogo Monica @diogo

  • Justin Cummins @sul3n3t

  • @databus23

  • Colin Jones @trptcolin

  • Eric Monti @emonti

  • TJ Vanderpoel @bougyman


Written by Chris Chandler(

Released under the MIT License:

Main page:

Issue tracking: