Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
create a self-signed certificate using ruby-openssl
# Copyright © 2020 Nicky Peeters
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'rubygems'
require 'openssl'
key = OpenSSL::PKey::RSA.new(1024)
public_key = key.public_key
subject = "/C=BE/O=Test/OU=Test/CN=Test"
cert = OpenSSL::X509::Certificate.new
cert.subject = cert.issuer = OpenSSL::X509::Name.parse(subject)
cert.not_before = Time.now
cert.not_after = Time.now + 365 * 24 * 60 * 60
cert.public_key = public_key
cert.serial = 0x0
cert.version = 2
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = cert
cert.extensions = [
ef.create_extension("basicConstraints","CA:TRUE", true),
ef.create_extension("subjectKeyIdentifier", "hash"),
# ef.create_extension("keyUsage", "cRLSign,keyCertSign", true),
]
cert.add_extension ef.create_extension("authorityKeyIdentifier",
"keyid:always,issuer:always")
cert.sign key, OpenSSL::Digest::SHA1.new
puts cert.to_pem
@SimonKaluza

This comment has been minimized.

Copy link

SimonKaluza commented Aug 21, 2014

This is exactly what I was looking for when I googled "generate self signed certificate ruby." Thanks so much for this!

@AnatoliiD

This comment has been minimized.

Copy link

AnatoliiD commented Nov 28, 2014

awesome :)

@pdougall1

This comment has been minimized.

Copy link

pdougall1 commented Nov 29, 2014

Thanks for saving me hours to figure out confusing key cert stuff!

@SuperDaveAU

This comment has been minimized.

Copy link

SuperDaveAU commented Jan 18, 2015

Brilliant, thanks a bunch. (Y)

@gmanfunky

This comment has been minimized.

Copy link

gmanfunky commented Apr 2, 2015

While this isn't immediately insecure, standard cert defaults these days are 2048 bit keys and SHA256 digests.

key = OpenSSL::PKey::RSA.new(2048) 
...
cert.sign key, OpenSSL::Digest::SHA256.new

Thanks for sharing!

@bsutherland

This comment has been minimized.

Copy link

bsutherland commented Jan 13, 2017

Why are some of the extensions set directly on cert.extensions, and one is added using cert.add_extension? In the example in the documentation, they only use add_extension: https://ruby-doc.org/stdlib-2.4.0/libdoc/openssl/rdoc/OpenSSL/X509/Certificate.html

@mltsy

This comment has been minimized.

Copy link

mltsy commented Jun 9, 2017

Thanks! I made it into a gem 😄 https://github.com/mltsy/fauthentic

@LyzioOh

This comment has been minimized.

Copy link

LyzioOh commented Jan 17, 2018

⚠️
OpenSSL::PKey::RSA.new(1024)
1024 bits keys are not secure anymore as well as 2048. 4096 bits are fine.

@kaspernj

This comment has been minimized.

Copy link

kaspernj commented May 3, 2018

How do you get the CA from that?

cert.pem returns the server certificate, right? What about the client certificate? How do you get that?

@bobbytables

This comment has been minimized.

Copy link

bobbytables commented Apr 13, 2019

Thank you!

@antonyedwards

This comment has been minimized.

Copy link

antonyedwards commented Mar 28, 2020

Thanks. This code was exactly what we needed and we've been using it for a while now. Unfortunately our company just ran a BlackDuck scan on our code and this is now being flagged as "unlicensed" because there is no explicit license statement on this page. I don't suppose you could give it a license (e.g. MIT?) so we can continue to use it? Many thanks.

@nickyp

This comment has been minimized.

Copy link
Owner Author

nickyp commented Jun 23, 2020

@antonyedwards I've added the MIT licence. Enjoy!

@antonyedwards

This comment has been minimized.

Copy link

antonyedwards commented Jun 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.