PageRenderTime 100ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/externals/ruby1.9/openssl/test_x509store.rb

https://github.com/rkh/jruby
Ruby | 218 lines | 191 code | 24 blank | 3 comment | 1 complexity | fb36ed8e204e9a42f15c32a5ae98d04b MD5 | raw file
  1. begin
  2. require "openssl"
  3. require_relative "utils"
  4. rescue LoadError
  5. end
  6. require "test/unit"
  7. if defined?(OpenSSL)
  8. class OpenSSL::TestX509Store < Test::Unit::TestCase
  9. def setup
  10. @rsa1024 = OpenSSL::TestUtils::TEST_KEY_RSA1024
  11. @rsa2048 = OpenSSL::TestUtils::TEST_KEY_RSA2048
  12. @dsa256 = OpenSSL::TestUtils::TEST_KEY_DSA256
  13. @dsa512 = OpenSSL::TestUtils::TEST_KEY_DSA512
  14. @ca1 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA1")
  15. @ca2 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA2")
  16. @ee1 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=EE1")
  17. @ee2 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=EE2")
  18. end
  19. def teardown
  20. end
  21. def issue_cert(*args)
  22. OpenSSL::TestUtils.issue_cert(*args)
  23. end
  24. def issue_crl(*args)
  25. OpenSSL::TestUtils.issue_crl(*args)
  26. end
  27. def test_verify
  28. now = Time.at(Time.now.to_i)
  29. ca_exts = [
  30. ["basicConstraints","CA:TRUE",true],
  31. ["keyUsage","cRLSign,keyCertSign",true],
  32. ]
  33. ee_exts = [
  34. ["keyUsage","keyEncipherment,digitalSignature",true],
  35. ]
  36. ca1_cert = issue_cert(@ca1, @rsa2048, 1, now, now+3600, ca_exts,
  37. nil, nil, OpenSSL::Digest::SHA1.new)
  38. ca2_cert = issue_cert(@ca2, @rsa1024, 2, now, now+1800, ca_exts,
  39. ca1_cert, @rsa2048, OpenSSL::Digest::SHA1.new)
  40. ee1_cert = issue_cert(@ee1, @dsa256, 10, now, now+1800, ee_exts,
  41. ca2_cert, @rsa1024, OpenSSL::Digest::SHA1.new)
  42. ee2_cert = issue_cert(@ee2, @dsa512, 20, now, now+1800, ee_exts,
  43. ca2_cert, @rsa1024, OpenSSL::Digest::SHA1.new)
  44. ee3_cert = issue_cert(@ee2, @dsa512, 30, now-100, now-1, ee_exts,
  45. ca2_cert, @rsa1024, OpenSSL::Digest::SHA1.new)
  46. ee4_cert = issue_cert(@ee2, @dsa512, 40, now+1000, now+2000, ee_exts,
  47. ca2_cert, @rsa1024, OpenSSL::Digest::SHA1.new)
  48. revoke_info = []
  49. crl1 = issue_crl(revoke_info, 1, now, now+1800, [],
  50. ca1_cert, @rsa2048, OpenSSL::Digest::SHA1.new)
  51. revoke_info = [ [2, now, 1], ]
  52. crl1_2 = issue_crl(revoke_info, 2, now, now+1800, [],
  53. ca1_cert, @rsa2048, OpenSSL::Digest::SHA1.new)
  54. revoke_info = [ [20, now, 1], ]
  55. crl2 = issue_crl(revoke_info, 1, now, now+1800, [],
  56. ca2_cert, @rsa1024, OpenSSL::Digest::SHA1.new)
  57. revoke_info = []
  58. crl2_2 = issue_crl(revoke_info, 2, now-100, now-1, [],
  59. ca2_cert, @rsa1024, OpenSSL::Digest::SHA1.new)
  60. assert_equal(true, ca1_cert.verify(ca1_cert.public_key)) # self signed
  61. assert_equal(true, ca2_cert.verify(ca1_cert.public_key)) # issued by ca1
  62. assert_equal(true, ee1_cert.verify(ca2_cert.public_key)) # issued by ca2
  63. assert_equal(true, ee2_cert.verify(ca2_cert.public_key)) # issued by ca2
  64. assert_equal(true, ee3_cert.verify(ca2_cert.public_key)) # issued by ca2
  65. assert_equal(true, crl1.verify(ca1_cert.public_key)) # issued by ca1
  66. assert_equal(true, crl1_2.verify(ca1_cert.public_key)) # issued by ca1
  67. assert_equal(true, crl2.verify(ca2_cert.public_key)) # issued by ca2
  68. assert_equal(true, crl2_2.verify(ca2_cert.public_key)) # issued by ca2
  69. store = OpenSSL::X509::Store.new
  70. assert_equal(false, store.verify(ca1_cert))
  71. assert_not_equal(OpenSSL::X509::V_OK, store.error)
  72. assert_equal(false, store.verify(ca2_cert))
  73. assert_not_equal(OpenSSL::X509::V_OK, store.error)
  74. store.add_cert(ca1_cert)
  75. assert_equal(true, store.verify(ca2_cert))
  76. assert_equal(OpenSSL::X509::V_OK, store.error)
  77. assert_equal("ok", store.error_string)
  78. chain = store.chain
  79. assert_equal(2, chain.size)
  80. assert_equal(@ca2.to_der, chain[0].subject.to_der)
  81. assert_equal(@ca1.to_der, chain[1].subject.to_der)
  82. store.purpose = OpenSSL::X509::PURPOSE_SSL_CLIENT
  83. assert_equal(false, store.verify(ca2_cert))
  84. assert_not_equal(OpenSSL::X509::V_OK, store.error)
  85. store.purpose = OpenSSL::X509::PURPOSE_CRL_SIGN
  86. assert_equal(true, store.verify(ca2_cert))
  87. assert_equal(OpenSSL::X509::V_OK, store.error)
  88. store.add_cert(ca2_cert)
  89. store.purpose = OpenSSL::X509::PURPOSE_SSL_CLIENT
  90. assert_equal(true, store.verify(ee1_cert))
  91. assert_equal(true, store.verify(ee2_cert))
  92. assert_equal(OpenSSL::X509::V_OK, store.error)
  93. assert_equal("ok", store.error_string)
  94. chain = store.chain
  95. assert_equal(3, chain.size)
  96. assert_equal(@ee2.to_der, chain[0].subject.to_der)
  97. assert_equal(@ca2.to_der, chain[1].subject.to_der)
  98. assert_equal(@ca1.to_der, chain[2].subject.to_der)
  99. assert_equal(false, store.verify(ee3_cert))
  100. assert_equal(OpenSSL::X509::V_ERR_CERT_HAS_EXPIRED, store.error)
  101. assert_match(/expire/i, store.error_string)
  102. assert_equal(false, store.verify(ee4_cert))
  103. assert_equal(OpenSSL::X509::V_ERR_CERT_NOT_YET_VALID, store.error)
  104. assert_match(/not yet valid/i, store.error_string)
  105. store = OpenSSL::X509::Store.new
  106. store.add_cert(ca1_cert)
  107. store.add_cert(ca2_cert)
  108. store.time = now + 1500
  109. assert_equal(true, store.verify(ca1_cert))
  110. assert_equal(true, store.verify(ca2_cert))
  111. assert_equal(true, store.verify(ee4_cert))
  112. store.time = now + 1900
  113. assert_equal(true, store.verify(ca1_cert))
  114. assert_equal(false, store.verify(ca2_cert))
  115. assert_equal(OpenSSL::X509::V_ERR_CERT_HAS_EXPIRED, store.error)
  116. assert_equal(false, store.verify(ee4_cert))
  117. assert_equal(OpenSSL::X509::V_ERR_CERT_HAS_EXPIRED, store.error)
  118. store.time = now + 4000
  119. assert_equal(false, store.verify(ee1_cert))
  120. assert_equal(OpenSSL::X509::V_ERR_CERT_HAS_EXPIRED, store.error)
  121. assert_equal(false, store.verify(ee4_cert))
  122. assert_equal(OpenSSL::X509::V_ERR_CERT_HAS_EXPIRED, store.error)
  123. # the underlying X509 struct caches the result of the last
  124. # verification for signature and not-before. so the following code
  125. # rebuilds new objects to avoid site effect.
  126. store.time = Time.now - 4000
  127. assert_equal(false, store.verify(OpenSSL::X509::Certificate.new(ca2_cert)))
  128. assert_equal(OpenSSL::X509::V_ERR_CERT_NOT_YET_VALID, store.error)
  129. assert_equal(false, store.verify(OpenSSL::X509::Certificate.new(ee1_cert)))
  130. assert_equal(OpenSSL::X509::V_ERR_CERT_NOT_YET_VALID, store.error)
  131. return unless defined?(OpenSSL::X509::V_FLAG_CRL_CHECK)
  132. store = OpenSSL::X509::Store.new
  133. store.purpose = OpenSSL::X509::PURPOSE_ANY
  134. store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK
  135. store.add_cert(ca1_cert)
  136. store.add_crl(crl1) # revoke no cert
  137. store.add_crl(crl2) # revoke ee2_cert
  138. assert_equal(true, store.verify(ca1_cert))
  139. assert_equal(true, store.verify(ca2_cert))
  140. assert_equal(true, store.verify(ee1_cert, [ca2_cert]))
  141. assert_equal(false, store.verify(ee2_cert, [ca2_cert]))
  142. store = OpenSSL::X509::Store.new
  143. store.purpose = OpenSSL::X509::PURPOSE_ANY
  144. store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK
  145. store.add_cert(ca1_cert)
  146. store.add_crl(crl1_2) # revoke ca2_cert
  147. store.add_crl(crl2) # revoke ee2_cert
  148. assert_equal(true, store.verify(ca1_cert))
  149. assert_equal(false, store.verify(ca2_cert))
  150. assert_equal(true, store.verify(ee1_cert, [ca2_cert]),
  151. "This test is expected to be success with OpenSSL 0.9.7c or later.")
  152. assert_equal(false, store.verify(ee2_cert, [ca2_cert]))
  153. store.flags =
  154. OpenSSL::X509::V_FLAG_CRL_CHECK|OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
  155. assert_equal(true, store.verify(ca1_cert))
  156. assert_equal(false, store.verify(ca2_cert))
  157. assert_equal(false, store.verify(ee1_cert, [ca2_cert]))
  158. assert_equal(false, store.verify(ee2_cert, [ca2_cert]))
  159. store = OpenSSL::X509::Store.new
  160. store.purpose = OpenSSL::X509::PURPOSE_ANY
  161. store.flags =
  162. OpenSSL::X509::V_FLAG_CRL_CHECK|OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
  163. store.add_cert(ca1_cert)
  164. store.add_cert(ca2_cert)
  165. store.add_crl(crl1)
  166. store.add_crl(crl2_2) # issued by ca2 but expired.
  167. assert_equal(true, store.verify(ca1_cert))
  168. assert_equal(true, store.verify(ca2_cert))
  169. assert_equal(false, store.verify(ee1_cert))
  170. assert_equal(OpenSSL::X509::V_ERR_CRL_HAS_EXPIRED, store.error)
  171. assert_equal(false, store.verify(ee2_cert))
  172. end
  173. def test_set_errors
  174. now = Time.now
  175. ca1_cert = issue_cert(@ca1, @rsa2048, 1, now, now+3600, [],
  176. nil, nil, OpenSSL::Digest::SHA1.new)
  177. store = OpenSSL::X509::Store.new
  178. store.add_cert(ca1_cert)
  179. assert_raise(OpenSSL::X509::StoreError){
  180. store.add_cert(ca1_cert) # add same certificate twice
  181. }
  182. revoke_info = []
  183. crl1 = issue_crl(revoke_info, 1, now, now+1800, [],
  184. ca1_cert, @rsa2048, OpenSSL::Digest::SHA1.new)
  185. revoke_info = [ [2, now, 1], ]
  186. crl2 = issue_crl(revoke_info, 2, now+1800, now+3600, [],
  187. ca1_cert, @rsa2048, OpenSSL::Digest::SHA1.new)
  188. store.add_crl(crl1)
  189. assert_raise(OpenSSL::X509::StoreError){
  190. store.add_crl(crl2) # add CRL issued by same CA twice.
  191. }
  192. end
  193. end
  194. end