PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/rkh/jruby
Ruby | 197 lines | 180 code | 17 blank | 0 comment | 1 complexity | 04d4b602eaff36ac238329eb33b73b3f MD5 | raw file
  1. begin
  2. require "openssl"
  3. require_relative 'utils'
  4. rescue LoadError
  5. end
  6. require 'test/unit'
  7. class OpenSSL::TestASN1 < Test::Unit::TestCase
  8. def test_decode
  9. subj = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=TestCA")
  10. key = OpenSSL::TestUtils::TEST_KEY_RSA1024
  11. now = Time.at(Time.now.to_i) # suppress usec
  12. s = 0xdeadbeafdeadbeafdeadbeafdeadbeaf
  13. exts = [
  14. ["basicConstraints","CA:TRUE,pathlen:1",true],
  15. ["keyUsage","keyCertSign, cRLSign",true],
  16. ["subjectKeyIdentifier","hash",false],
  17. ]
  18. dgst = OpenSSL::Digest::SHA1.new
  19. cert = OpenSSL::TestUtils.issue_cert(
  20. subj, key, s, now, now+3600, exts, nil, nil, dgst)
  21. asn1 = OpenSSL::ASN1.decode(cert)
  22. assert_equal(OpenSSL::ASN1::Sequence, asn1.class)
  23. assert_equal(3, asn1.value.size)
  24. tbs_cert, sig_alg, sig_val = *asn1.value
  25. assert_equal(OpenSSL::ASN1::Sequence, tbs_cert.class)
  26. assert_equal(8, tbs_cert.value.size)
  27. version = tbs_cert.value[0]
  28. assert_equal(:CONTEXT_SPECIFIC, version.tag_class)
  29. assert_equal(0, version.tag)
  30. assert_equal(1, version.value.size)
  31. assert_equal(OpenSSL::ASN1::Integer, version.value[0].class)
  32. assert_equal(2, version.value[0].value)
  33. serial = tbs_cert.value[1]
  34. assert_equal(OpenSSL::ASN1::Integer, serial.class)
  35. assert_equal(0xdeadbeafdeadbeafdeadbeafdeadbeaf, serial.value)
  36. sig = tbs_cert.value[2]
  37. assert_equal(OpenSSL::ASN1::Sequence, sig.class)
  38. assert_equal(2, sig.value.size)
  39. assert_equal(OpenSSL::ASN1::ObjectId, sig.value[0].class)
  40. assert_equal("1.2.840.113549.1.1.5", sig.value[0].oid)
  41. assert_equal(OpenSSL::ASN1::Null, sig.value[1].class)
  42. dn = tbs_cert.value[3] # issuer
  43. assert_equal(subj.hash, OpenSSL::X509::Name.new(dn).hash)
  44. assert_equal(OpenSSL::ASN1::Sequence, dn.class)
  45. assert_equal(3, dn.value.size)
  46. assert_equal(OpenSSL::ASN1::Set, dn.value[0].class)
  47. assert_equal(OpenSSL::ASN1::Set, dn.value[1].class)
  48. assert_equal(OpenSSL::ASN1::Set, dn.value[2].class)
  49. assert_equal(1, dn.value[0].value.size)
  50. assert_equal(1, dn.value[1].value.size)
  51. assert_equal(1, dn.value[2].value.size)
  52. assert_equal(OpenSSL::ASN1::Sequence, dn.value[0].value[0].class)
  53. assert_equal(OpenSSL::ASN1::Sequence, dn.value[1].value[0].class)
  54. assert_equal(OpenSSL::ASN1::Sequence, dn.value[2].value[0].class)
  55. assert_equal(2, dn.value[0].value[0].value.size)
  56. assert_equal(2, dn.value[1].value[0].value.size)
  57. assert_equal(2, dn.value[2].value[0].value.size)
  58. oid, value = *dn.value[0].value[0].value
  59. assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
  60. assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
  61. assert_equal(OpenSSL::ASN1::IA5String, value.class)
  62. assert_equal("org", value.value)
  63. oid, value = *dn.value[1].value[0].value
  64. assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
  65. assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
  66. assert_equal(OpenSSL::ASN1::IA5String, value.class)
  67. assert_equal("ruby-lang", value.value)
  68. oid, value = *dn.value[2].value[0].value
  69. assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
  70. assert_equal("2.5.4.3", oid.oid)
  71. assert_equal(OpenSSL::ASN1::UTF8String, value.class)
  72. assert_equal("TestCA", value.value)
  73. validity = tbs_cert.value[4]
  74. assert_equal(OpenSSL::ASN1::Sequence, validity.class)
  75. assert_equal(2, validity.value.size)
  76. assert_equal(OpenSSL::ASN1::UTCTime, validity.value[0].class)
  77. assert_equal(now, validity.value[0].value)
  78. assert_equal(OpenSSL::ASN1::UTCTime, validity.value[1].class)
  79. assert_equal(now+3600, validity.value[1].value)
  80. dn = tbs_cert.value[5] # subject
  81. assert_equal(subj.hash, OpenSSL::X509::Name.new(dn).hash)
  82. assert_equal(OpenSSL::ASN1::Sequence, dn.class)
  83. assert_equal(3, dn.value.size)
  84. assert_equal(OpenSSL::ASN1::Set, dn.value[0].class)
  85. assert_equal(OpenSSL::ASN1::Set, dn.value[1].class)
  86. assert_equal(OpenSSL::ASN1::Set, dn.value[2].class)
  87. assert_equal(1, dn.value[0].value.size)
  88. assert_equal(1, dn.value[1].value.size)
  89. assert_equal(1, dn.value[2].value.size)
  90. assert_equal(OpenSSL::ASN1::Sequence, dn.value[0].value[0].class)
  91. assert_equal(OpenSSL::ASN1::Sequence, dn.value[1].value[0].class)
  92. assert_equal(OpenSSL::ASN1::Sequence, dn.value[2].value[0].class)
  93. assert_equal(2, dn.value[0].value[0].value.size)
  94. assert_equal(2, dn.value[1].value[0].value.size)
  95. assert_equal(2, dn.value[2].value[0].value.size)
  96. oid, value = *dn.value[0].value[0].value
  97. assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
  98. assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
  99. assert_equal(OpenSSL::ASN1::IA5String, value.class)
  100. assert_equal("org", value.value)
  101. oid, value = *dn.value[1].value[0].value
  102. assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
  103. assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
  104. assert_equal(OpenSSL::ASN1::IA5String, value.class)
  105. assert_equal("ruby-lang", value.value)
  106. oid, value = *dn.value[2].value[0].value
  107. assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
  108. assert_equal("2.5.4.3", oid.oid)
  109. assert_equal(OpenSSL::ASN1::UTF8String, value.class)
  110. assert_equal("TestCA", value.value)
  111. pkey = tbs_cert.value[6]
  112. assert_equal(OpenSSL::ASN1::Sequence, pkey.class)
  113. assert_equal(2, pkey.value.size)
  114. assert_equal(OpenSSL::ASN1::Sequence, pkey.value[0].class)
  115. assert_equal(2, pkey.value[0].value.size)
  116. assert_equal(OpenSSL::ASN1::ObjectId, pkey.value[0].value[0].class)
  117. assert_equal("1.2.840.113549.1.1.1", pkey.value[0].value[0].oid)
  118. assert_equal(OpenSSL::ASN1::BitString, pkey.value[1].class)
  119. assert_equal(0, pkey.value[1].unused_bits)
  120. spkey = OpenSSL::ASN1.decode(pkey.value[1].value)
  121. assert_equal(OpenSSL::ASN1::Sequence, spkey.class)
  122. assert_equal(2, spkey.value.size)
  123. assert_equal(OpenSSL::ASN1::Integer, spkey.value[0].class)
  124. assert_equal(143085709396403084580358323862163416700436550432664688288860593156058579474547937626086626045206357324274536445865308750491138538454154232826011964045825759324933943290377903384882276841880081931690695505836279972214003660451338124170055999155993192881685495391496854691199517389593073052473319331505702779271, spkey.value[0].value)
  125. assert_equal(OpenSSL::ASN1::Integer, spkey.value[1].class)
  126. assert_equal(65537, spkey.value[1].value)
  127. extensions = tbs_cert.value[7]
  128. assert_equal(:CONTEXT_SPECIFIC, extensions.tag_class)
  129. assert_equal(3, extensions.tag)
  130. assert_equal(1, extensions.value.size)
  131. assert_equal(OpenSSL::ASN1::Sequence, extensions.value[0].class)
  132. assert_equal(3, extensions.value[0].value.size)
  133. ext = extensions.value[0].value[0] # basicConstraints
  134. assert_equal(OpenSSL::ASN1::Sequence, ext.class)
  135. assert_equal(3, ext.value.size)
  136. assert_equal(OpenSSL::ASN1::ObjectId, ext.value[0].class)
  137. assert_equal("2.5.29.19", ext.value[0].oid)
  138. assert_equal(OpenSSL::ASN1::Boolean, ext.value[1].class)
  139. assert_equal(true, ext.value[1].value)
  140. assert_equal(OpenSSL::ASN1::OctetString, ext.value[2].class)
  141. extv = OpenSSL::ASN1.decode(ext.value[2].value)
  142. assert_equal(OpenSSL::ASN1::Sequence, extv.class)
  143. assert_equal(2, extv.value.size)
  144. assert_equal(OpenSSL::ASN1::Boolean, extv.value[0].class)
  145. assert_equal(true, extv.value[0].value)
  146. assert_equal(OpenSSL::ASN1::Integer, extv.value[1].class)
  147. assert_equal(1, extv.value[1].value)
  148. ext = extensions.value[0].value[1] # keyUsage
  149. assert_equal(OpenSSL::ASN1::Sequence, ext.class)
  150. assert_equal(3, ext.value.size)
  151. assert_equal(OpenSSL::ASN1::ObjectId, ext.value[0].class)
  152. assert_equal("2.5.29.15", ext.value[0].oid)
  153. assert_equal(OpenSSL::ASN1::Boolean, ext.value[1].class)
  154. assert_equal(true, ext.value[1].value)
  155. assert_equal(OpenSSL::ASN1::OctetString, ext.value[2].class)
  156. extv = OpenSSL::ASN1.decode(ext.value[2].value)
  157. assert_equal(OpenSSL::ASN1::BitString, extv.class)
  158. str = "\000"; str[0] = 0b00000110.chr
  159. assert_equal(str, extv.value)
  160. ext = extensions.value[0].value[2] # subjetKeyIdentifier
  161. assert_equal(OpenSSL::ASN1::Sequence, ext.class)
  162. assert_equal(2, ext.value.size)
  163. assert_equal(OpenSSL::ASN1::ObjectId, ext.value[0].class)
  164. assert_equal("2.5.29.14", ext.value[0].oid)
  165. assert_equal(OpenSSL::ASN1::OctetString, ext.value[1].class)
  166. extv = OpenSSL::ASN1.decode(ext.value[1].value)
  167. assert_equal(OpenSSL::ASN1::OctetString, extv.class)
  168. sha1 = OpenSSL::Digest::SHA1.new
  169. sha1.update(pkey.value[1].value)
  170. assert_equal(sha1.digest, extv.value)
  171. assert_equal(OpenSSL::ASN1::Sequence, sig_alg.class)
  172. assert_equal(2, sig_alg.value.size)
  173. assert_equal(OpenSSL::ASN1::ObjectId, pkey.value[0].value[0].class)
  174. assert_equal("1.2.840.113549.1.1.1", pkey.value[0].value[0].oid)
  175. assert_equal(OpenSSL::ASN1::Null, pkey.value[0].value[1].class)
  176. assert_equal(OpenSSL::ASN1::BitString, sig_val.class)
  177. cululated_sig = key.sign(OpenSSL::Digest::SHA1.new, tbs_cert.to_der)
  178. assert_equal(cululated_sig, sig_val.value)
  179. end
  180. end if defined?(OpenSSL)