Skip to content

Instantly share code, notes, and snippets.

@nickyp
Last active September 4, 2023 23:46
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 19 You must be signed in to fork a gist
  • Save nickyp/886884 to your computer and use it in GitHub Desktop.
Save nickyp/886884 to your computer and use it in GitHub Desktop.
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
Copy link

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

@AnatoliiD
Copy link

awesome :)

@pdougall1
Copy link

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

@SuperDaveAU
Copy link

Brilliant, thanks a bunch. (Y)

@gmanfunky
Copy link

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!

Copy link

ghost 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
Copy link

mltsy commented Jun 9, 2017

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

@LyzioOh
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
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
Copy link

Thank you!

@antonyedwards
Copy link

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
Copy link
Author

nickyp commented Jun 23, 2020

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

@antonyedwards
Copy link

antonyedwards commented Jun 23, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment