Skip to content

Commit

Permalink
Added Values::Cert#as_json (#47)
Browse files Browse the repository at this point in the history
* Added specs for `Values::Cert#as_json`.
  • Loading branch information
AI-Mozi authored Jul 31, 2023
1 parent 6b198c2 commit 755c53d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
19 changes: 18 additions & 1 deletion lib/ronin/recon/values/cert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#

require 'ronin/recon/value'
require 'ronin/support/crypto'

module Ronin
module Recon
Expand All @@ -42,7 +43,7 @@ class Cert < Value
# The decoded X509 certificate.
#
def initialize(cert)
@cert = cert
@cert = Support::Crypto::Cert(cert)
end

#
Expand Down Expand Up @@ -128,6 +129,22 @@ def to_s
@cert.to_s
end

#
# Converts the certificate to a hash of attributes
#
# @return [Hash]
#
def as_json
{
subject: @cert.subject.to_h,
issuer: @cert.issuer.to_h,
extensions: @cert.extensions_hash,
serial: @cert.serial,
not_before: @cert.not_before,
not_after: @cert.not_after
}
end

#
# Returns the type or kind of recon value.
#
Expand Down
20 changes: 18 additions & 2 deletions spec/values/cert_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require 'spec_helper'
require 'ronin/recon/values/cert'
require 'ronin/support/crypto'

require 'openssl'

describe Ronin::Recon::Values::Cert do
let(:fixtures_dir) { File.join(__dir__,'fixtures') }
let(:cert_path) { File.join(fixtures_dir,'example.crt') }
let(:cert) do
OpenSSL::X509::Certificate.new(File.read(cert_path))
Ronin::Support::Crypto::Cert(OpenSSL::X509::Certificate.new(File.read(cert_path)))
end

subject { described_class.new(cert) }
Expand Down Expand Up @@ -98,7 +99,22 @@
end
end

describe "#as_json"
describe "#as_json" do
let(:cert_hash) {
{
subject: cert.subject.to_h,
issuer: cert.issuer.to_h,
extensions: cert.extensions_hash,
serial: cert.serial,
not_before: cert.not_before,
not_after: cert.not_after
}
}

it "must return the Hash version of #cert" do
expect(subject.as_json).to eq(cert_hash)
end
end

describe ".value_type" do
subject { described_class }
Expand Down

0 comments on commit 755c53d

Please sign in to comment.