File: ssl.rb

package info (click to toggle)
ruby-async-rspec 1.16.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: ruby: 457; makefile: 4
file content (224 lines) | stat: -rw-r--r-- 7,899 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'openssl'

module Async
	module RSpec
		module SSL
			module CertificateAuthority
			end
			
			module ValidCertificate
			end
			
			module InvalidCertificate
			end
			
			module VerifiedContexts
			end
			
			module HostCertificates
			end
		end
		
		::RSpec.shared_context SSL::CertificateAuthority do
			# This key size is generally considered insecure, but it's fine for testing.
			let(:certificate_authority_key) {OpenSSL::PKey::RSA.new(2048)}
			let(:certificate_authority_name) {OpenSSL::X509::Name.parse("O=TestCA/CN=localhost")}

			# The certificate authority is used for signing and validating the certificate which is used for communciation:
			let(:certificate_authority) do
				certificate = OpenSSL::X509::Certificate.new
				
				certificate.subject = certificate_authority_name
				# We use the same issuer as the subject, which makes this certificate self-signed:
				certificate.issuer = certificate_authority_name
				
				certificate.public_key = certificate_authority_key.public_key
				
				certificate.serial = 1
				certificate.version = 2
				
				certificate.not_before = Time.now
				certificate.not_after = Time.now + 3600
				
				extension_factory = OpenSSL::X509::ExtensionFactory.new
				extension_factory.subject_certificate = certificate
				extension_factory.issuer_certificate = certificate
				certificate.add_extension extension_factory.create_extension("basicConstraints", "CA:TRUE", true)
				certificate.add_extension extension_factory.create_extension("keyUsage", "keyCertSign, cRLSign", true)
				certificate.add_extension extension_factory.create_extension("subjectKeyIdentifier", "hash")
				certificate.add_extension extension_factory.create_extension("authorityKeyIdentifier", "keyid:always", false)
				
				certificate.sign certificate_authority_key, OpenSSL::Digest::SHA256.new
			end
			
			let(:certificate_store) do
				# The certificate store which is used for validating the server certificate:
				OpenSSL::X509::Store.new.tap do |certificates|
					certificates.add_cert(certificate_authority)
				end
			end
		end
		
		::RSpec.shared_context SSL::ValidCertificate do
			include_context SSL::CertificateAuthority
			
			# The private key to use on the server side:
			let(:key) {OpenSSL::PKey::RSA.new(2048)}
			let(:certificate_name) {OpenSSL::X509::Name.parse("O=Test/CN=localhost")}

			# The certificate used for actual communication:
			let(:certificate) do
				certificate = OpenSSL::X509::Certificate.new
				certificate.subject = certificate_name
				certificate.issuer = certificate_authority.subject
				
				certificate.public_key = key.public_key
				
				certificate.serial = 2
				certificate.version = 2
				
				certificate.not_before = Time.now
				certificate.not_after = Time.now + 3600
				
				extension_factory = OpenSSL::X509::ExtensionFactory.new()
				extension_factory.subject_certificate = certificate
				extension_factory.issuer_certificate = certificate_authority
				certificate.add_extension extension_factory.create_extension("keyUsage", "digitalSignature", true)
				certificate.add_extension extension_factory.create_extension("subjectKeyIdentifier", "hash")
				
				certificate.sign certificate_authority_key, OpenSSL::Digest::SHA256.new
			end
		end
		
		::RSpec.shared_context SSL::HostCertificates do
			include_context SSL::CertificateAuthority
			
			let(:keys) do
				Hash[
					hosts.collect{|name| [name, OpenSSL::PKey::RSA.new(2048)]}
				]
			end
			
			# The certificate used for actual communication:
			let(:certificates) do
				Hash[
					hosts.collect do |name|
						certificate_name = OpenSSL::X509::Name.parse("O=Test/CN=#{name}")
						
						certificate = OpenSSL::X509::Certificate.new
						certificate.subject = certificate_name
						certificate.issuer = certificate_authority.subject
						
						certificate.public_key = keys[name].public_key
						
						certificate.serial = 2
						certificate.version = 2
						
						certificate.not_before = Time.now
						certificate.not_after = Time.now + 3600
						
						extension_factory = OpenSSL::X509::ExtensionFactory.new
						extension_factory.subject_certificate = certificate
						extension_factory.issuer_certificate = certificate_authority
						certificate.add_extension extension_factory.create_extension("keyUsage", "digitalSignature", true)
						certificate.add_extension extension_factory.create_extension("subjectKeyIdentifier", "hash")
						
						certificate.sign certificate_authority_key, OpenSSL::Digest::SHA256.new
						
						[name, certificate]
					end
				]
			end
			
			let(:server_context) do
				OpenSSL::SSL::SSLContext.new.tap do |context|
					context.servername_cb = Proc.new do |socket, name|
						if hosts.include? name
							socket.hostname = name
							
							OpenSSL::SSL::SSLContext.new.tap do |context|
								context.cert = certificates[name]
								context.key = keys[name]
							end
						end
					end
				end
			end
			
			let(:client_context) do
				OpenSSL::SSL::SSLContext.new.tap do |context|
					context.cert_store = certificate_store
					context.verify_mode = OpenSSL::SSL::VERIFY_PEER
				end
			end
		end
		
		::RSpec.shared_context SSL::InvalidCertificate do
			include_context SSL::CertificateAuthority
			
			# The private key to use on the server side:
			let(:key) {OpenSSL::PKey::RSA.new(2048)}
			let(:invalid_key) {OpenSSL::PKey::RSA.new(2048)}
			let(:certificate_name) {OpenSSL::X509::Name.parse("O=Test/CN=localhost")}

			# The certificate used for actual communication:
			let(:certificate) do
				certificate = OpenSSL::X509::Certificate.new
				certificate.subject = certificate_name
				certificate.issuer = certificate_authority.subject
				
				certificate.public_key = key.public_key
				
				certificate.serial = 2
				certificate.version = 2
				
				certificate.not_before = Time.now - 3600
				certificate.not_after = Time.now
				
				extension_factory = OpenSSL::X509::ExtensionFactory.new()
				extension_factory.subject_certificate = certificate
				extension_factory.issuer_certificate = certificate_authority
				certificate.add_extension extension_factory.create_extension("keyUsage", "digitalSignature", true)
				certificate.add_extension extension_factory.create_extension("subjectKeyIdentifier", "hash")
				
				certificate.sign invalid_key, OpenSSL::Digest::SHA256.new
			end
		end
		
		::RSpec.shared_context SSL::VerifiedContexts do
			let(:server_context) do
				OpenSSL::SSL::SSLContext.new.tap do |context|
					context.cert = certificate
					context.key = key
				end
			end

			let(:client_context) do
				OpenSSL::SSL::SSLContext.new.tap do |context|
					context.cert_store = certificate_store
					context.verify_mode = OpenSSL::SSL::VERIFY_PEER
				end
			end
		end
	end
end