certstore_c 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +202 -0
- data/README.md +39 -0
- data/Rakefile +17 -0
- data/appveyor.yml +26 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/certstore_c.gemspec +43 -0
- data/example/open_tls_socket.rb +33 -0
- data/ext/certstore/certstore.c +20 -0
- data/ext/certstore/certstore.h +46 -0
- data/ext/certstore/certstore_util.c +23 -0
- data/ext/certstore/extconf.rb +20 -0
- data/ext/certstore/loader.c +314 -0
- data/lib/certstore.rb +7 -0
- data/lib/certstore/loader.rb +35 -0
- data/lib/certstore/openssl/loader.rb +65 -0
- data/lib/certstore/version.rb +19 -0
- metadata +122 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 96c6bbfa6206bad18270e7309092a3137a1f241c0b67d249b2c63635012ed601
|
|
4
|
+
data.tar.gz: 39d67816ca8dce4dd98130f630b1281d3dca7a648d0c42d5d5897ddc6e456395
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c31add0c2e14fd046c50dacff252cf03ce4cf5203a1007d03af69d027dd0ae48e5b4138867e86b2e7dc715d7fbbbaf5b78b28b6ba98a678799a96a1a9adcc3ab
|
|
7
|
+
data.tar.gz: 1897ca62202a239e0039a702eb24b03a6a105b2588ecd0fb9d21fe9173388a9914122d00aeb072013af8d6234e273e53a39b8244da41ff70d68de6dfbb4d5ade
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Certstore
|
|
2
|
+
|
|
3
|
+
[](https://ci.appveyor.com/project/cosmo0920/certstore-c/branch/master)
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
* Windows Vista/Windows Server 2008 or later.
|
|
8
|
+
* gcc and g++ from MSYS2 for building C/C++ extension.
|
|
9
|
+
* Ruby 2.4 or later with MSYS2.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add this line to your application's Gemfile:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem 'certstore_c'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
And then execute:
|
|
20
|
+
|
|
21
|
+
$ ridk exec bundle
|
|
22
|
+
|
|
23
|
+
Or install it yourself as:
|
|
24
|
+
|
|
25
|
+
$ ridk exec gem install certstore_c
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
Usage examples are found in [example directory](example).
|
|
30
|
+
|
|
31
|
+
## Development
|
|
32
|
+
|
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
34
|
+
|
|
35
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
36
|
+
|
|
37
|
+
## Contributing
|
|
38
|
+
|
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cosmo0920/certstore_c.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
2
|
+
require "bundler/setup"
|
|
3
|
+
require "rake/testtask"
|
|
4
|
+
|
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
|
6
|
+
t.libs << "test"
|
|
7
|
+
t.libs << "lib"
|
|
8
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
require "rake/extensiontask"
|
|
12
|
+
|
|
13
|
+
Rake::ExtensionTask.new("certstore") do |ext|
|
|
14
|
+
ext.lib_dir = "lib/certstore"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
task :default => [:clobber, :compile, :test]
|
data/appveyor.yml
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
version: '{build}'
|
|
2
|
+
|
|
3
|
+
# init:
|
|
4
|
+
# - ps: iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
|
|
5
|
+
|
|
6
|
+
install:
|
|
7
|
+
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
|
|
8
|
+
- ruby --version
|
|
9
|
+
- gem --version
|
|
10
|
+
- bundle --version
|
|
11
|
+
- bundle install
|
|
12
|
+
- ridk.cmd exec bundle exec rake compile
|
|
13
|
+
build: off
|
|
14
|
+
test_script:
|
|
15
|
+
# - bundle exec rake test
|
|
16
|
+
- bundle exec rake test TESTOPTS=-v
|
|
17
|
+
|
|
18
|
+
# https://www.appveyor.com/docs/installed-software/#ruby
|
|
19
|
+
environment:
|
|
20
|
+
matrix:
|
|
21
|
+
- ruby_version: "26-x64"
|
|
22
|
+
- ruby_version: "26"
|
|
23
|
+
- ruby_version: "25-x64"
|
|
24
|
+
- ruby_version: "25"
|
|
25
|
+
- ruby_version: "24-x64"
|
|
26
|
+
- ruby_version: "24"
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "certstore_c"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/certstore_c.gemspec
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "certstore/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "certstore_c"
|
|
8
|
+
spec.version = Certstore::VERSION
|
|
9
|
+
spec.authors = ["Hiroshi Hatake"]
|
|
10
|
+
spec.email = ["cosmo0920.wp@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Windows CertStore loader.}
|
|
13
|
+
spec.description = spec.summary
|
|
14
|
+
spec.homepage = "https://github.com/cosmo0920/certstore_c"
|
|
15
|
+
|
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
18
|
+
if spec.respond_to?(:metadata)
|
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
20
|
+
|
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/cosmo0920/certstore_c"
|
|
23
|
+
else
|
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
25
|
+
"public gem pushes."
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
30
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
31
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
32
|
+
end
|
|
33
|
+
spec.bindir = "exe"
|
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
35
|
+
spec.require_paths = ["lib"]
|
|
36
|
+
spec.license = "Apache-2.0"
|
|
37
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4".freeze)
|
|
38
|
+
|
|
39
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
40
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
|
41
|
+
spec.add_development_dependency "rake-compiler", "~> 1.0"
|
|
42
|
+
spec.add_development_dependency "test-unit", "~> 3.3.3"
|
|
43
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'socket'
|
|
2
|
+
require 'openssl'
|
|
3
|
+
require 'certstore'
|
|
4
|
+
|
|
5
|
+
if (ARGV.length < 3)
|
|
6
|
+
puts "host, port and store_name must be specified"
|
|
7
|
+
exit 1
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
host = ARGV[0]
|
|
11
|
+
port = ARGV[1]
|
|
12
|
+
store_name = ARGV[2]
|
|
13
|
+
thumbprint = ARGV[3]
|
|
14
|
+
|
|
15
|
+
cert_store = OpenSSL::X509::Store.new
|
|
16
|
+
loader = Certstore::OpenSSL::Loader.new(cert_store, store_name)
|
|
17
|
+
loader.load_cert_store
|
|
18
|
+
cert_store = loader.cert_store
|
|
19
|
+
|
|
20
|
+
socket = TCPSocket.open(host,port)
|
|
21
|
+
ssl_context = OpenSSL::SSL::SSLContext.new()
|
|
22
|
+
ssl_context.set_params({})
|
|
23
|
+
ssl_context.cert_store = cert_store
|
|
24
|
+
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
25
|
+
ssl_context.cert = loader.get_certificate(thumbprint) if thumbprint
|
|
26
|
+
ssl_context.ssl_version = :TLSv1_2
|
|
27
|
+
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
|
|
28
|
+
ssl_socket.sync_close = true
|
|
29
|
+
ssl_socket.connect
|
|
30
|
+
|
|
31
|
+
now = Time.now
|
|
32
|
+
ssl_socket.puts("[\"test.tag\", #{now.to_i}, {\"message\": \"Hi!!!!\"}]")
|
|
33
|
+
ssl_socket.close
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* certstore_c */
|
|
2
|
+
/* Licensed under the Apache License, Version 2.0 (the "License"); */
|
|
3
|
+
/* you may not use this file except in compliance with the License. */
|
|
4
|
+
/* You may obtain a copy of the License at */
|
|
5
|
+
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
|
6
|
+
/* Unless required by applicable law or agreed to in writing, software */
|
|
7
|
+
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
|
8
|
+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
|
9
|
+
/* See the License for the specific language governing permissions and */
|
|
10
|
+
/* limitations under the License. */
|
|
11
|
+
|
|
12
|
+
#include <certstore.h>
|
|
13
|
+
|
|
14
|
+
void
|
|
15
|
+
Init_certstore(void)
|
|
16
|
+
{
|
|
17
|
+
rb_mCertstore = rb_define_module("Certstore");
|
|
18
|
+
|
|
19
|
+
Init_certstore_loader(rb_mCertstore);
|
|
20
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* certstore_c */
|
|
2
|
+
/* Licensed under the Apache License, Version 2.0 (the "License"); */
|
|
3
|
+
/* you may not use this file except in compliance with the License. */
|
|
4
|
+
/* You may obtain a copy of the License at */
|
|
5
|
+
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
|
6
|
+
/* Unless required by applicable law or agreed to in writing, software */
|
|
7
|
+
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
|
8
|
+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
|
9
|
+
/* See the License for the specific language governing permissions and */
|
|
10
|
+
/* limitations under the License. */
|
|
11
|
+
|
|
12
|
+
#ifndef _CERTSTORE_H_
|
|
13
|
+
#define _CERTSTORE_H_
|
|
14
|
+
|
|
15
|
+
#include <ruby.h>
|
|
16
|
+
#include <ruby/encoding.h>
|
|
17
|
+
|
|
18
|
+
#ifdef __GNUC__
|
|
19
|
+
# include <w32api.h>
|
|
20
|
+
# define MINIMUM_WINDOWS_VERSION WindowsVista
|
|
21
|
+
#else /* __GNUC__ */
|
|
22
|
+
# define MINIMUM_WINDOWS_VERSION 0x0600 /* Vista */
|
|
23
|
+
#endif /* __GNUC__ */
|
|
24
|
+
|
|
25
|
+
#ifdef _WIN32_WINNT
|
|
26
|
+
# undef _WIN32_WINNT
|
|
27
|
+
#endif /* WIN32_WINNT */
|
|
28
|
+
#define _WIN32_WINNT MINIMUM_WINDOWS_VERSION
|
|
29
|
+
|
|
30
|
+
#define CERT_THUMBPRINT_STR_LENGTH 40
|
|
31
|
+
#define CERT_THUMBPRINT_SIZE (160 / 8)
|
|
32
|
+
|
|
33
|
+
#include <Wincrypt.h>
|
|
34
|
+
|
|
35
|
+
VALUE rb_mCertstore;
|
|
36
|
+
VALUE rb_cCertLoader;
|
|
37
|
+
VALUE rb_eCertLoaderError;
|
|
38
|
+
|
|
39
|
+
struct CertstoreLoader {
|
|
40
|
+
HCERTSTORE hStore;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
char* wstr_to_mbstr(UINT cp, const WCHAR *wstr, int clen);
|
|
44
|
+
void Init_certstore_loader(VALUE rb_mCertstore);
|
|
45
|
+
|
|
46
|
+
#endif // _WINEVT_H
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* certstore_c */
|
|
2
|
+
/* Licensed under the Apache License, Version 2.0 (the "License"); */
|
|
3
|
+
/* you may not use this file except in compliance with the License. */
|
|
4
|
+
/* You may obtain a copy of the License at */
|
|
5
|
+
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
|
6
|
+
/* Unless required by applicable law or agreed to in writing, software */
|
|
7
|
+
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
|
8
|
+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
|
9
|
+
/* See the License for the specific language governing permissions and */
|
|
10
|
+
/* limitations under the License. */
|
|
11
|
+
|
|
12
|
+
#include <certstore.h>
|
|
13
|
+
|
|
14
|
+
char*
|
|
15
|
+
wstr_to_mbstr(UINT cp, const WCHAR *wstr, int clen)
|
|
16
|
+
{
|
|
17
|
+
char *ptr;
|
|
18
|
+
int len = WideCharToMultiByte(cp, 0, wstr, clen, NULL, 0, NULL, NULL);
|
|
19
|
+
if (!(ptr = xmalloc(len))) return NULL;
|
|
20
|
+
WideCharToMultiByte(cp, 0, wstr, clen, ptr, len, NULL, NULL);
|
|
21
|
+
|
|
22
|
+
return ptr;
|
|
23
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require "mkmf"
|
|
2
|
+
require "rbconfig"
|
|
3
|
+
|
|
4
|
+
if RbConfig::CONFIG['host_os'] =~ /mingw/
|
|
5
|
+
$CFLAGS << ' -fno-omit-frame-pointer'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
libdir = RbConfig::CONFIG["libdir"]
|
|
9
|
+
includedir = RbConfig::CONFIG["includedir"]
|
|
10
|
+
|
|
11
|
+
dir_config("certstore", includedir, libdir)
|
|
12
|
+
|
|
13
|
+
have_library("crypt32")
|
|
14
|
+
have_func("PFXExportCertStoreEx", "Wincrypt.h")
|
|
15
|
+
|
|
16
|
+
$LDFLAGS << " -lCrypt32"
|
|
17
|
+
$CFLAGS << " -std=c99 -fPIC -fms-extensions "
|
|
18
|
+
# $CFLAGS << " -g -O0"
|
|
19
|
+
|
|
20
|
+
create_makefile("certstore/certstore")
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/* certstore_c */
|
|
2
|
+
/* Licensed under the Apache License, Version 2.0 (the "License"); */
|
|
3
|
+
/* you may not use this file except in compliance with the License. */
|
|
4
|
+
/* You may obtain a copy of the License at */
|
|
5
|
+
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
|
6
|
+
/* Unless required by applicable law or agreed to in writing, software */
|
|
7
|
+
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
|
8
|
+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
|
9
|
+
/* See the License for the specific language governing permissions and */
|
|
10
|
+
/* limitations under the License. */
|
|
11
|
+
|
|
12
|
+
#include <certstore.h>
|
|
13
|
+
|
|
14
|
+
static void certstore_loader_free(void *certstore);
|
|
15
|
+
|
|
16
|
+
static const rb_data_type_t rb_win_certstore_loader_type = {
|
|
17
|
+
"winevt/certstore_loader", {
|
|
18
|
+
0, certstore_loader_free, 0,
|
|
19
|
+
}, NULL, NULL,
|
|
20
|
+
RUBY_TYPED_FREE_IMMEDIATELY
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
static void
|
|
24
|
+
certstore_loader_free(void *ptr)
|
|
25
|
+
{
|
|
26
|
+
struct CertstoreLoader *loader = (struct CertstoreLoader *)ptr;
|
|
27
|
+
if (loader->hStore)
|
|
28
|
+
CertCloseStore(loader->hStore, 0);
|
|
29
|
+
|
|
30
|
+
xfree(ptr);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static VALUE
|
|
34
|
+
rb_win_certstore_loader_alloc(VALUE klass)
|
|
35
|
+
{
|
|
36
|
+
VALUE obj;
|
|
37
|
+
struct CertstoreLoader *loader;
|
|
38
|
+
obj = TypedData_Make_Struct(klass,
|
|
39
|
+
struct CertstoreLoader,
|
|
40
|
+
&rb_win_certstore_loader_type,
|
|
41
|
+
loader);
|
|
42
|
+
return obj;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static VALUE
|
|
46
|
+
rb_win_certstore_loader_initialize(VALUE self, VALUE store_name, VALUE use_enterprise)
|
|
47
|
+
{
|
|
48
|
+
VALUE vStoreName;
|
|
49
|
+
struct CertstoreLoader *loader;
|
|
50
|
+
DWORD len;
|
|
51
|
+
|
|
52
|
+
Check_Type(store_name, T_STRING);
|
|
53
|
+
|
|
54
|
+
// store_name : To wide char
|
|
55
|
+
len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(store_name), RSTRING_LEN(store_name), NULL, 0);
|
|
56
|
+
PWSTR winStoreName = ALLOCV_N(WCHAR, vStoreName, len+1);
|
|
57
|
+
MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(store_name), RSTRING_LEN(store_name), winStoreName, len);
|
|
58
|
+
winStoreName[len] = L'\0';
|
|
59
|
+
|
|
60
|
+
TypedData_Get_Struct(self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader);
|
|
61
|
+
|
|
62
|
+
if (RTEST(use_enterprise)) {
|
|
63
|
+
loader->hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, winStoreName);
|
|
64
|
+
} else {
|
|
65
|
+
loader->hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, winStoreName);
|
|
66
|
+
}
|
|
67
|
+
ALLOCV_END(vStoreName);
|
|
68
|
+
|
|
69
|
+
return Qnil;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static VALUE
|
|
73
|
+
certificate_context_to_string(PCCERT_CONTEXT pContext)
|
|
74
|
+
{
|
|
75
|
+
WCHAR *wszString;
|
|
76
|
+
DWORD cchString;
|
|
77
|
+
CHAR *utf8str;
|
|
78
|
+
CHAR *certificate;
|
|
79
|
+
CHAR *certHeader = "-----BEGIN CERTIFICATE-----\n";
|
|
80
|
+
CHAR *certFooter = "\n-----END CERTIFICATE-----";
|
|
81
|
+
CHAR errBuf[256];
|
|
82
|
+
DWORD errCode;
|
|
83
|
+
|
|
84
|
+
if (!CryptBinaryToStringW(pContext->pbCertEncoded, pContext->cbCertEncoded,
|
|
85
|
+
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &cchString)) {
|
|
86
|
+
rb_raise(rb_eCertLoaderError, "cannot obtain certificate string length.");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
wszString = malloc(sizeof(WCHAR) * cchString);
|
|
90
|
+
CryptBinaryToStringW(pContext->pbCertEncoded, pContext->cbCertEncoded,
|
|
91
|
+
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, wszString, &cchString);
|
|
92
|
+
|
|
93
|
+
utf8str = wstr_to_mbstr(CP_UTF8, wszString, -1);
|
|
94
|
+
// malloc sizeof CHAR * ((base64 cert content + header + footer) length).
|
|
95
|
+
certificate = malloc(sizeof(CHAR) * (strlen(utf8str) + strlen(certHeader) + strlen(certFooter)));
|
|
96
|
+
sprintf(certificate, "%s%s%s", certHeader, utf8str, certFooter);
|
|
97
|
+
|
|
98
|
+
if (ERROR_SUCCESS != GetLastError() && CRYPT_E_NOT_FOUND != GetLastError()) {
|
|
99
|
+
sprintf(errBuf, "ErrorCode(%d)", GetLastError());
|
|
100
|
+
|
|
101
|
+
goto error;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
VALUE rb_pem = rb_utf8_str_new_cstr(certificate);
|
|
105
|
+
xfree(utf8str);
|
|
106
|
+
free(wszString);
|
|
107
|
+
free(certificate);
|
|
108
|
+
|
|
109
|
+
return rb_pem;
|
|
110
|
+
|
|
111
|
+
error:
|
|
112
|
+
xfree(utf8str);
|
|
113
|
+
free(wszString);
|
|
114
|
+
free(certificate);
|
|
115
|
+
|
|
116
|
+
rb_raise(rb_eCertLoaderError, errBuf);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static VALUE
|
|
120
|
+
rb_win_certstore_loader_each_pem(VALUE self)
|
|
121
|
+
{
|
|
122
|
+
PCCERT_CONTEXT pContext = NULL;
|
|
123
|
+
struct CertstoreLoader *loader;
|
|
124
|
+
|
|
125
|
+
RETURN_ENUMERATOR(self, 0, 0);
|
|
126
|
+
|
|
127
|
+
TypedData_Get_Struct(self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader);
|
|
128
|
+
|
|
129
|
+
while ((pContext = CertEnumCertificatesInStore(loader->hStore, pContext)) != NULL) {
|
|
130
|
+
VALUE rb_certificate = certificate_context_to_string(pContext);
|
|
131
|
+
rb_yield(rb_certificate);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
CertFreeCertificateContext(pContext);
|
|
135
|
+
|
|
136
|
+
return Qnil;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static VALUE
|
|
140
|
+
rb_win_certstore_loader_dispose(VALUE self)
|
|
141
|
+
{
|
|
142
|
+
struct CertstoreLoader *loader;
|
|
143
|
+
|
|
144
|
+
TypedData_Get_Struct(self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader);
|
|
145
|
+
|
|
146
|
+
/* What should we dispose here? */
|
|
147
|
+
|
|
148
|
+
return Qnil;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static VALUE
|
|
152
|
+
rb_win_certstore_loader_each(VALUE self)
|
|
153
|
+
{
|
|
154
|
+
PCCERT_CONTEXT pContext = NULL;
|
|
155
|
+
struct CertstoreLoader *loader;
|
|
156
|
+
|
|
157
|
+
RETURN_ENUMERATOR(self, 0, 0);
|
|
158
|
+
|
|
159
|
+
TypedData_Get_Struct(self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader);
|
|
160
|
+
|
|
161
|
+
rb_ensure(rb_win_certstore_loader_each_pem, self, rb_win_certstore_loader_dispose, self);
|
|
162
|
+
|
|
163
|
+
return Qnil;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
static VALUE
|
|
167
|
+
rb_win_certstore_loader_find_certificate(VALUE self, VALUE rb_thumbprint)
|
|
168
|
+
{
|
|
169
|
+
VALUE vThumbprint;
|
|
170
|
+
PCCERT_CONTEXT pContext = NULL;
|
|
171
|
+
struct CertstoreLoader *loader;
|
|
172
|
+
DWORD len;
|
|
173
|
+
CHAR errBuf[256];
|
|
174
|
+
|
|
175
|
+
Check_Type(rb_thumbprint, T_STRING);
|
|
176
|
+
|
|
177
|
+
TypedData_Get_Struct(self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader);
|
|
178
|
+
|
|
179
|
+
// thumbprint : To wide char
|
|
180
|
+
len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_thumbprint), RSTRING_LEN(rb_thumbprint), NULL, 0);
|
|
181
|
+
WCHAR *winThumbprint = ALLOCV_N(WCHAR, vThumbprint, len+1);
|
|
182
|
+
MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_thumbprint), RSTRING_LEN(rb_thumbprint), winThumbprint, len);
|
|
183
|
+
winThumbprint[len] = L'\0';
|
|
184
|
+
|
|
185
|
+
BYTE pbThumb[CERT_THUMBPRINT_SIZE];
|
|
186
|
+
CRYPT_HASH_BLOB blob;
|
|
187
|
+
blob.cbData = CERT_THUMBPRINT_SIZE;
|
|
188
|
+
blob.pbData = pbThumb;
|
|
189
|
+
CryptStringToBinaryW(winThumbprint, CERT_THUMBPRINT_STR_LENGTH, CRYPT_STRING_HEX, pbThumb,
|
|
190
|
+
&blob.cbData, NULL, NULL);
|
|
191
|
+
|
|
192
|
+
pContext = CertFindCertificateInStore(
|
|
193
|
+
loader->hStore,
|
|
194
|
+
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
|
|
195
|
+
0,
|
|
196
|
+
CERT_FIND_HASH,
|
|
197
|
+
&blob,
|
|
198
|
+
pContext);
|
|
199
|
+
|
|
200
|
+
if (!pContext)
|
|
201
|
+
goto error;
|
|
202
|
+
|
|
203
|
+
VALUE rb_certificate = certificate_context_to_string(pContext);
|
|
204
|
+
CertFreeCertificateContext(pContext);
|
|
205
|
+
ALLOCV_END(vThumbprint);
|
|
206
|
+
|
|
207
|
+
return rb_certificate;
|
|
208
|
+
|
|
209
|
+
error:
|
|
210
|
+
|
|
211
|
+
CertFreeCertificateContext(pContext);
|
|
212
|
+
|
|
213
|
+
sprintf(errBuf, "Cannot find certificates with thumbprint(%S)", winThumbprint);
|
|
214
|
+
rb_raise(rb_eCertLoaderError, errBuf);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
static VALUE
|
|
218
|
+
rb_win_certstore_loader_export_pfx(VALUE self, VALUE rb_thumbprint, VALUE rb_password)
|
|
219
|
+
{
|
|
220
|
+
VALUE vThumbprint;
|
|
221
|
+
PCCERT_CONTEXT pContext = NULL;
|
|
222
|
+
struct CertstoreLoader *loader;
|
|
223
|
+
DWORD len;
|
|
224
|
+
CHAR errBuf[256];
|
|
225
|
+
HCERTSTORE hMemoryStore = NULL;
|
|
226
|
+
VALUE vPassword;
|
|
227
|
+
CRYPT_DATA_BLOB pfxPacket;
|
|
228
|
+
|
|
229
|
+
TypedData_Get_Struct(self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader);
|
|
230
|
+
|
|
231
|
+
// thumbprint : To wide char
|
|
232
|
+
len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_thumbprint), RSTRING_LEN(rb_thumbprint), NULL, 0);
|
|
233
|
+
WCHAR *winThumbprint = ALLOCV_N(WCHAR, vThumbprint, len+1);
|
|
234
|
+
MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_thumbprint), RSTRING_LEN(rb_thumbprint), winThumbprint, len);
|
|
235
|
+
winThumbprint[len] = L'\0';
|
|
236
|
+
// password : To wide char
|
|
237
|
+
len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_password), RSTRING_LEN(rb_password), NULL, 0);
|
|
238
|
+
WCHAR *winPassword = ALLOCV_N(WCHAR, vPassword, len+1);
|
|
239
|
+
MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(rb_password), RSTRING_LEN(rb_password), winPassword, len);
|
|
240
|
+
winPassword[len] = L'\0';
|
|
241
|
+
|
|
242
|
+
BYTE pbThumb[CERT_THUMBPRINT_SIZE];
|
|
243
|
+
CRYPT_HASH_BLOB blob;
|
|
244
|
+
blob.cbData = CERT_THUMBPRINT_SIZE;
|
|
245
|
+
blob.pbData = pbThumb;
|
|
246
|
+
CryptStringToBinaryW(winThumbprint, CERT_THUMBPRINT_STR_LENGTH, CRYPT_STRING_HEX, pbThumb,
|
|
247
|
+
&blob.cbData, NULL, NULL);
|
|
248
|
+
|
|
249
|
+
pContext = CertFindCertificateInStore(
|
|
250
|
+
loader->hStore,
|
|
251
|
+
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
|
|
252
|
+
0,
|
|
253
|
+
CERT_FIND_HASH,
|
|
254
|
+
&blob,
|
|
255
|
+
pContext);
|
|
256
|
+
if (!pContext) {
|
|
257
|
+
sprintf(errBuf, "Cannot find certificates with thumbprint(%S)", winThumbprint);
|
|
258
|
+
|
|
259
|
+
goto error;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
hMemoryStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, 0, NULL);
|
|
263
|
+
CertAddCertificateContextToStore(hMemoryStore, pContext, CERT_STORE_ADD_ALWAYS, NULL);
|
|
264
|
+
|
|
265
|
+
pfxPacket.pbData = NULL;
|
|
266
|
+
if (!PFXExportCertStoreEx(hMemoryStore, &pfxPacket, winPassword, NULL, EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY)) {
|
|
267
|
+
sprintf(errBuf, "Cannot export pfx certificate with thumbprint(%S)", winThumbprint);
|
|
268
|
+
|
|
269
|
+
goto error;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
pfxPacket.pbData = (LPBYTE)CryptMemAlloc(pfxPacket.cbData);
|
|
273
|
+
if (!PFXExportCertStoreEx(hMemoryStore, &pfxPacket, winPassword, NULL, EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY)) {
|
|
274
|
+
sprintf(errBuf, "Cannot export pfx certificate with thumbprint(%S)", winThumbprint);
|
|
275
|
+
|
|
276
|
+
CryptMemFree(pfxPacket.pbData);
|
|
277
|
+
|
|
278
|
+
goto error;
|
|
279
|
+
}
|
|
280
|
+
ALLOCV_END(vThumbprint);
|
|
281
|
+
ALLOCV_END(vPassword);
|
|
282
|
+
|
|
283
|
+
VALUE rb_str = rb_str_new(pfxPacket.pbData, pfxPacket.cbData);
|
|
284
|
+
|
|
285
|
+
CryptMemFree(pfxPacket.pbData);
|
|
286
|
+
CertCloseStore(hMemoryStore, CERT_CLOSE_STORE_CHECK_FLAG);
|
|
287
|
+
CertFreeCertificateContext(pContext);
|
|
288
|
+
|
|
289
|
+
return rb_str;
|
|
290
|
+
|
|
291
|
+
error:
|
|
292
|
+
ALLOCV_END(vThumbprint);
|
|
293
|
+
ALLOCV_END(vPassword);
|
|
294
|
+
|
|
295
|
+
if (pContext)
|
|
296
|
+
CertFreeCertificateContext(pContext);
|
|
297
|
+
if (hMemoryStore)
|
|
298
|
+
CertCloseStore(hMemoryStore, CERT_CLOSE_STORE_CHECK_FLAG);
|
|
299
|
+
|
|
300
|
+
rb_raise(rb_eCertLoaderError, errBuf);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
void
|
|
304
|
+
Init_certstore_loader(VALUE rb_mCertstore)
|
|
305
|
+
{
|
|
306
|
+
rb_cCertLoader = rb_define_class_under(rb_mCertstore, "Loader", rb_cObject);
|
|
307
|
+
rb_eCertLoaderError = rb_define_class_under(rb_cCertLoader, "LoaderError", rb_eStandardError);
|
|
308
|
+
|
|
309
|
+
rb_define_alloc_func(rb_cCertLoader, rb_win_certstore_loader_alloc);
|
|
310
|
+
rb_define_method(rb_cCertLoader, "initialize", rb_win_certstore_loader_initialize, 2);
|
|
311
|
+
rb_define_method(rb_cCertLoader, "each", rb_win_certstore_loader_each, 0);
|
|
312
|
+
rb_define_method(rb_cCertLoader, "find_cert", rb_win_certstore_loader_find_certificate, 1);
|
|
313
|
+
rb_define_method(rb_cCertLoader, "export_pfx", rb_win_certstore_loader_export_pfx, 2);
|
|
314
|
+
}
|
data/lib/certstore.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#
|
|
2
|
+
# certstore_c
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
require "certstore/certstore"
|
|
18
|
+
module Certstore
|
|
19
|
+
class Loader
|
|
20
|
+
alias_method :initialize_raw, :initialize
|
|
21
|
+
|
|
22
|
+
class InvalidStoreNameError < StandardError; end
|
|
23
|
+
|
|
24
|
+
def initialize(store_name, enterprise: true)
|
|
25
|
+
unless store_name && valid_logical_store_list.include?(store_name.upcase)
|
|
26
|
+
raise InvalidStoreNameError, "'#{store_name}' is not a valid logical store name"
|
|
27
|
+
end
|
|
28
|
+
initialize_raw(store_name, enterprise)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def valid_logical_store_list
|
|
32
|
+
%w[MY CA ROOT AUTHROOT DISALLOWED SPC TRUST TRUSTEDPEOPLE TRUSTEDPUBLISHER CLIENTAUTHISSUER TRUSTEDDEVICES SMARTCARDROOT WEBHOSTING REMOTE\ DESKTOP]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#
|
|
2
|
+
# certstore_c
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
require "certstore/certstore"
|
|
18
|
+
require "openssl"
|
|
19
|
+
require "logger"
|
|
20
|
+
|
|
21
|
+
module Certstore
|
|
22
|
+
module OpenSSL
|
|
23
|
+
class Loader
|
|
24
|
+
attr_reader :cert_store
|
|
25
|
+
|
|
26
|
+
def initialize(log = Logger.new(STDOUT), cert_store, store_name, enterprise: true)
|
|
27
|
+
@log = log
|
|
28
|
+
@cert_store = cert_store
|
|
29
|
+
@store_name = store_name
|
|
30
|
+
@loader = Certstore::Loader.new(@store_name, enterprise: enterprise)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def load_cert_store
|
|
34
|
+
@loader.each do |pem|
|
|
35
|
+
begin
|
|
36
|
+
x509_certificate_obj = ::OpenSSL::X509::Certificate.new(pem)
|
|
37
|
+
valid_duration?(x509_certificate_obj)
|
|
38
|
+
@cert_store.add_cert(x509_certificate_obj)
|
|
39
|
+
rescue ::OpenSSL::X509::StoreError => e # continue to read
|
|
40
|
+
@log.warn "failed to load certificate(thumbprint: #{OpenSSL::Digest::SHA1.new(x509_certificate_obj.to_der).to_s}) from certstore", error: e
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def cleanup_thumbprint(thumbprint)
|
|
46
|
+
thumbprint.gsub(/[^A-Za-z0-9]/, "")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def get_certificate(thumbprint)
|
|
50
|
+
thumbprint = cleanup_thumbprint(thumbprint)
|
|
51
|
+
::OpenSSL::X509::Certificate.new(@loader.find_cert(thumbprint))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def export_pkcs12(thumbprint, password)
|
|
55
|
+
thumbprint = cleanup_thumbprint(thumbprint)
|
|
56
|
+
pkcs12 = @loader.export_pfx(thumbprint, password)
|
|
57
|
+
::OpenSSL::PKCS12.new(pkcs12, password)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def valid_duration?(x509_obj)
|
|
61
|
+
x509_obj.not_before < Time.now.utc && x509_obj.not_after > Time.now.utc
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#
|
|
2
|
+
# certstore_c
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
module Certstore
|
|
18
|
+
VERSION = "0.1.0"
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: certstore_c
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Hiroshi Hatake
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-09-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '12.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '12.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake-compiler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: test-unit
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 3.3.3
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 3.3.3
|
|
69
|
+
description: Windows CertStore loader.
|
|
70
|
+
email:
|
|
71
|
+
- cosmo0920.wp@gmail.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".gitignore"
|
|
77
|
+
- Gemfile
|
|
78
|
+
- LICENSE.txt
|
|
79
|
+
- README.md
|
|
80
|
+
- Rakefile
|
|
81
|
+
- appveyor.yml
|
|
82
|
+
- bin/console
|
|
83
|
+
- bin/setup
|
|
84
|
+
- certstore_c.gemspec
|
|
85
|
+
- example/open_tls_socket.rb
|
|
86
|
+
- ext/certstore/certstore.c
|
|
87
|
+
- ext/certstore/certstore.h
|
|
88
|
+
- ext/certstore/certstore_util.c
|
|
89
|
+
- ext/certstore/extconf.rb
|
|
90
|
+
- ext/certstore/loader.c
|
|
91
|
+
- lib/certstore.rb
|
|
92
|
+
- lib/certstore/loader.rb
|
|
93
|
+
- lib/certstore/openssl/loader.rb
|
|
94
|
+
- lib/certstore/version.rb
|
|
95
|
+
homepage: https://github.com/cosmo0920/certstore_c
|
|
96
|
+
licenses:
|
|
97
|
+
- Apache-2.0
|
|
98
|
+
metadata:
|
|
99
|
+
allowed_push_host: https://rubygems.org
|
|
100
|
+
homepage_uri: https://github.com/cosmo0920/certstore_c
|
|
101
|
+
source_code_uri: https://github.com/cosmo0920/certstore_c
|
|
102
|
+
post_install_message:
|
|
103
|
+
rdoc_options: []
|
|
104
|
+
require_paths:
|
|
105
|
+
- lib
|
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '2.4'
|
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
|
+
requirements:
|
|
113
|
+
- - ">="
|
|
114
|
+
- !ruby/object:Gem::Version
|
|
115
|
+
version: '0'
|
|
116
|
+
requirements: []
|
|
117
|
+
rubyforge_project:
|
|
118
|
+
rubygems_version: 2.7.3
|
|
119
|
+
signing_key:
|
|
120
|
+
specification_version: 4
|
|
121
|
+
summary: Windows CertStore loader.
|
|
122
|
+
test_files: []
|