Created
April 8, 2021 06:55
-
-
Save BranLiang/8319d1343093cf2a24d16a4da236c831 to your computer and use it in GitHub Desktop.
create a self-signed elliptic curve certificate using ruby-openssl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # frozen_string_literal: true | |
| require 'openssl' | |
| private_ec = OpenSSL::PKey::EC.new('prime256v1') | |
| private_ec.generate_key | |
| puts "Your private key:" | |
| puts private_ec.to_pem | |
| public_ec = OpenSSL::PKey::EC.new(private_ec.public_key.group) | |
| public_ec.public_key = private_ec.public_key | |
| public_key = public_ec | |
| subject = "/O=COMPANY/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 + 100 * 365 * 24 * 60 * 60 # 100 years later | |
| 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 private_ec, OpenSSL::Digest.new("SHA256") | |
| puts "\nYour public certificate:" | |
| puts cert.to_pem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment