Skip to content

Commit

Permalink
Add subject_alt_names kwarg to Cert.generate (#445).
Browse files Browse the repository at this point in the history
  • Loading branch information
moozzi authored and postmodern committed Nov 30, 2023
1 parent 3663072 commit 382c3e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/ronin/support/crypto/cert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'ronin/support/crypto/openssl'
require 'ronin/support/crypto/key/rsa'
require 'ronin/support/crypto/key/ec'
require 'ronin/support/network/ip'

module Ronin
module Support
Expand Down Expand Up @@ -279,6 +280,9 @@ def self.load_file(path)
# @param [Boolean] ca
# Indicates whether to add the basicConstraints extension.
#
# @param [Array<String>, nil] subject_alt_names
# List of subject alt names to add into subjectAltName extension.
#
# @param [Symbol] signing_hash
# The hashing algorithm to use to sign the new certificate.
#
Expand Down Expand Up @@ -356,6 +360,7 @@ def self.generate(version: 2,
ca_cert: nil,
ca_key: nil,
ca: false,
subject_alt_names: nil,
signing_hash: :sha256)
cert = new

Expand All @@ -375,6 +380,19 @@ def self.generate(version: 2,
else cert.subject
end

if subject_alt_names
subject_alt_name = subject_alt_names.map { |alt_name|
if alt_name.match?(Network::IP::REGEX)
"IP:#{alt_name}"
else
"DNS:#{alt_name}"
end
}.join(', ')

extensions ||= {}
extensions = extensions.merge('subjectAltName' => subject_alt_name)
end

if ca
extensions ||= {}
extensions = extensions.merge('basicConstraints' => ['CA:TRUE', true])
Expand Down
15 changes: 15 additions & 0 deletions spec/crypto/cert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,21 @@
expect(subject.extension_names).to match_array(["subjectAltName", "basicConstraints"])
end
end

context "when subject_alt_names kwarg is given" do
subject do
Ronin::Support::Crypto::Cert.generate(
key: rsa_key,
extensions: { 'basicConstraints' => ['CA:TRUE', true] },
subject_alt_names: ["localhost", "127.0.0.1"]
)
end

it "must not override extensions from extensions kwarg" do
expect(subject.extension_names).to match_array(["subjectAltName", "basicConstraints"])
expect(subject.extension_value("subjectAltName")).to eq("DNS:localhost, IP Address:127.0.0.1")
end
end
end

it "must default #not_before to Time.now" do
Expand Down

0 comments on commit 382c3e1

Please sign in to comment.