1
+ require_relative '../../../../spec_helper'
2
+ require 'openssl'
3
+
4
+ describe "OpenSSL::X509::Name.verify" do
5
+ it "returns true for valid certificate" do
6
+ key = OpenSSL ::PKey ::RSA . new 2048
7
+ cert = OpenSSL ::X509 ::Certificate . new
8
+ cert . version = 2
9
+ cert . serial = 1
10
+ cert . subject = OpenSSL ::X509 ::Name . parse "/DC=org/DC=truffleruby/CN=TruffleRuby CA"
11
+ cert . issuer = cert . subject
12
+ cert . public_key = key . public_key
13
+ cert . not_before = Time . now
14
+ cert . not_after = cert . not_before + 365 * 24 * 60 * 60
15
+ cert . sign key , OpenSSL ::Digest ::SHA1 . new
16
+ store = OpenSSL ::X509 ::Store . new
17
+ store . add_cert ( cert )
18
+ store . verify ( cert ) . should == true
19
+ end
20
+
21
+ it "returns false for an expired certificate" do
22
+ key = OpenSSL ::PKey ::RSA . new 2048
23
+ cert = OpenSSL ::X509 ::Certificate . new
24
+ cert . version = 2
25
+ cert . serial = 1
26
+ cert . subject = OpenSSL ::X509 ::Name . parse "/DC=org/DC=truffleruby/CN=TruffleRuby CA"
27
+ cert . issuer = cert . subject
28
+ cert . public_key = key . public_key
29
+ cert . not_before = Time . now - 10
30
+ cert . not_after = Time . now - 5
31
+ cert . sign key , OpenSSL ::Digest ::SHA1 . new
32
+ store = OpenSSL ::X509 ::Store . new
33
+ store . add_cert ( cert )
34
+ store . verify ( cert ) . should == false
35
+ end
36
+
37
+ it "returns false for an expired root certificate" do
38
+ root_key = OpenSSL ::PKey ::RSA . new 2048
39
+ root_cert = OpenSSL ::X509 ::Certificate . new
40
+ root_cert . version = 2
41
+ root_cert . serial = 1
42
+ root_cert . subject = OpenSSL ::X509 ::Name . parse "/DC=org/DC=truffleruby/CN=TruffleRuby CA"
43
+ root_cert . issuer = root_cert . subject
44
+ root_cert . public_key = root_key . public_key
45
+ root_cert . not_before = Time . now - 10
46
+ root_cert . not_after = Time . now - 5
47
+ ef = OpenSSL ::X509 ::ExtensionFactory . new
48
+ ef . subject_certificate = root_cert
49
+ ef . issuer_certificate = root_cert
50
+ root_cert . add_extension ( ef . create_extension ( "basicConstraints" , "CA:TRUE" , true ) )
51
+ root_cert . add_extension ( ef . create_extension ( "keyUsage" , "keyCertSign, cRLSign" , true ) )
52
+ root_cert . add_extension ( ef . create_extension ( "subjectKeyIdentifier" , "hash" , false ) )
53
+ root_cert . add_extension ( ef . create_extension ( "authorityKeyIdentifier" , "keyid:always" , false ) )
54
+ root_cert . sign ( root_key , OpenSSL ::Digest ::SHA256 . new )
55
+
56
+
57
+ key = OpenSSL ::PKey ::RSA . new 2048
58
+ cert = OpenSSL ::X509 ::Certificate . new
59
+ cert . version = 2
60
+ cert . serial = 2
61
+ cert . subject = OpenSSL ::X509 ::Name . parse "/DC=org/DC=truffleruby/CN=TruffleRuby certificate"
62
+ cert . issuer = root_cert . subject
63
+ cert . public_key = key . public_key
64
+ cert . not_before = Time . now
65
+ cert . not_after = cert . not_before + 1 * 365 * 24 * 60 * 60
66
+ ef = OpenSSL ::X509 ::ExtensionFactory . new
67
+ ef . subject_certificate = cert
68
+ ef . issuer_certificate = root_cert
69
+ cert . add_extension ( ef . create_extension ( "keyUsage" , "digitalSignature" , true ) )
70
+ cert . add_extension ( ef . create_extension ( "subjectKeyIdentifier" , "hash" , false ) )
71
+ cert . sign ( root_key , OpenSSL ::Digest ::SHA256 . new )
72
+
73
+ store = OpenSSL ::X509 ::Store . new
74
+ store . add_cert ( root_cert )
75
+ store . add_cert ( cert )
76
+ store . verify ( cert ) . should == false
77
+ end
78
+ end
0 commit comments