win32-certstore 0.5.3 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b391e4d81e324162538a2a3644aea74f0da1f8679409733a58dcf4c590734a6c
4
- data.tar.gz: 5ed7821ec5bffe58cb09608cfc008b287874b5ddafe8556662fa27f42a25c1b1
3
+ metadata.gz: 63c8c4f2aaa89a78a8123d98079710df407a3930b0f4b5a6ef3ff2232c11ba05
4
+ data.tar.gz: 8641dfff337fe7b702783becfcfbfbba6b2a66af189eeb511897c2f0cb5e7d6c
5
5
  SHA512:
6
- metadata.gz: fc5c77cd659695ac3a58480ab875a6c5daa2b93cd83e45f1fcdfcd1bd89e0cf9653d38d63ae9e5260ecff90a7277f6ee600b91137665d53e672fbb77e1543cfb
7
- data.tar.gz: 5b6a8025b85ae8ce026a9a7b603a35137000525ba182bbbcc8fc9667fb78dd6cf179ad1cfbb1cbdcdf582dd4749f82795a17df01181720d7f77a36a4477dc639
6
+ metadata.gz: 4f255e439feee57642565bd9fca87f19140b5f95c38eed0a1e8621326749dd5274c0b72c8211bddfbec265820bff4b8d2ad9460ebbf5c30fa1d6607a7e81f204
7
+ data.tar.gz: 4efd363fb264fc8501f0f9e105e79f7537319838aafc0cb751387a73360386186d326dedd51522907aa131fe74c1b3ba07bd7b3f6f16a1283f5f59203d96a6a7
@@ -78,6 +78,18 @@ module Win32
78
78
  cert_get(certificate_thumbprint)
79
79
  end
80
80
 
81
+ # Returns a filepath to a PKCS12 container. The filepath is in a temporary folder so normal housekeeping by the OS should clear it.
82
+ # However, you should delete it yourself anyway.
83
+ # @param certificate_thumbprint [String] Is the thumbprint of the pfx blob you want to capture
84
+ # @param store_location: [String] A location in the Cert store where the pfx is located, typically 'LocalMachine'
85
+ # @param export_password: [String] The password to export with. P12 objects are an encrypted container that have a private key in \
86
+ # them and a password is required to export them.
87
+ # @param output_path: [String] The path where the you want P12 exported to.
88
+ # @return [Object] of certificate set in PKSC12 format at the path specified above
89
+ def get_pfx(certificate_thumbprint, store_location: @store_location, export_password:, output_path: "")
90
+ get_cert_pfx(certificate_thumbprint, store_location: store_location, export_password: export_password, output_path: output_path)
91
+ end
92
+
81
93
  # Returns all the certificates in a store
82
94
  # @param [nil]
83
95
  # @return [Array] array of certificates list
@@ -21,20 +21,52 @@ module Win32
21
21
  class Certstore
22
22
  module Mixin
23
23
  module Helper
24
- # PSCommand to search certificate from thumbprint and convert in pem
25
- def cert_ps_cmd(thumbprint, store_name, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE)
24
+ # PSCommand to search certificate from thumbprint and either turn it into a pem or return a path to a pfx object
25
+ def cert_ps_cmd(thumbprint, store_location: "LocalMachine", export_password: "1234", output_path: "")
26
26
  <<-EOH
27
- $content = $null
28
- $cert = Get-ChildItem Cert:\\'#{store_location}'\\'#{store_name}' -Recurse | Where { $_.Thumbprint -eq '#{thumbprint}' }
29
- if($cert -ne $null)
30
- {
31
- $content = @(
32
- '-----BEGIN CERTIFICATE-----'
33
- [System.Convert]::ToBase64String($cert.RawData, 'InsertLineBreaks')
34
- '-----END CERTIFICATE-----'
35
- )
27
+ $cert = Get-ChildItem Cert:\'#{store_location}' -Recurse | Where { $_.Thumbprint -eq '#{thumbprint}' }
28
+
29
+ # The function and the code below test to see if a) the cert has a private key and b) it has a
30
+ # Enhanced Usage of Client Auth. Those 2 attributes would mean this is a pfx-able object
31
+ function test_cert_values{
32
+ $usagelist = ($cert).EnhancedKeyUsageList
33
+ foreach($use in $usagelist){
34
+ if($use.FriendlyName -like "Client Authentication" ){
35
+ return $true
36
+ }
37
+ }
38
+ return $false
39
+ }
40
+
41
+ $result = test_cert_values
42
+
43
+ $output_path = "#{output_path}"
44
+ if([string]::IsNullOrEmpty($output_path)){
45
+ $temproot = [System.IO.Path]::GetTempPath()
46
+ }
47
+ else{
48
+ $temproot = $output_path
49
+ }
50
+
51
+ if((($cert).HasPrivateKey) -and ($result -eq $true)){
52
+ $file_name = '#{thumbprint}'
53
+ $file_path = $(Join-Path -Path $temproot -ChildPath "$file_name.pfx")
54
+ $mypwd = ConvertTo-SecureString -String '#{export_password}' -Force -AsPlainText
55
+ $cert | Export-PfxCertificate -FilePath $file_path -Password $mypwd | Out-Null
56
+ $file_path
57
+ }
58
+ else {
59
+ $content = $null
60
+ if($cert -ne $null)
61
+ {
62
+ $content = @(
63
+ '-----BEGIN CERTIFICATE-----'
64
+ [System.Convert]::ToBase64String($cert.RawData, 'InsertLineBreaks')
65
+ '-----END CERTIFICATE-----'
66
+ )
67
+ }
68
+ $content
36
69
  }
37
- $content
38
70
  EOH
39
71
  end
40
72
 
@@ -236,9 +236,19 @@ module Win32
236
236
  else
237
237
  "CurrentUser"
238
238
  end
239
- get_data = powershell_exec!(cert_ps_cmd(thumbprint, store_name, store_location: converted_store))
239
+ get_data = powershell_exec!(cert_ps_cmd(thumbprint, store_location: converted_store))
240
+ get_data.stdout
241
+ end
242
+
243
+ # Get PFX object
244
+ def get_cert_pfx(thumbprint, store_location: CERT_SYSTEM_STORE_LOCAL_MACHINE, export_password:, output_path: )
245
+ converted_store = if store_location == CERT_SYSTEM_STORE_LOCAL_MACHINE
246
+ "LocalMachine"
247
+ else
248
+ "CurrentUser"
249
+ end
250
+ get_data = powershell_exec!(cert_ps_cmd(thumbprint, export_password: export_password, store_location: converted_store, output_path: output_path))
240
251
  get_data.stdout
241
- # get_data.result
242
252
  end
243
253
 
244
254
  # Format pem
@@ -1,6 +1,6 @@
1
1
  module Win32
2
2
  class Certstore
3
- VERSION = "0.5.3".freeze
3
+ VERSION = "0.6.1".freeze
4
4
  MAJOR, MINOR, TINY = VERSION.split(".")
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: win32-certstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-01 00:00:00.000000000 Z
11
+ date: 2021-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler