1# coding: utf-8 2from __future__ import unicode_literals, division, absolute_import, print_function 3 4import unittest 5import os 6from datetime import datetime 7 8from asn1crypto import tsp, cms, util 9from ._unittest_compat import patch 10 11patch() 12 13tests_root = os.path.dirname(__file__) 14fixtures_dir = os.path.join(tests_root, 'fixtures') 15 16 17class TSPTests(unittest.TestCase): 18 19 def test_parse_request(self): 20 with open(os.path.join(fixtures_dir, 'tsp_request'), 'rb') as f: 21 request = tsp.TimeStampReq.load(f.read()) 22 23 self.assertEqual( 24 'v1', 25 request['version'].native 26 ) 27 self.assertEqual( 28 'sha1', 29 request['message_imprint']['hash_algorithm']['algorithm'].native 30 ) 31 self.assertEqual( 32 None, 33 request['message_imprint']['hash_algorithm']['parameters'].native 34 ) 35 self.assertEqual( 36 b'\x53\xC9\xDB\xC1\x6D\xDB\x34\x3B\x28\x4E\xEF\xA6\x03\x0E\x02\x64\x79\x31\xAF\xFB', 37 request['message_imprint']['hashed_message'].native 38 ) 39 self.assertEqual( 40 17842879675353045770, 41 request['nonce'].native 42 ) 43 44 def test_parse_response(self): 45 with open(os.path.join(fixtures_dir, 'tsp_response'), 'rb') as f: 46 response = tsp.TimeStampResp.load(f.read()) 47 48 status_info = response['status'] 49 token = response['time_stamp_token'] 50 signed_data = token['content'] 51 encap_content_info = signed_data['encap_content_info'] 52 tst_info = encap_content_info['content'].parsed 53 signer_infos = signed_data['signer_infos'] 54 signer_info = signer_infos[0] 55 signed_attrs = signer_info['signed_attrs'] 56 57 self.assertEqual( 58 'granted', 59 status_info['status'].native 60 ) 61 self.assertEqual( 62 None, 63 status_info['status_string'].native 64 ) 65 self.assertEqual( 66 None, 67 status_info['fail_info'].native 68 ) 69 self.assertEqual( 70 'signed_data', 71 token['content_type'].native 72 ) 73 self.assertIsInstance( 74 signed_data, 75 cms.SignedData 76 ) 77 self.assertEqual( 78 'v3', 79 signed_data['version'].native 80 ) 81 self.assertEqual( 82 'sha1', 83 signed_data['digest_algorithms'][0]['algorithm'].native 84 ) 85 self.assertEqual( 86 'tst_info', 87 encap_content_info['content_type'].native 88 ) 89 self.assertIsInstance( 90 tst_info, 91 tsp.TSTInfo 92 ) 93 self.assertEqual( 94 'v1', 95 tst_info['version'].native 96 ) 97 self.assertEqual( 98 '1.1.2', 99 tst_info['policy'].native 100 ) 101 self.assertEqual( 102 'sha1', 103 tst_info['message_imprint']['hash_algorithm']['algorithm'].native 104 ) 105 self.assertEqual( 106 None, 107 tst_info['message_imprint']['hash_algorithm']['parameters'].native 108 ) 109 self.assertEqual( 110 b'\x53\xC9\xDB\xC1\x6D\xDB\x34\x3B\x28\x4E\xEF\xA6\x03\x0E\x02\x64\x79\x31\xAF\xFB', 111 tst_info['message_imprint']['hashed_message'].native 112 ) 113 self.assertEqual( 114 544918635, 115 tst_info['serial_number'].native 116 ) 117 self.assertEqual( 118 datetime(2015, 6, 1, 18, 39, 55, tzinfo=util.timezone.utc), 119 tst_info['gen_time'].native 120 ) 121 self.assertEqual( 122 60, 123 tst_info['accuracy']['seconds'].native 124 ) 125 self.assertEqual( 126 None, 127 tst_info['accuracy']['millis'].native 128 ) 129 self.assertEqual( 130 None, 131 tst_info['accuracy']['micros'].native 132 ) 133 self.assertEqual( 134 False, 135 tst_info['ordering'].native 136 ) 137 self.assertEqual( 138 17842879675353045770, 139 tst_info['nonce'].native 140 ) 141 self.assertEqual( 142 util.OrderedDict([ 143 ('country_name', 'US'), 144 ('organization_name', 'GeoTrust Inc'), 145 ('common_name', 'GeoTrust Timestamping Signer 1'), 146 ]), 147 tst_info['tsa'].native 148 ) 149 self.assertEqual( 150 None, 151 tst_info['extensions'].native 152 ) 153 self.assertEqual( 154 None, 155 signed_data['certificates'].native 156 ) 157 self.assertEqual( 158 None, 159 signed_data['crls'].native 160 ) 161 self.assertEqual( 162 1, 163 len(signer_infos) 164 ) 165 self.assertEqual( 166 'v1', 167 signer_info['version'].native 168 ) 169 self.assertEqual( 170 util.OrderedDict([ 171 ( 172 'issuer', 173 util.OrderedDict([ 174 ('country_name', 'ZA'), 175 ('state_or_province_name', 'Western Cape'), 176 ('locality_name', 'Durbanville'), 177 ('organization_name', 'Thawte'), 178 ('organizational_unit_name', 'Thawte Certification'), 179 ('common_name', 'Thawte Timestamping CA'), 180 ]) 181 ), 182 ( 183 'serial_number', 184 125680471847352264461591953321128732863 185 ) 186 ]), 187 signer_info['sid'].native 188 ) 189 self.assertEqual( 190 'sha1', 191 signer_info['digest_algorithm']['algorithm'].native 192 ) 193 self.assertEqual( 194 4, 195 len(signed_attrs) 196 ) 197 self.assertEqual( 198 'content_type', 199 signed_attrs[0]['type'].native 200 ) 201 self.assertEqual( 202 'tst_info', 203 signed_attrs[0]['values'][0].native 204 ) 205 self.assertEqual( 206 'signing_time', 207 signed_attrs[1]['type'].native 208 ) 209 self.assertEqual( 210 datetime(2015, 6, 1, 18, 39, 55, tzinfo=util.timezone.utc), 211 signed_attrs[1]['values'][0].native 212 ) 213 self.assertEqual( 214 'message_digest', 215 signed_attrs[2]['type'].native 216 ) 217 self.assertEqual( 218 b'\x22\x06\x7D\xA4\xFC\x7B\xC5\x94\x80\xB4\xB0\x78\xC2\x07\x66\x02\xA3\x0D\x62\xAE', 219 signed_attrs[2]['values'][0].native 220 ) 221 self.assertEqual( 222 'signing_certificate', 223 signed_attrs[3]['type'].native 224 ) 225 self.assertEqual( 226 util.OrderedDict([ 227 ( 228 'certs', 229 [ 230 util.OrderedDict([ 231 ( 232 'cert_hash', 233 b'\x22\x3C\xDA\x27\x07\x96\x73\x81\x6B\x60\x8A\x1B\x8C\xB0\xAB\x02\x30\x10\x7F\xCC' 234 ), 235 ('issuer_serial', None), 236 ]) 237 ] 238 ), 239 ( 240 'policies', 241 None 242 ) 243 ]), 244 signed_attrs[3]['values'][0].native 245 ) 246