test_rfc2985.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2019, Vigil Security, LLC
  6. # License: http://snmplabs.com/pyasn1/license.html
  7. #
  8. import sys
  9. import unittest
  10. from pyasn1.codec.der.decoder import decode as der_decoder
  11. from pyasn1.codec.der.encoder import encode as der_encoder
  12. from pyasn1.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc2985
  15. from pyasn1_modules import rfc5280
  16. from pyasn1_modules import rfc5652
  17. from pyasn1_modules import rfc7292
  18. class PKCS9AttrsTestCase(unittest.TestCase):
  19. pem_text = """\
  20. MYIQjzAOBgNVBEExBwwFQWxpY2UwDwYIKwYBBQUHCQMxAxMBTTAQBgNVBAUxCRMH
  21. QjQ4LTAwNzAQBggrBgEFBQcJBDEEEwJVUzAQBggrBgEFBQcJBTEEEwJVUzARBgoq
  22. hkiG9w0BCRkEMQMCATAwFAYJKoZIhvcNAQkCMQcWBUFsaWNlMBgGCiqGSIb3DQEJ
  23. GQMxCgQIUTeqnHYky4AwHAYJKoZIhvcNAQkPMQ8wDTALBglghkgBZQMEAS0wHQYI
  24. KwYBBQUHCQExERgPMjAxOTA4MDMxMjAwMDBaMB0GCCsGAQUFBwkCMREMD0hlcm5k
  25. b24sIFZBLCBVUzApBgkqhkiG9w0BCRQxHB4aAEYAcgBpAGUAbgBkAGwAeQAgAE4A
  26. YQBtAGU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KAQKgggT+MIIE+jAcBgoqhkiG9w0BDAED
  58. MA4ECO6rT/7SnK61AgIH0ASCBNhl7+ZgGmaQO8qy97gTAhXCjVM2/iV3LHWodlbY
  59. iHqpAJj42/Uye/3B7TNROXine1DMI9ZeetIDzYiA52i0sh7PhjBeuCIqFwiRJIv7
  60. bIKYCgz6qSOIAgqr6XdQnpeFp97YqDgST/RGQel7obCNO115+SlelmBxwwSik60p
  61. AwslawMzunvvH9qafrIiTa2myQqpRj/ifxjESJNZxG1O2FiplAi36r3icotim3Sj
  62. zzRJU5+90SqnkogjtxODrQYkv6fqg3qGY/RuwAy+eT3V/z+UUoyL22w1T8qdSFsN
  63. WmMnAFCSGBuoHHoZ22ipItKVg09UzTCWe3CbUmEfjJuJDmw3Oo7sWVYLltxjCS86
  64. XHWAauyFjmMr9aNsDiloGnFKSChslF6Ktj0F6ohOe+iReW5vi16EeEzbQiTjakpr
  65. eQZoeajC/N+XGoT6jKxbk5r1dtnEEJ+Q4wnvSjiGpr6frr4T+4pw301sptOjfO3f
  66. F23rKk7Advvi3k5xZobHcRmzDSfT9X5agtKlc4HCnHTz7XKHstXb1o1DSgTNVWQX
  67. phhFBm10gx6zfEHaLqyMtqXbWe2TuIHMwnBWiLnbhIBn+hbxK4MCfVz3cBZbApks
  68. Au/lXcVnakOJBcCtx/MMfZ3kcnI3Hs6W8rM2ASeDBLIQLVduOc6xlVSoYUQ24NNr
  69. 9usfigQkcSTJZPIO52vPyIIQ7zR7U8TiqonkKWU3QJJVarPgLEYMUhBfNHqiGfx/
  70. d1Hf4MBoti8CMFUwsmOTv6d+cHYvQelqeFMXP0DE88gN/mkFBDAzXiXzAqMQcjJ+
  71. pyW6l4o2iQFSvXKSKg/IKved/hGp7RngQohjg4KlbqeGuRYea8Xs4pH5ue5KTeOc
  72. HGNI3Qi/Lmr2rd+e1iuGxwwYZHve6Z+Lxnb20zW9I/2MFm+KsCiB4Z/+x84jR7BG
  73. 8l//lpuc2D/vxnKTxaaUAdUXM0Zwze7e+Gc2lMhVG5TJWR1KY51vN5J+apDYc8IR
  74. 0L0c2bbkom3WkPq/po/dPDuoaX61nKmztUHaL5r5QZzBBwKVyhdw9J0btnWAFPNK
  75. vzgy5U9iV4+6jXH5TCmlIreszwRPoqqEaYRIfmUpp2+zy91PpzjTs98tx/HIAbOM
  76. fT3WmuTahEnEHehABhwq+S4xwzoVIskLbrcOP6l7UYYR7GTUCjKxh7ru0rSwHrqG
  77. 9t33YdzJaFbz+8jb88xtf454Rvur66Cew/4GYX9u1Zef0DF9So1ay3IicpOf5emo
  78. VWIwg4bh7bELi78i/MbdWtNZQcXimykfeTsYH8Q4u+1uxHS5pwEWWwKiUnLQVpZP
  79. 2ut255TdgSIhEILwsaLVelRrx/lp14EpY355FOusXiju6g14aWfBnt5udvuTXxDQ
  80. ZHPPNNk+gwzgvvTey98T941hYUctjg0NApJiB66bfrlYB9mkc5ftg5zqhEasYH5C
  81. 4ajKKRNMM7zGlwSZvy8PPhnAeE3Q9LTnos0l4ygjQD/kMlvd7XSLW3GUzjyxtkG4
  82. gQh6LGvnafAbgu7GpcapKEppN86sXEePHiQjj92n103+TxMYWwtaO4iAwkjqdEdt
  83. avEHcXRcpdqC0st6nUwPAPAC4LKJbZgLQnNG+wlWIiCMMD56IdfQ7r/zGIr13MxC
  84. kjNNUdISoWWE5GnQMYHbMBMGCSqGSIb3DQEJFTEGBAQBAAAAMFcGCSqGSIb3DQEJ
  85. FDFKHkgAMwBmADcAMQBhAGYANgA1AC0AMQA2ADgANwAtADQANAA0AGEALQA5AGYA
  86. NAA2AC0AYwA4AGIAZQAxADkANABjADMAZQA4AGUwawYJKwYBBAGCNxEBMV4eXABN
  87. AGkAYwByAG8AcwBvAGYAdAAgAEUAbgBoAGEAbgBjAGUAZAAgAEMAcgB5AHAAdABv
  88. AGcAcgBhAHAAaABpAGMAIABQAHIAbwB2AGkAZABlAHIAIAB2ADEALgAwMIIDaAYJ
  89. KoZIhvcNAQcBoIIDWQSCA1UwggNRMIIDTQYLKoZIhvcNAQwKAQOgggMlMIIDIQYK
  90. KoZIhvcNAQkWAaCCAxEEggMNMIIDCTCCAfGgAwIBAgIQNu32hzqhCKdHATXzboyI
  91. ETANBgkqhkiG9w0BAQUFADAUMRIwEAYDVQQDEwlhbm9ueW1vdXMwIBcNMTYwNzE5
  92. MjIwMDAxWhgPMjExNjA2MjUyMjAwMDFaMBQxEjAQBgNVBAMTCWFub255bW91czCC
  93. ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALy2sEJMGNdcDg6BI7mdFM5T
  94. lPzo5sKBzvUnagK5SKBJ11xMPN5toPTBzICB/XTWEB3AwpD0O+srSca+bsUAyedS
  95. 5V4BNp8qCyEu5RNRR8qPHheJ/guhLT96/gGI4jlrUyUhFntPkLKODxu+7KanMy6K
  96. dD+PVE8shXRUZTYe4PG64/c7z3wapnf4XoCXkJRzCY5f3MKz3Ul039kVnTlJcikd
  97. C7I9I9RflXLwXVl4nxUbeeRt6Z8WVWS4pCq+14v2aVPvP3mtVmAYHedRkvS04Hrx
  98. 4xx98D3NSSw6Z5OLkzqOcFw15fYmH2NLdhh34gSWJmaaCBAbuQ+1rx/42p7MvvsC
  99. AwEAAaNVMFMwFQYDVR0lBA4wDAYKKwYBBAGCNwoDBDAvBgNVHREEKDAmoCQGCisG
  100. AQQBgjcUAgOgFgwUYW5vbnltb3VzQHdpbmRvd3MteAAwCQYDVR0TBAIwADANBgkq
  101. hkiG9w0BAQUFAAOCAQEAuH7iqY0/MLozwFb39ILYAJDHE+HToZBQbHQP4YtienrU
  102. Stk60rIp0WH65lam7m/JhgAcItc/tV1L8mEnLrvvKcA+NeIL8sDOtM28azvgcOi0
  103. P3roeLLLRCuiykUaKmUcZEDm9cDYKIpJf7QetWQ3uuGTk9iRzpH79x2ix35BnyWQ
  104. Rr3INZzmX/+9YRvPBXKYl/89F/w1ORYArpI9XtjfuPWaGQmM4f1WRHE2t3qRyKFF
  105. ri7QiZdpcSx5zvsRHSyjfUMoKs+b6upk+P01lIhg/ewwYngGab+fZhF15pTNN2hx
  106. 8PdNGcrGzrkNKCmJKrWCa2xczuMA+z8SCuC1tYTKmDEVMBMGCSqGSIb3DQEJFTEG
  107. BAQBAAAAMDswHzAHBgUrDgMCGgQUpWCP/fZR0TK5BwGuqvTd0+duiKcEFJTubF2k
  108. HktMK+isIjxOTk4yJTOOAgIH0A==
  109. """
  110. def setUp(self):
  111. self.asn1Spec = rfc2985.AttributeSet()
  112. def testDerCodec(self):
  113. substrate = pem.readBase64fromText(self.pem_text)
  114. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  115. self.assertFalse(rest)
  116. self.assertTrue(asn1Object.prettyPrint())
  117. self.assertEqual(der_encoder(asn1Object), substrate)
  118. openTypesMap = {
  119. rfc2985.pkcs_9_at_smimeCapabilities: rfc2985.SMIMECapabilities(),
  120. }
  121. openTypesMap.update(rfc5280.certificateAttributesMap)
  122. openTypesMap.update(rfc5652.cmsAttributesMap)
  123. for attr in asn1Object:
  124. self.assertIn(attr['type'], openTypesMap)
  125. av, rest = der_decoder(
  126. attr['values'][0], asn1Spec=openTypesMap[attr['type']])
  127. self.assertFalse(rest)
  128. self.assertTrue(av.prettyPrint())
  129. self.assertEqual(attr['values'][0], der_encoder(av))
  130. if attr['type'] == rfc2985.pkcs_9_at_userPKCS12:
  131. self.assertEqual(univ.Integer(3), av['version'])
  132. self.assertEqual(rfc5652.id_data, av['authSafe']['contentType'])
  133. outdata, rest = der_decoder(
  134. av['authSafe']['content'], asn1Spec=univ.OctetString())
  135. self.assertFalse(rest)
  136. authsafe, rest = der_decoder(
  137. outdata, asn1Spec=rfc7292.AuthenticatedSafe())
  138. self.assertFalse(rest)
  139. for ci in authsafe:
  140. self.assertEqual(rfc5652.id_data, ci['contentType'])
  141. indata, rest = der_decoder(
  142. ci['content'], asn1Spec=univ.OctetString())
  143. self.assertFalse(rest)
  144. sc, rest = der_decoder(
  145. indata, asn1Spec=rfc7292.SafeContents())
  146. self.assertFalse(rest)
  147. for sb in sc:
  148. if sb['bagId'] in rfc7292.pkcs12BagTypeMap:
  149. bv, rest = der_decoder(
  150. sb['bagValue'], asn1Spec=rfc7292.pkcs12BagTypeMap[sb['bagId']])
  151. self.assertFalse(rest)
  152. for bagattr in sb['bagAttributes']:
  153. if bagattr['attrType'] in openTypesMap:
  154. inav, rest = der_decoder(
  155. bagattr['attrValues'][0], asn1Spec=openTypesMap[bagattr['attrType']])
  156. self.assertFalse(rest)
  157. if bagattr['attrType'] == rfc2985.pkcs_9_at_friendlyName:
  158. self.assertEqual( "3f71af65-1687-444a-9f46-c8be194c3e8e", inav)
  159. if bagattr['attrType'] == rfc2985.pkcs_9_at_localKeyId:
  160. self.assertEqual(univ.OctetString(hexValue='01000000'), inav)
  161. if attr['type'] == rfc2985.pkcs_9_at_pkcs7PDU:
  162. ci, rest = der_decoder(
  163. attr['values'][0], asn1Spec=rfc5652.ContentInfo())
  164. self.assertFalse(rest)
  165. self.assertEqual(rfc5652.id_signedData, ci['contentType'])
  166. sd, rest = der_decoder(
  167. ci['content'], asn1Spec=rfc5652.SignedData())
  168. self.assertFalse(rest)
  169. self.assertEqual(1, sd['version'])
  170. for si in sd['signerInfos']:
  171. self.assertEqual(1, si['version'])
  172. for siattr in si['signedAttrs']:
  173. if siattr['attrType'] in openTypesMap:
  174. siav, rest = der_decoder(
  175. siattr['attrValues'][0], asn1Spec=openTypesMap[siattr['attrType']])
  176. self.assertFalse(rest)
  177. if siattr['attrType'] == rfc2985.pkcs_9_at_contentType:
  178. self.assertEqual(rfc5652.id_data, siav)
  179. if siattr['attrType'] == rfc2985.pkcs_9_at_messageDigest:
  180. self.assertEqual('b6e422a4', siav.prettyPrint()[2:10])
  181. if siattr['attrType'] == rfc2985.pkcs_9_at_signingTime:
  182. self.assertEqual('190529182319Z', siav['utcTime'])
  183. for choices in sd['certificates']:
  184. for rdn in choices[0]['tbsCertificate']['subject']['rdnSequence']:
  185. if rdn[0]['type'] in openTypesMap:
  186. nv, rest = der_decoder(
  187. rdn[0]['value'], asn1Spec=openTypesMap[rdn[0]['type']])
  188. self.assertFalse(rest)
  189. if rdn[0]['type'] == rfc2985.pkcs_9_at_emailAddress:
  190. self.assertEqual('alice@example.com', nv)
  191. def testOpenTypes(self):
  192. openTypesMap = {
  193. rfc2985.pkcs_9_at_smimeCapabilities: rfc2985.SMIMECapabilities(),
  194. }
  195. openTypesMap.update(rfc5280.certificateAttributesMap)
  196. openTypesMap.update(rfc5652.cmsAttributesMap)
  197. substrate = pem.readBase64fromText(self.pem_text)
  198. asn1Object, rest = der_decoder(
  199. substrate, asn1Spec=self.asn1Spec,
  200. openTypes=openTypesMap, decodeOpenTypes=True)
  201. self.assertFalse(rest)
  202. self.assertTrue(asn1Object.prettyPrint())
  203. self.assertEqual(substrate, der_encoder(asn1Object))
  204. for attr in asn1Object:
  205. self.assertTrue(attr['type'], openTypesMap)
  206. if attr['type'] == rfc2985.pkcs_9_at_userPKCS12:
  207. self.assertEqual(univ.Integer(3), attr['values'][0]['version'])
  208. self.assertEqual(rfc5652.id_data, attr['values'][0]['authSafe']['contentType'])
  209. authsafe, rest = der_decoder(
  210. attr['values'][0]['authSafe']['content'],
  211. asn1Spec=rfc7292.AuthenticatedSafe())
  212. self.assertFalse(rest)
  213. for ci in authsafe:
  214. self.assertEqual(rfc5652.id_data, ci['contentType'])
  215. indata, rest = der_decoder(
  216. ci['content'], asn1Spec=univ.OctetString())
  217. self.assertFalse(rest)
  218. sc, rest = der_decoder(
  219. indata, asn1Spec=rfc7292.SafeContents(), decodeOpenTypes=True)
  220. self.assertFalse(rest)
  221. for sb in sc:
  222. if sb['bagId'] in rfc7292.pkcs12BagTypeMap:
  223. for bagattr in sb['bagAttributes']:
  224. if bagattr['attrType'] in openTypesMap:
  225. if bagattr['attrType'] == rfc2985.pkcs_9_at_friendlyName:
  226. self.assertEqual(
  227. "3f71af65-1687-444a-9f46-c8be194c3e8e",
  228. bagattr['attrValues'][0])
  229. if bagattr['attrType'] == rfc2985.pkcs_9_at_localKeyId:
  230. self.assertEqual(
  231. univ.OctetString(hexValue='01000000'),
  232. bagattr['attrValues'][0])
  233. if attr['type'] == rfc2985.pkcs_9_at_pkcs7PDU:
  234. self.assertEqual(rfc5652.id_signedData, attr['values'][0]['contentType'])
  235. self.assertEqual(1, attr['values'][0]['content']['version'])
  236. for si in attr['values'][0]['content']['signerInfos']:
  237. self.assertEqual(1, si['version'])
  238. for siattr in si['signedAttrs']:
  239. if siattr['attrType'] in openTypesMap:
  240. if siattr['attrType'] == rfc2985.pkcs_9_at_contentType:
  241. self.assertEqual(rfc5652.id_data, siattr['attrValues'][0])
  242. if siattr['attrType'] == rfc2985.pkcs_9_at_messageDigest:
  243. self.assertEqual('b6e422a4', siattr['attrValues'][0].prettyPrint()[2:10])
  244. if siattr['attrType'] == rfc2985.pkcs_9_at_signingTime:
  245. self.assertEqual('190529182319Z', siattr['attrValues'][0]['utcTime'])
  246. for choices in attr['values'][0]['content']['certificates']:
  247. for rdn in choices[0]['tbsCertificate']['subject']['rdnSequence']:
  248. if rdn[0]['type'] in openTypesMap:
  249. if rdn[0]['type'] == rfc2985.pkcs_9_at_emailAddress:
  250. self.assertEqual('alice@example.com', rdn[0]['value'])
  251. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  252. if __name__ == '__main__':
  253. unittest.TextTestRunner(verbosity=2).run(suite)