Skip to content

Commit 2a43a05

Browse files
committed
Fix test CSR versions for OpenSSL 3.4 change
The only valid CSR version is version 1, which is encoded with "csr.version = 0". Versions of OpenSSL prior to 3.4 would allow other versions to be set / versions to be unset but OpenSSL 3.4 is strict about only allowing version 1. This change updates the unit tests to use version 1 in all CSRs generated as part of testing. It also adds the start of a script to help regenerate the spec/fixtures/ssl directory contents.
1 parent b189450 commit 2a43a05

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This will regenerate SSL fixtures. It does not cover all fixtures yet.
2+
# Run it from the spec/fixtures/ssl directory.
3+
4+
require_relative '../../lib/puppet_spec/ssl'
5+
6+
def make_subject(name)
7+
OpenSSL::X509::Name.new([["CN", name]])
8+
end
9+
10+
def write_pem(content, path)
11+
File.write(path, "#{content.to_text}\n#{content.to_pem}")
12+
puts "Wrote #{content.class} to #{path}"
13+
end
14+
15+
def read_pem(path, type)
16+
text = File.read(path, encoding: 'UTF-8')
17+
type.new(text)
18+
end
19+
20+
def load_or_generate_key(path, size = nil)
21+
if File.exist?(path)
22+
puts "Loading RSA key from #{path}"
23+
read_pem(path, OpenSSL::PKey::RSA)
24+
else
25+
puts "Generating new RSA key"
26+
PuppetSpec::SSL.create_private_key(size)
27+
end
28+
end
29+
30+
def load_or_generate_csr(path, key = nil, name = nil)
31+
if File.exist?(path)
32+
puts "Loading CSR from #{path}"
33+
return read_pem(path, OpenSSL::X509::Request)
34+
end
35+
36+
raise "Must pass key and name parameters if CSR needs to be generated" unless key && name
37+
38+
csr = PuppetSpec::SSL.create_csr(key, "CN=#{name}")
39+
puts "Generating new CSR for #{csr.subject}"
40+
write_pem(csr, 'request.pem')
41+
end
42+
43+
# Load or generate request-key.pem and request.pem
44+
req_key = load_or_generate_key('request-key.pem')
45+
req_csr = load_or_generate_csr('request.pem', req_key, 'pending')
46+
47+
# Swap associated public key in request.pem to create a tampered CSR:
48+
unless File.exist?('tampered-csr.pem')
49+
tampered_csr = load_or_generate_csr('request.pem')
50+
tampered_csr.subject = make_subject('signed')
51+
write_pem(tampered_csr, 'tampered-csr.pem')
52+
end
53+
54+
puts "NOTE: Most fixtures are not yet able to be generated with this script"

spec/lib/puppet/test_ca.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def create_request(name)
3434
csr = OpenSSL::X509::Request.new
3535
csr.public_key = key.public_key
3636
csr.subject = OpenSSL::X509::Name.new([["CN", name]])
37-
csr.version = 2
37+
csr.version = 0
3838
csr.sign(key, @digest)
3939
{ private_key: key, csr: csr }
4040
end

spec/lib/puppet_spec/ssl.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def self.create_csr(key, name)
6161

6262
csr.public_key = key.public_key
6363
csr.subject = OpenSSL::X509::Name.parse(name)
64-
csr.version = 2
64+
csr.version = 0
6565
csr.sign(key, DEFAULT_SIGNING_DIGEST)
6666

6767
csr

spec/unit/ssl/certificate_request_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@
314314
it "should use SHA1 to sign the csr when SHA256 isn't available" do
315315
csr = OpenSSL::X509::Request.new
316316
csr.public_key = key.public_key
317+
csr.version = 0
317318
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA256").and_return(false)
318319
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA1").and_return(true)
319320
signer = Puppet::SSL::CertificateSigner.new
@@ -325,6 +326,7 @@
325326
key = OpenSSL::PKey::RSA.new(2048)
326327
csr = OpenSSL::X509::Request.new
327328
csr.public_key = key.public_key
329+
csr.version = 0
328330
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA256").and_return(false)
329331
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA1").and_return(false)
330332
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA512").and_return(true)
@@ -337,6 +339,7 @@
337339
key = OpenSSL::PKey::RSA.new(2048)
338340
csr = OpenSSL::X509::Request.new
339341
csr.public_key = key.public_key
342+
csr.version = 0
340343
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA256").and_return(false)
341344
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA1").and_return(false)
342345
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA512").and_return(false)
@@ -349,6 +352,7 @@
349352
it "should use SHA224 to sign the csr when SHA256/SHA1/SHA512/SHA384 aren't available" do
350353
csr = OpenSSL::X509::Request.new
351354
csr.public_key = key.public_key
355+
csr.version = 0
352356
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA256").and_return(false)
353357
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA1").and_return(false)
354358
expect(OpenSSL::Digest).to receive(:const_defined?).with("SHA512").and_return(false)

0 commit comments

Comments
 (0)