diff --git a/lib/win32/certstore.rb b/lib/win32/certstore.rb index 363297c..326e4eb 100644 --- a/lib/win32/certstore.rb +++ b/lib/win32/certstore.rb @@ -78,6 +78,17 @@ def get(certificate_thumbprint, store_name: @store_name, store_location: @store_ cert_get(certificate_thumbprint, store_name: store_name, store_location: store_location) end + # Return `OpenSSL::X509` certificate object if present otherwise raise a "Certificate not found!" error + # @param request [thumbprint] of certificate + # @return [Object] of certificates in OpenSSL::X509 format + def get!(certificate_thumbprint, store_name: @store_name, store_location: @store_location) + cert_pem = cert_get(certificate_thumbprint, store_name: store_name, store_location: store_location) + + raise ArgumentError, "Unable to retrieve the certificate" if cert_pem.empty? + + cert_pem + end + # Returns all the certificates in a store # @param [nil] # @return [Array] array of certificates list @@ -103,7 +114,13 @@ def search(search_token) # @param request[thumbprint] of certificate # @return [true, false] only true or false def valid?(certificate_thumbprint, store_location: "", store_name: "") - cert_validate(certificate_thumbprint, store_location: store_location, store_name: store_name) + cert_validate(certificate_thumbprint, store_location: store_location, store_name: store_name).yield_self do |x| + if x.is_a?(TrueClass) || x.is_a?(FalseClass) + x + else + false + end + end end # To close and destroy pointer of open certificate store handler diff --git a/lib/win32/certstore/store_base.rb b/lib/win32/certstore/store_base.rb index 0156a32..c7407ca 100644 --- a/lib/win32/certstore/store_base.rb +++ b/lib/win32/certstore/store_base.rb @@ -92,13 +92,8 @@ def cert_get(certificate_thumbprint, store_name:, store_location:) thumbprint = update_thumbprint(certificate_thumbprint) cert_pem = get_cert_pem(thumbprint, store_name: store_name, store_location: store_location) cert_pem = format_pem(cert_pem) - if cert_pem.empty? - raise ArgumentError, "Unable to retrieve the certificate" - end - unless cert_pem.empty? - build_openssl_obj(cert_pem) - end + cert_pem.empty? ? cert_pem : build_openssl_obj(cert_pem) end # Listing certificate of open certstore and return list in json @@ -147,6 +142,8 @@ def cert_validate(certificate_thumbprint, store_location:, store_name:) thumbprint = update_thumbprint(certificate_thumbprint) cert_pem = get_cert_pem(thumbprint, store_name: store_name, store_location: store_location) cert_pem = format_pem(cert_pem) + return "Certificate not found" if cert_pem.empty? + verify_certificate(cert_pem) end @@ -223,8 +220,6 @@ def update_thumbprint(certificate_thumbprint) # Verify OpenSSL::X509::Certificate object def verify_certificate(cert_pem) - return "Certificate not found" if cert_pem.empty? - valid_duration?(build_openssl_obj(cert_pem)) end diff --git a/spec/win32/functional/win32/certstore_spec.rb b/spec/win32/functional/win32/certstore_spec.rb index 064a44e..a41a94a 100644 --- a/spec/win32/functional/win32/certstore_spec.rb +++ b/spec/win32/functional/win32/certstore_spec.rb @@ -38,10 +38,29 @@ @store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: "My") end + # passing invalid thumbprint + it "returns nil if certificate not found" do + thumbprint = "b1bc968bd4f49d622aa89a81f2150152a41d829cab" + cert_obj = @store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: "My") + expect(cert_obj).to be_empty + end + end + + describe "#get!" do + before { add_cert } + let(:cert_pem) { File.read('.\spec\win32\assets\GlobalSignRootCA.pem') } + + # passing valid thumbprint + it "returns the certificate_object if found" do + thumbprint = "b1bc968bd4f49d622aa89a81f2150152a41d829c" + expect(@store).to receive(:cert_get).with(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: "My").and_return(cert_pem) + @store.get!(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: "My") + end + # passing invalid thumbprint it "returns ArgumentError if certificate not found" do thumbprint = "b1bc968bd4f49d622aa89a81f2150152a41d829cab" - expect { @store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: "My") }.to raise_error(ArgumentError) + expect { @store.get!(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: "My") }.to raise_error(ArgumentError) end end diff --git a/spec/win32/unit/certstore_spec.rb b/spec/win32/unit/certstore_spec.rb index 27af68c..a090fa2 100644 --- a/spec/win32/unit/certstore_spec.rb +++ b/spec/win32/unit/certstore_spec.rb @@ -131,9 +131,10 @@ before(:each) do allow_any_instance_of(certbase).to receive(:get_cert_pem).and_return("") end - it "raises ArgumentError" do + it "returns empty string" do store = certstore.open(store_name) - expect { store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) }.to raise_error(ArgumentError, "Unable to retrieve the certificate") + cert_obj = store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) + expect(cert_obj).to be_empty end end @@ -259,9 +260,10 @@ before(:each) do allow_any_instance_of(certbase).to receive(:get_cert_pem).and_return("") end - it "raises Error" do + it "returns empty string" do store = certstore.open(store_name) - expect { store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) }.to raise_error(ArgumentError, "Unable to retrieve the certificate") + cert_obj = store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) + expect(cert_obj).to be_empty end end end @@ -298,9 +300,9 @@ before(:each) do allow_any_instance_of(certbase).to receive(:get_cert_pem).and_return("") end - it "returns Certificate not found" do + it "returns false" do store = certstore.open(store_name) - expect(store.valid?(thumbprint)).to eql("Certificate not found") + expect(store.valid?(thumbprint)).to eql(false) end end @@ -597,9 +599,10 @@ before(:each) do allow_any_instance_of(certbase).to receive(:get_cert_pem).and_return("") end - it "returns nil" do + it "returns empty string" do store = certstore.open(store_name, store_location: store_location) - expect { store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) }.to raise_error(ArgumentError, "Unable to retrieve the certificate") + cert_obj = store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) + expect(cert_obj).to be_empty end end @@ -732,9 +735,10 @@ before(:each) do allow_any_instance_of(certbase).to receive(:get_cert_pem).and_return("") end - it "returns nil" do + it "returns empty string" do store = certstore.open(store_name, store_location: store_location) - expect { store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) }.to raise_error(ArgumentError, "Unable to retrieve the certificate") + cert_obj = store.get(thumbprint, store_location: CERT_SYSTEM_STORE_CURRENT_USER, store_name: store_name) + expect(cert_obj).to be_empty end end end @@ -775,9 +779,9 @@ before(:each) do allow_any_instance_of(certbase).to receive(:get_cert_pem).and_return("") end - it "returns Certificate not found" do + it "returns false" do store = certstore.open(store_name, store_location: store_location) - expect(store.valid?(thumbprint)).to eql("Certificate not found") + expect(store.valid?(thumbprint)).to eql(false) end end