diff options
author | elijah <elijah@riseup.net> | 2016-10-05 14:35:56 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2016-10-05 14:35:56 -0700 |
commit | 7abfbd6abae14fa6a72350f7b75268ff561354ee (patch) | |
tree | af5c969c905a8d2a95f2b2aa7c4dd6f4b8763126 /vendor/acme-client/lib/acme/client/self_sign_certificate.rb | |
parent | cc57bc6c0ff99d88f3bfeff1b04297e9b91e6988 (diff) | |
parent | f95e08ef7d8defbde4a19e138b1ac4ebc9677669 (diff) |
Merge branch 'develop'
# Conflicts:
# lib/leap_cli/version.rb
Diffstat (limited to 'vendor/acme-client/lib/acme/client/self_sign_certificate.rb')
-rw-r--r-- | vendor/acme-client/lib/acme/client/self_sign_certificate.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/acme-client/lib/acme/client/self_sign_certificate.rb b/vendor/acme-client/lib/acme/client/self_sign_certificate.rb new file mode 100644 index 0000000..2e7d98c --- /dev/null +++ b/vendor/acme-client/lib/acme/client/self_sign_certificate.rb @@ -0,0 +1,60 @@ +class Acme::Client::SelfSignCertificate + attr_reader :private_key, :subject_alt_names, :not_before, :not_after + + extend Forwardable + def_delegators :certificate, :to_pem, :to_der + + def initialize(subject_alt_names:, not_before: default_not_before, not_after: default_not_after, private_key: generate_private_key) + @private_key = private_key + @subject_alt_names = subject_alt_names + @not_before = not_before + @not_after = not_after + end + + def certificate + @certificate ||= begin + certificate = generate_certificate + + extension_factory = generate_extension_factory(certificate) + subject_alt_name_entry = subject_alt_names.map { |d| "DNS: #{d}" }.join(',') + subject_alt_name_extension = extension_factory.create_extension('subjectAltName', subject_alt_name_entry) + certificate.add_extension(subject_alt_name_extension) + + certificate.sign(private_key, digest) + end + end + + private + + def generate_private_key + OpenSSL::PKey::RSA.new(2048) + end + + def default_not_before + Time.now - 3600 + end + + def default_not_after + Time.now + 30 * 24 * 3600 + end + + def digest + OpenSSL::Digest::SHA256.new + end + + def generate_certificate + certificate = OpenSSL::X509::Certificate.new + certificate.not_before = not_before + certificate.not_after = not_after + certificate.public_key = private_key.public_key + certificate.version = 2 + certificate + end + + def generate_extension_factory(certificate) + extension_factory = OpenSSL::X509::ExtensionFactory.new + extension_factory.subject_certificate = certificate + extension_factory.issuer_certificate = certificate + extension_factory + end +end |