From df82147fd041d73820a35ee524bec558e866f6b0 Mon Sep 17 00:00:00 2001 From: John McCrae Date: Sun, 15 May 2022 11:14:44 +0600 Subject: [PATCH] refactoring to correct for not finding certs correctly in some cases. Signed-off-by: John McCrae --- lib/win32/certstore.rb | 10 ++-------- lib/win32/certstore/store_base.rb | 10 +++++++--- spec/win32/assets/billg.pfx | Bin 0 -> 2597 bytes spec/win32/unit/certstore_spec.rb | 15 +++++++++++++-- 4 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 spec/win32/assets/billg.pfx diff --git a/lib/win32/certstore.rb b/lib/win32/certstore.rb index cacc42a..d4f48c6 100644 --- a/lib/win32/certstore.rb +++ b/lib/win32/certstore.rb @@ -84,7 +84,7 @@ def get(certificate_thumbprint) def get!(certificate_thumbprint) cert_pem = cert_get(certificate_thumbprint) - raise ArgumentError, "Unable to retrieve the certificate" if cert_pem.empty? + raise ArgumentError, "Unable to retrieve the certificate" if cert_pem.empty? || cert_pem == "Certificate Not Found" cert_pem end @@ -118,13 +118,7 @@ def get_thumbprint(search_token) # @param request[thumbprint] of certificate # @return [true, false] only true or false def valid?(certificate_thumbprint) - cert_validate(certificate_thumbprint).yield_self do |x| - if x.is_a?(TrueClass) || x.is_a?(FalseClass) - x - else - false - end - end + cert_validate(certificate_thumbprint) 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 bf0b52c..0f37db2 100644 --- a/lib/win32/certstore/store_base.rb +++ b/lib/win32/certstore/store_base.rb @@ -146,10 +146,11 @@ def cert_delete(store_handler, certificate_thumbprint) def cert_validate(certificate_thumbprint) validate_thumbprint(certificate_thumbprint) thumbprint = update_thumbprint(certificate_thumbprint) - cert_pem = get_cert_pem(thumbprint) + return cert_pem if cert_pem == "Certificate Not Found" cert_pem = format_pem(cert_pem) - verify_certificate(cert_pem) + result = verify_certificate(cert_pem) + result == false ? "Certificate Has Expired" : result end # Search certificate from open certificate store and return list @@ -184,13 +185,16 @@ def cert_lookup_by_token(search_token, store_name: @store_name, store_location: end powershell_cmd = <<~EOH $result = Get-ChildItem -Path Cert:\\#{converted_store}\\#{store_name} | Where-Object { $_.Subject -match "#{search_token.strip}" } | Select-Object Thumbprint + if ([string]::IsNullOrEmpty($result)){ + return "Certificate Not Found" + } return $result[0].Thumbprint EOH powershell_exec!(powershell_cmd, :powershell, timeout: timeout).result rescue ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed - return "Certificate not found" + raise ArgumentError, "PowerShell threw an error retreiving the certificate. You asked for a cert with this Search Token : #{search_token}, located in this store : #{store_name}, at this location : #{store_location}" end # To close and destroy pointer of open certificate store handler diff --git a/spec/win32/assets/billg.pfx b/spec/win32/assets/billg.pfx new file mode 100644 index 0000000000000000000000000000000000000000..3efdf25cb3bd0f741804f2c4e96fddd3072597ab GIT binary patch literal 2597 zcmZWqc{tQtAODRRGsZ4;sb&g;>}H6ul@!IDOm-t{W8X6eKLPWAF z%aASGWGVY4*$OeQ;m+;3%kw_(bDr-xpYP{$zRN%7e9;tcaSjj|O@U8A;c~HtvETVX zFc6Ib?}Jd_-E6u8O@Rvhj|2rE6wZA#g>#o}TTq_AqIkJM95f2#08N1~(FiE_-!wZp zKZNVd8J_fG47Y3!4%h&M0%2rd$Q@Nm*WV7;1N8f)%p~!LfV=&Vk!F6wVqhID_fyFb^)=a1o7fZWTF1%7QEzxf1 zobAl58(qD0v_xLi(D=zgV(V&0$rQ@&p-a2X?o0Vj&}ey(XitPbf^r6`*`snA@>?$J zy{raSkJswdh1Xp-Z*rQ&e8|)`gs>3OhIQ9SD{dMvd{Mhn$)x|g-ME(O#~WiGf@6(I zVYA#Gp^Kvsrkba&@i->4p3u6bH0(KJEu)FQM<%F1L$q5Ax|S~TQ0()k23yoY%}ayQ zQg30EZXPGI0^YQrqQzDHwk;JZUg2x~&X_!cpc39p^73dolgn-(#8@#_(^X5^t@BUQ zQ1L5NqMDSz(Ax|j3%XE#*OOg_@4ao~sB>@X#%S>lm8_1d`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<>FqeV`qY4jWZzFM zTL#7J$`>x;))nR*jYmO=SUIm%W{Je8y3Gym$P1JtQ=MR)70-$*@^z*W`_Igfy;W^N z*K(JTdxUAFy3@Bd&+vVfsk)qoZ#56Nfx}mDBb^@`l9b^~Hwjyh&&!x1g>oWRBc<+` zDO_rDIM$YL^F#o*QbJbBelFyw&HFuKfotW}N_uI*Pa`jbgPGZ`qN}En4FcbGM2j3d zB0`w>j?zz*2$pZ9 rVCdu^*vz*Kr65i&7UXD%yqn0=SM9FGz_B^zq2jI1?sxNlKGZ(|`C*Nt literal 0 HcmV?d00001 diff --git a/spec/win32/unit/certstore_spec.rb b/spec/win32/unit/certstore_spec.rb index 51acfcc..1c03229 100644 --- a/spec/win32/unit/certstore_spec.rb +++ b/spec/win32/unit/certstore_spec.rb @@ -270,9 +270,20 @@ describe "#cert_lookup_by_token" do context "when searching for a certificate that does not exist" do let(:store_name) { "root" } - it "returns a message of Certificate not found" do + it "returns a message of Certificate Not Found" do store = certstore.open(store_name, store_location: store_location) - expect(store.cert_lookup_by_token("nunya")).to eql("Certificate not found") + expect(store.cert_lookup_by_token("nunya")).to eql("Certificate Not Found") + end + end + + context "when searching for a certificate that does exist" do + before(:each) do + allow_any_instance_of(certbase).to receive(:cert_lookup_by_token).and_return("506285bbf4f30446d93e3120e2bffa71b7b9acf2") + end + let(:store_name) { "root" } + it "returns a message of Certificate Not Found" do + store = certstore.open(store_name, store_location: store_location) + expect(store.cert_lookup_by_token("BillG")).to eql("506285bbf4f30446d93e3120e2bffa71b7b9acf2") end end end