1# coding: utf-8 2from __future__ import unicode_literals, division, absolute_import, print_function 3 4import os 5import sys 6import unittest 7 8from asn1crypto import keys, core, util 9 10from .unittest_data import data_decorator, data 11from ._unittest_compat import patch 12 13patch() 14 15if sys.version_info < (3,): 16 int_types = (int, long) # noqa 17else: 18 int_types = int 19 20tests_root = os.path.dirname(__file__) 21fixtures_dir = os.path.join(tests_root, 'fixtures') 22 23 24@data_decorator 25class KeysTests(unittest.TestCase): 26 27 def test_parse_rsa_private_key(self): 28 with open(os.path.join(fixtures_dir, 'keys/test-der.key'), 'rb') as f: 29 key = keys.RSAPrivateKey.load(f.read()) 30 31 self.assertEqual( 32 'two-prime', 33 key['version'].native 34 ) 35 self.assertEqual( 36 23903990516906431865559598284199534387004799030432486061102966678620221767754702651554142956492614440585611990224871381291841413369032752409360196079700921141819811294444393525264295297988924243231844876926173670633422654261873814968313363171188082579071492839040415373948505938897419917635370450127498164824808630475648771544810334682447182123219422360569466851807131368135806769502898151721274383486320505905826683946456552230958810028663378886363555981449715929872558073101554364803925363048965464124465016494920967179276744892632783712377912841537032383450409486298694116013299423220523450956288827030007092359007, # noqa 37 key['modulus'].native 38 ) 39 self.assertEqual( 40 65537, 41 key['public_exponent'].native 42 ) 43 self.assertEqual( 44 9979296894007354255484849917690758820642557661666429934720496335307525025035760937280030384204921358841911348590147260206368632524783497961763507098900120579828036636037636859350155169644276779450131617753331883188587268575077705380671279069284616924232052795766448946873233783789819627790465470123569125678598045748629782316184667685110712273519313310937077963014676074966877849272992367512921997850502687035430136911690081438185238817835171119161013656103255853961444458012340770881411877222316871444386486841632394098449378506206645681449475758856053641206175913163492821894709155329556294181613669730336931773953, # noqa 45 key['private_exponent'].native 46 ) 47 self.assertEqual( 48 166647390172913547327716251713919741459272587597255782032652236515036001974461323181989715320980256918783849999012066159723695368018857439366733087649658067943054926668058248612521531843495934099419046629521378187012692776633310821178903471282399402138521150042979117060141563972064613977168440186057796106743, # noqa 49 key['prime1'].native 50 ) 51 self.assertEqual( 52 143440533284701431115857974625778819273481773744021067505004499855263691219807413711274106281992493130281690570930126889424222979194828112331057105055939481042398415265558356642606674863401518188395487842736496447305100392269029249928750130190700690239916449523411304928539660679996452045625683879143320460249, # noqa 53 key['prime2'].native 54 ) 55 self.assertEqual( 56 109414079859473229289779858629449815451592843305649008118818271892297238643195390011716060554289324731958287404176117228233683079641781234394481865640434212819044363330635799312574408253258259431525735957118503776629524657609514187779529692628749620437591384488141789034909003405007374076072765197764330205487, # noqa 57 key['exponent1'].native 58 ) 59 self.assertEqual( 60 39361498857013145813625735320048312950154816653378623953034178027634194773898965899927575680536994315500952488328843279054659597751495930118280223039291020752651068863936425009698924893471060669547041417272275998418220630400632040385105243470857091616562513209775072216226822370097138922876120342440353924609, # noqa 61 key['exponent2'].native 62 ) 63 self.assertEqual( 64 109796662729796355370195012683418958273962986010546166376879205603219777065076464250440708895625560840314914603409569660942497623175203159192440744329997446961447023349392064212216532091513743978251892999757210494211477167363008686808094766092274115601607346901935491774285446659775729268493276413171032997893, # noqa 65 key['coefficient'].native 66 ) 67 self.assertEqual( 68 None, 69 key['other_prime_infos'].native 70 ) 71 72 def test_parse_rsa_private_key_no_spec(self): 73 with open(os.path.join(fixtures_dir, 'keys/test-der.key'), 'rb') as f: 74 key = core.Asn1Value.load(f.read()) 75 76 self.assertEqual( 77 0, 78 key[0].native 79 ) 80 self.assertEqual( 81 23903990516906431865559598284199534387004799030432486061102966678620221767754702651554142956492614440585611990224871381291841413369032752409360196079700921141819811294444393525264295297988924243231844876926173670633422654261873814968313363171188082579071492839040415373948505938897419917635370450127498164824808630475648771544810334682447182123219422360569466851807131368135806769502898151721274383486320505905826683946456552230958810028663378886363555981449715929872558073101554364803925363048965464124465016494920967179276744892632783712377912841537032383450409486298694116013299423220523450956288827030007092359007, # noqa 82 key[1].native 83 ) 84 self.assertEqual( 85 65537, 86 key[2].native 87 ) 88 self.assertEqual( 89 9979296894007354255484849917690758820642557661666429934720496335307525025035760937280030384204921358841911348590147260206368632524783497961763507098900120579828036636037636859350155169644276779450131617753331883188587268575077705380671279069284616924232052795766448946873233783789819627790465470123569125678598045748629782316184667685110712273519313310937077963014676074966877849272992367512921997850502687035430136911690081438185238817835171119161013656103255853961444458012340770881411877222316871444386486841632394098449378506206645681449475758856053641206175913163492821894709155329556294181613669730336931773953, # noqa 90 key[3].native 91 ) 92 self.assertEqual( 93 166647390172913547327716251713919741459272587597255782032652236515036001974461323181989715320980256918783849999012066159723695368018857439366733087649658067943054926668058248612521531843495934099419046629521378187012692776633310821178903471282399402138521150042979117060141563972064613977168440186057796106743, # noqa 94 key[4].native 95 ) 96 self.assertEqual( 97 143440533284701431115857974625778819273481773744021067505004499855263691219807413711274106281992493130281690570930126889424222979194828112331057105055939481042398415265558356642606674863401518188395487842736496447305100392269029249928750130190700690239916449523411304928539660679996452045625683879143320460249, # noqa 98 key[5].native 99 ) 100 self.assertEqual( 101 109414079859473229289779858629449815451592843305649008118818271892297238643195390011716060554289324731958287404176117228233683079641781234394481865640434212819044363330635799312574408253258259431525735957118503776629524657609514187779529692628749620437591384488141789034909003405007374076072765197764330205487, # noqa 102 key[6].native 103 ) 104 self.assertEqual( 105 39361498857013145813625735320048312950154816653378623953034178027634194773898965899927575680536994315500952488328843279054659597751495930118280223039291020752651068863936425009698924893471060669547041417272275998418220630400632040385105243470857091616562513209775072216226822370097138922876120342440353924609, # noqa 106 key[7].native 107 ) 108 self.assertEqual( 109 109796662729796355370195012683418958273962986010546166376879205603219777065076464250440708895625560840314914603409569660942497623175203159192440744329997446961447023349392064212216532091513743978251892999757210494211477167363008686808094766092274115601607346901935491774285446659775729268493276413171032997893, # noqa 110 key[8].native 111 ) 112 113 with self.assertRaises(KeyError): 114 key[9].native 115 116 def test_parse_dsa_private_key(self): 117 with open(os.path.join(fixtures_dir, 'keys/test-dsa-der.key'), 'rb') as f: 118 key = keys.DSAPrivateKey.load(f.read()) 119 120 self.assertEqual( 121 0, 122 key['version'].native 123 ) 124 self.assertEqual( 125 4511743893397705393934377497936985478231822206263141826261443300639402520800626925517264115785551703273809312112372693877437137848393530691841757974971843334497076835630893064661599193178307024379015589119302113551197423138934242435710226975119594589912289060014025377813473273600967729027125618396732574594753039493158066887433778053086408525146692226448554390096911703556213619406958876388642882534250747780313634767409586007581976273681005928967585750017105562145167146445061803488570714706090280814293902464230717946651489964409785146803791743658888866280873858000476717727810363942159874283767926511678640730707887895260274767195555813448140889391762755466967436731106514029224490921857229134393798015954890071206959203407845438863870686180087606429828973298318856683615900474921310376145478859687052812749087809700610549251964102790514588562086548577933609968589710807989944739877028770343142449461177732058649962678857, # noqa 126 key['p'].native 127 ) 128 self.assertEqual( 129 71587850165936478337655415373676526523562874562337607790945426056266440596923, 130 key['q'].native 131 ) 132 self.assertEqual( 133 761437146067908309288345767887973163494473925243194806582679580640442238588269326525839153095505341738937595419375068472941615006110237832663093084973431440436421580371384720052414080562019831325744042316268714195397974084616335082272743706567701546951285088540646372701485690904535540223121118329044403681933304838754517522024738251994717369464179515923093116622352823578284891812676662979104509631349201801577889230316128523885862472086364717411346341249139971907827526291913249445756671582283459372536334490171231311487207683108274785825764378203622999309355578169139646003751751448501475767709869676880946562283552431757983801739671783678927397420797147373441051876558068212062253171347849380506793433921881336652424898488378657239798694995315456959568806256079056461448199493507273882763491729787817044805150879660784158902456811649964987582162907020243296662602990514615480712948126671999033658064244112238138589732202, # noqa 134 key['g'].native 135 ) 136 self.assertEqual( 137 934231235067929794039535952071098031636053793876274937162425423023735221571983693370780054696865229184537343792766496068557051933738826401423094028670222490622041397241325320965905259541032379046252395145258594355589801644789631904099105867133976990593761395721476198083091062806327384261369876465927159169400428623265291958463077792777155465482611741502621885386691681062128487785344975981628995609792181581218570320181053055516069553767918513262908069925035292416868414952256645902605335068760774106734518308281769128146479819566784704033671969858507248124850451414380441279385481154336362988505436125981975735568289420374790767927084033441728922597082155884801013899630856890463962357814273014111039522903328923758417820349377075487103441305806369234738881875734407495707878637895190993370257589211331043479113328811265005530361001980539377903738453549980082795009589559114091215518866106998956304437954236070776810740036, # noqa 138 key['public_key'].native 139 ) 140 self.assertEqual( 141 67419307522580891944110478232775481982040250615628832761657973309422062357004, 142 key['private_key'].native 143 ) 144 145 def test_parse_ec_private_key(self): 146 with open(os.path.join(fixtures_dir, 'keys/test-ec-der.key'), 'rb') as f: 147 key = keys.ECPrivateKey.load(f.read()) 148 149 self.assertEqual( 150 'ecPrivkeyVer1', 151 key['version'].native 152 ) 153 self.assertEqual( 154 105342176757643535635985202437872662036661123763048203788770333621775587689309, 155 key['private_key'].native 156 ) 157 self.assertEqual( 158 util.OrderedDict([ 159 ('version', 'ecdpVer1'), 160 ( 161 'field_id', 162 util.OrderedDict([ 163 ('field_type', 'prime_field'), 164 ('parameters', 115792089210356248762697446949407573530086143415290314195533631308867097853951) 165 ]) 166 ), 167 ( 168 'curve', 169 util.OrderedDict([ 170 ( 171 'a', 172 b'\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00' 173 b'\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC' 174 ), 175 ( 176 'b', 177 b'\x5A\xC6\x35\xD8\xAA\x3A\x93\xE7\xB3\xEB\xBD\x55\x76\x98\x86\xBC' 178 b'\x65\x1D\x06\xB0\xCC\x53\xB0\xF6\x3B\xCE\x3C\x3E\x27\xD2\x60\x4B' 179 ), 180 ('seed', b'\xC4\x9D\x36\x08\x86\xE7\x04\x93\x6A\x66\x78\xE1\x13\x9D\x26\xB7\x81\x9F\x7E\x90'), 181 ]) 182 ), 183 ( 184 'base', 185 b'\x04\x6B\x17\xD1\xF2\xE1\x2C\x42\x47\xF8\xBC\xE6\xE5\x63\xA4\x40\xF2\x77' 186 b'\x03\x7D\x81\x2D\xEB\x33\xA0\xF4\xA1\x39\x45\xD8\x98\xC2\x96\x4F\xE3\x42' 187 b'\xE2\xFE\x1A\x7F\x9B\x8E\xE7\xEB\x4A\x7C\x0F\x9E\x16\x2B\xCE\x33\x57\x6B' 188 b'\x31\x5E\xCE\xCB\xB6\x40\x68\x37\xBF\x51\xF5' 189 ), 190 ( 191 'order', 192 115792089210356248762697446949407573529996955224135760342422259061068512044369 193 ), 194 ('cofactor', 1), 195 ('hash', None), 196 ]), 197 key['parameters'].native 198 ) 199 self.assertEqual( 200 b'\x04\x8B\x5D\x4C\x71\xF7\xD6\xC6\xA3\x49\x63\x42\x5C\x47\x9F\xCB\x73\x24\x1D\xC9\xDD' 201 b'\xD1\x2D\xF1\x3A\x9F\xB7\x04\xDE\x20\xD0\x58\x00\x93\x54\xF6\x89\xC7\x2F\x87\x2B\xF7' 202 b'\xF9\x3D\x3B\x34\xED\x9E\x7B\x0E\x3D\x57\x42\xDF\x78\x03\x0B\xCC\x31\xC6\x03\xD7\x9F' 203 b'\x60\x01', 204 key['public_key'].native 205 ) 206 207 def test_parse_rsa_public_key(self): 208 with open(os.path.join(fixtures_dir, 'keys/test-public-rsa-der.key'), 'rb') as f: 209 key = keys.RSAPublicKey.load(f.read()) 210 211 self.assertEqual( 212 23903990516906431865559598284199534387004799030432486061102966678620221767754702651554142956492614440585611990224871381291841413369032752409360196079700921141819811294444393525264295297988924243231844876926173670633422654261873814968313363171188082579071492839040415373948505938897419917635370450127498164824808630475648771544810334682447182123219422360569466851807131368135806769502898151721274383486320505905826683946456552230958810028663378886363555981449715929872558073101554364803925363048965464124465016494920967179276744892632783712377912841537032383450409486298694116013299423220523450956288827030007092359007, # noqa 213 key['modulus'].native 214 ) 215 self.assertEqual( 216 65537, 217 key['public_exponent'].native 218 ) 219 220 def test_parse_public_key_info(self): 221 with open(os.path.join(fixtures_dir, 'keys/test-public-der.key'), 'rb') as f: 222 key = keys.PublicKeyInfo.load(f.read()) 223 224 public_key = key['public_key'].parsed 225 226 self.assertEqual( 227 'rsa', 228 key['algorithm']['algorithm'].native 229 ) 230 self.assertEqual( 231 None, 232 key['algorithm']['parameters'].native 233 ) 234 self.assertEqual( 235 23903990516906431865559598284199534387004799030432486061102966678620221767754702651554142956492614440585611990224871381291841413369032752409360196079700921141819811294444393525264295297988924243231844876926173670633422654261873814968313363171188082579071492839040415373948505938897419917635370450127498164824808630475648771544810334682447182123219422360569466851807131368135806769502898151721274383486320505905826683946456552230958810028663378886363555981449715929872558073101554364803925363048965464124465016494920967179276744892632783712377912841537032383450409486298694116013299423220523450956288827030007092359007, # noqa 236 public_key['modulus'].native 237 ) 238 self.assertEqual( 239 65537, 240 public_key['public_exponent'].native 241 ) 242 243 def test_parse_pkcs8_private_key(self): 244 with open(os.path.join(fixtures_dir, 'keys/test-pkcs8-der.key'), 'rb') as f: 245 key_info = keys.PrivateKeyInfo.load(f.read()) 246 247 key = key_info['private_key'].parsed 248 249 self.assertEqual( 250 0, 251 key_info['version'].native 252 ) 253 self.assertEqual( 254 'rsa', 255 key_info['private_key_algorithm']['algorithm'].native 256 ) 257 self.assertEqual( 258 None, 259 key_info['private_key_algorithm']['parameters'].native 260 ) 261 262 self.assertEqual( 263 'two-prime', 264 key['version'].native 265 ) 266 self.assertEqual( 267 23903990516906431865559598284199534387004799030432486061102966678620221767754702651554142956492614440585611990224871381291841413369032752409360196079700921141819811294444393525264295297988924243231844876926173670633422654261873814968313363171188082579071492839040415373948505938897419917635370450127498164824808630475648771544810334682447182123219422360569466851807131368135806769502898151721274383486320505905826683946456552230958810028663378886363555981449715929872558073101554364803925363048965464124465016494920967179276744892632783712377912841537032383450409486298694116013299423220523450956288827030007092359007, # noqa 268 key['modulus'].native 269 ) 270 self.assertEqual( 271 65537, 272 key['public_exponent'].native 273 ) 274 self.assertEqual( 275 9979296894007354255484849917690758820642557661666429934720496335307525025035760937280030384204921358841911348590147260206368632524783497961763507098900120579828036636037636859350155169644276779450131617753331883188587268575077705380671279069284616924232052795766448946873233783789819627790465470123569125678598045748629782316184667685110712273519313310937077963014676074966877849272992367512921997850502687035430136911690081438185238817835171119161013656103255853961444458012340770881411877222316871444386486841632394098449378506206645681449475758856053641206175913163492821894709155329556294181613669730336931773953, # noqa 276 key['private_exponent'].native 277 ) 278 self.assertEqual( 279 166647390172913547327716251713919741459272587597255782032652236515036001974461323181989715320980256918783849999012066159723695368018857439366733087649658067943054926668058248612521531843495934099419046629521378187012692776633310821178903471282399402138521150042979117060141563972064613977168440186057796106743, # noqa 280 key['prime1'].native 281 ) 282 self.assertEqual( 283 143440533284701431115857974625778819273481773744021067505004499855263691219807413711274106281992493130281690570930126889424222979194828112331057105055939481042398415265558356642606674863401518188395487842736496447305100392269029249928750130190700690239916449523411304928539660679996452045625683879143320460249, # noqa 284 key['prime2'].native 285 ) 286 self.assertEqual( 287 109414079859473229289779858629449815451592843305649008118818271892297238643195390011716060554289324731958287404176117228233683079641781234394481865640434212819044363330635799312574408253258259431525735957118503776629524657609514187779529692628749620437591384488141789034909003405007374076072765197764330205487, # noqa 288 key['exponent1'].native 289 ) 290 self.assertEqual( 291 39361498857013145813625735320048312950154816653378623953034178027634194773898965899927575680536994315500952488328843279054659597751495930118280223039291020752651068863936425009698924893471060669547041417272275998418220630400632040385105243470857091616562513209775072216226822370097138922876120342440353924609, # noqa 292 key['exponent2'].native 293 ) 294 self.assertEqual( 295 109796662729796355370195012683418958273962986010546166376879205603219777065076464250440708895625560840314914603409569660942497623175203159192440744329997446961447023349392064212216532091513743978251892999757210494211477167363008686808094766092274115601607346901935491774285446659775729268493276413171032997893, # noqa 296 key['coefficient'].native 297 ) 298 self.assertEqual( 299 None, 300 key['other_prime_infos'].native 301 ) 302 303 self.assertEqual( 304 None, 305 key_info['attributes'].native 306 ) 307 308 def test_parse_rsapss_private_key(self): 309 with open(os.path.join(fixtures_dir, 'keys/test-rsapss-der.key'), 'rb') as f: 310 key_info = keys.PrivateKeyInfo.load(f.read()) 311 312 key = key_info['private_key'].parsed 313 314 self.assertEqual( 315 0, 316 key_info['version'].native 317 ) 318 self.assertEqual( 319 'rsassa_pss', 320 key_info['private_key_algorithm']['algorithm'].native 321 ) 322 self.assertEqual( 323 None, 324 key_info['private_key_algorithm']['parameters'].native 325 ) 326 327 self.assertEqual( 328 'two-prime', 329 key['version'].native 330 ) 331 self.assertEqual( 332 20334810015710919160110203472269180092101382951468058535601491502957196266577250503666807938732810152931665713052098820680792829137564325868564844098687045650387144565108903086036194735310494097581552241575174798917880615962200904076841064384200149608953782976948109759991080721261141139715447415148530436086884795768009560076896590825433136132086023781159444716805738553676228393667377624295683128237093827752550284339271476658714835879903906034493875531632793284572104031230158276531850092876884395075794398068537347947800593962574809516836581297669594643468201529164877789603529698620577572178907861813134904392181, # noqa 333 key['modulus'].native 334 ) 335 self.assertEqual( 336 65537, 337 key['public_exponent'].native 338 ) 339 self.assertEqual( 340 19811367921985171557639752989981035886303512541789150212828710994763522615025976847568941008714007785902419332260807020468874408966438534060269241736746690644631569655037665166904359886012100769497873119376457740069070560586943676477505866318738720913860857882999478282122015106772111353446622784949473859714808146533832277397219231218258638918521475883551912394494264506377559745603922894963456171825545032908365582944199734667178542763963194351614183530759037228600105514522819433425764227915014375970397879315537366008672232442295229043876987446583754589361036423305704619726617664187630589314612553217357586095673, # noqa 341 key['private_exponent'].native 342 ) 343 self.assertEqual( 344 145062186227663059634108314593892541355080853648164075820395373006330022883408993468365984286369578851636705799765757665015182142763055043654284213839887910732213256250809510746337738407165996181392718941356683486810092456676083857188565619344293262177288309348259896401807590237461717880393098789423620650939, # noqa 345 key['prime1'].native 346 ) 347 self.assertEqual( 348 140179950023620372289001596962713930540779028054089057618536399863850868080064249195053602322991362108187576825895413419966213531630187432159266399149913629896819277637422106295703267471029328291865017941552279870382011332512626586060449095917164740367589115287472025339179557750935025294415109144213020312079, # noqa 349 key['prime2'].native 350 ) 351 self.assertEqual( 352 118228658851708114001194157738654137417646348120344781510758784408198602961600439097293142570946864897406396441532083859790972106955549111215800799518497533665722246507785513633594518505277393228754912332478232018012333162654627815552589285314495327920681107702945726939074883271186966123919571825659906212509, # noqa 353 key['exponent1'].native 354 ) 355 self.assertEqual( 356 56878789554421364113540907677075374840783006759759162308194149033058002105452927576710337564627405910873614034121348759689054278241450542380322750296695046251983127560528078041645807537568272852545501885984378691627606471980343411760066258123338644976958508227786686876412756148631524064712858116223089798721, # noqa 357 key['exponent2'].native 358 ) 359 self.assertEqual( 360 23480707628058872067473220975854826046220552607063059593257976510053338333806071359463231176605785818753563067398907246278690942690250152695883594601176151883590956534074071491193074275985805378044282321604348476199853682247297755042167691612551582210509658456585074900583647465600111554502893125233815233234, # noqa 361 key['coefficient'].native 362 ) 363 self.assertEqual( 364 None, 365 key['other_prime_infos'].native 366 ) 367 368 self.assertEqual( 369 None, 370 key_info['attributes'].native 371 ) 372 373 def test_parse_rsapss_public_key_info(self): 374 with open(os.path.join(fixtures_dir, 'keys/test-public-rsapss-der.key'), 'rb') as f: 375 key = keys.PublicKeyInfo.load(f.read()) 376 377 public_key = key['public_key'].parsed 378 379 self.assertEqual( 380 'rsassa_pss', 381 key['algorithm']['algorithm'].native 382 ) 383 self.assertEqual( 384 None, 385 key['algorithm']['parameters'].native 386 ) 387 self.assertEqual( 388 20334810015710919160110203472269180092101382951468058535601491502957196266577250503666807938732810152931665713052098820680792829137564325868564844098687045650387144565108903086036194735310494097581552241575174798917880615962200904076841064384200149608953782976948109759991080721261141139715447415148530436086884795768009560076896590825433136132086023781159444716805738553676228393667377624295683128237093827752550284339271476658714835879903906034493875531632793284572104031230158276531850092876884395075794398068537347947800593962574809516836581297669594643468201529164877789603529698620577572178907861813134904392181, # noqa 389 public_key['modulus'].native 390 ) 391 self.assertEqual( 392 65537, 393 public_key['public_exponent'].native 394 ) 395 396 @staticmethod 397 def key_sha1_hashes(): 398 return ( 399 ('keys/test-public-der.key', b'\xbeB\x85=\xcc\xff\xe3\xf9(\x02\x8f~XV\xb4\xfd\x03\\\xeaK'), 400 ('keys/test-public-dsa-der.key', b'\x81\xa37\x86\xf9\x99(\xf2tp`\x87\xf2\xd3~\x8d\x19a\xa8\xbe'), 401 ('keys/test-public-ec-named-der.key', b'#\x8d\xee\xeeGH*\xe45T\xb8\xfdVh\x16_\xe2\xaa\xcd\x81'), 402 ('keys/test-public-ec-der.key', b'T\xaaTpl4\x1am\xeb]\x97\xd7\x1e\xfc\xd5$<\x8a\x0e\xd7'), 403 ) 404 405 @data('key_sha1_hashes') 406 def sha1(self, relative_path, sha1): 407 with open(os.path.join(fixtures_dir, relative_path), 'rb') as f: 408 public_key = keys.PublicKeyInfo.load(f.read()) 409 410 self.assertEqual(sha1, public_key.sha1) 411 412 @staticmethod 413 def key_sha256_hashes(): 414 return ( 415 ( 416 'keys/test-public-der.key', 417 b'\xd9\x80\xdf\x94J\x8e\x1e\xf5z\xd2o\x8eS\xa8\x03qX\x9a[\x17g\x12\x89\xc5\xcc\xca\x04\x94\xf2R|F' 418 ), 419 ( 420 'keys/test-public-dsa-der.key', 421 b'<\x10X\xbf=\xe4\xec3\xb9\xb2 \x11\xce9\xca\xd4\x95\xcf\xf9\xbc\x91q]O\x8f4\xbf\xdb\xdc\xe2\xd6\x82' 422 ), 423 ( 424 'keys/test-public-ec-named-der.key', 425 b'\x87e \xb4\x13\x8cu\xdd\x11\x92\xa4\xd9;\x8e\xe5"p\xb2\xb7\xa7\xcb8\x88\x16;f\xb9\xf8I\x86J\x1c' 426 ), 427 ( 428 'keys/test-public-ec-der.key', 429 b'\xf3\xa3k\xe0\xbf\xa9\xd9sl\xaa\x99\xe7\x9c-\xec\xb9\x0e\xe2d\xe9\xc3$\xb9\x893\x99A\xc19ec_' 430 ), 431 ) 432 433 @data('key_sha256_hashes') 434 def sha256(self, relative_path, sha256): 435 with open(os.path.join(fixtures_dir, relative_path), 'rb') as f: 436 public_key = keys.PublicKeyInfo.load(f.read()) 437 438 self.assertEqual(sha256, public_key.sha256) 439 440 @staticmethod 441 def key_pairs(): 442 return ( 443 ( 444 'dsa', 445 'keys/test-pkcs8-dsa-der.key', 446 'keys/test-public-dsa-der.key', 447 'dsa', 448 3072 449 ), 450 ( 451 'ec_named', 452 'keys/test-pkcs8-ec-named-der.key', 453 'keys/test-public-ec-named-der.key', 454 'ec', 455 256 456 ), 457 ( 458 'ec', 459 'keys/test-pkcs8-ec-der.key', 460 'keys/test-public-ec-der.key', 461 'ec', 462 256 463 ), 464 ( 465 'rsa', 466 'keys/test-pkcs8-der.key', 467 'keys/test-public-der.key', 468 'rsa', 469 2048 470 ), 471 ) 472 473 @data('key_pairs', True) 474 def algorithm_name(self, private_key_file, public_key_file, algorithm, _): 475 with open(os.path.join(fixtures_dir, private_key_file), 'rb') as f: 476 private_key = keys.PrivateKeyInfo.load(f.read()) 477 with open(os.path.join(fixtures_dir, public_key_file), 'rb') as f: 478 public_key = keys.PublicKeyInfo.load(f.read()) 479 480 self.assertEqual(algorithm, private_key.algorithm) 481 self.assertEqual(algorithm, public_key.algorithm) 482 483 @data('key_pairs', True) 484 def bit_size(self, private_key_file, public_key_file, _, bit_size): 485 with open(os.path.join(fixtures_dir, private_key_file), 'rb') as f: 486 private_key = keys.PrivateKeyInfo.load(f.read()) 487 with open(os.path.join(fixtures_dir, public_key_file), 'rb') as f: 488 public_key = keys.PublicKeyInfo.load(f.read()) 489 490 self.assertIsInstance(private_key.bit_size, int_types) 491 self.assertEqual(bit_size, private_key.bit_size) 492 self.assertIsInstance(public_key.bit_size, int_types) 493 self.assertEqual(bit_size, public_key.bit_size) 494 495 @staticmethod 496 def key_variations(): 497 return ( 498 ( 499 'dsa', 500 'keys/test-pkcs8-dsa-der.key', 501 'keys/test-dsa-der.key', 502 ), 503 ( 504 'ec_named', 505 'keys/test-pkcs8-ec-named-der.key', 506 'keys/test-ec-named-der.key', 507 ), 508 ( 509 'ec', 510 'keys/test-pkcs8-ec-der.key', 511 'keys/test-ec-der.key', 512 ), 513 ( 514 'rsa', 515 'keys/test-pkcs8-der.key', 516 'keys/test-der.key', 517 ), 518 ) 519 520 def test_curve_invalid(self): 521 with open(os.path.join(fixtures_dir, 'keys/test-pkcs8-der.key'), 'rb') as f: 522 private_key = keys.PrivateKeyInfo.load(f.read()) 523 524 with self.assertRaises(ValueError): 525 private_key.curve 526 527 with open(os.path.join(fixtures_dir, 'keys/test-public-rsa-der.key'), 'rb') as f: 528 public_key = keys.PublicKeyInfo.load(f.read()) 529 530 with self.assertRaises(ValueError): 531 public_key.curve 532 533 def test_curve_info_name(self): 534 with open(os.path.join(fixtures_dir, 'keys/test-pkcs8-ec-named-der.key'), 'rb') as f: 535 private_key = keys.PrivateKeyInfo.load(f.read()) 536 537 curve = ('named', 'secp256r1') 538 539 self.assertEqual(curve, private_key.curve) 540 541 with open(os.path.join(fixtures_dir, 'keys/test-public-ec-named-der.key'), 'rb') as f: 542 public_key = keys.PublicKeyInfo.load(f.read()) 543 544 self.assertEqual(curve, public_key.curve) 545 546 def test_curve_info_specified(self): 547 with open(os.path.join(fixtures_dir, 'keys/test-pkcs8-ec-der.key'), 'rb') as f: 548 private_key = keys.PrivateKeyInfo.load(f.read()) 549 550 curve = ( 551 'specified', 552 util.OrderedDict([ 553 ('version', 'ecdpVer1'), 554 ( 555 'field_id', 556 util.OrderedDict([ 557 ('field_type', 'prime_field'), 558 ('parameters', 115792089210356248762697446949407573530086143415290314195533631308867097853951) 559 ]) 560 ), 561 ( 562 'curve', 563 util.OrderedDict([ 564 ( 565 'a', 566 b'\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00' 567 b'\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC' 568 ), 569 ( 570 'b', 571 b'\x5A\xC6\x35\xD8\xAA\x3A\x93\xE7\xB3\xEB\xBD\x55\x76\x98\x86\xBC' 572 b'\x65\x1D\x06\xB0\xCC\x53\xB0\xF6\x3B\xCE\x3C\x3E\x27\xD2\x60\x4B' 573 ), 574 ( 575 'seed', 576 b'\xC4\x9D\x36\x08\x86\xE7\x04\x93\x6A\x66\x78\xE1\x13\x9D\x26\xB7\x81\x9F\x7E\x90' 577 ), 578 ]) 579 ), 580 ( 581 'base', 582 b'\x04\x6B\x17\xD1\xF2\xE1\x2C\x42\x47\xF8\xBC\xE6\xE5\x63\xA4\x40\xF2\x77\x03\x7D' 583 b'\x81\x2D\xEB\x33\xA0\xF4\xA1\x39\x45\xD8\x98\xC2\x96\x4F\xE3\x42\xE2\xFE\x1A\x7F' 584 b'\x9B\x8E\xE7\xEB\x4A\x7C\x0F\x9E\x16\x2B\xCE\x33\x57\x6B\x31\x5E\xCE\xCB\xB6\x40' 585 b'\x68\x37\xBF\x51\xF5' 586 ), 587 ( 588 'order', 589 115792089210356248762697446949407573529996955224135760342422259061068512044369 590 ), 591 ('cofactor', 1), 592 ('hash', None), 593 ]) 594 ) 595 596 self.assertEqual(curve, private_key.curve) 597 598 with open(os.path.join(fixtures_dir, 'keys/test-public-ec-der.key'), 'rb') as f: 599 public_key = keys.PublicKeyInfo.load(f.read()) 600 601 self.assertEqual(curve, public_key.curve) 602 603 def test_named_curve_register(self): 604 keys.NamedCurve.register('customcurve', '1.2.3.4.5.6.7.8', 16) 605 606 k = keys.NamedCurve('customcurve') 607 self.assertEqual('customcurve', k.native) 608 self.assertEqual('1.2.3.4.5.6.7.8', k.dotted) 609 610 k = keys.ECPrivateKey({ 611 'version': 1, 612 'private_key': 1, 613 'parameters': keys.ECDomainParameters(('named', 'customcurve')), 614 }) 615 616 self.assertEqual('ecPrivkeyVer1', k['version'].native) 617 self.assertEqual(1, k['private_key'].native) 618 self.assertEqual('customcurve', k['parameters'].native) 619 self.assertEqual( 620 b'\x04\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01', 621 k['private_key'].dump() 622 ) 623 624 def test_ec_private_key_width(self): 625 k = keys.ECPrivateKey({ 626 'version': 1, 627 'private_key': 1, 628 'parameters': keys.ECDomainParameters(('named', 'secp256r1')), 629 }) 630 631 self.assertEqual('ecPrivkeyVer1', k['version'].native) 632 self.assertEqual(1, k['private_key'].native) 633 self.assertEqual('secp256r1', k['parameters'].native) 634 self.assertEqual( 635 b'\x04\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 636 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01', 637 k['private_key'].dump() 638 ) 639 640 def test_ec_private_key_width_dotted(self): 641 k = keys.ECPrivateKey({ 642 'version': 1, 643 'private_key': 1, 644 'parameters': keys.ECDomainParameters(('named', '1.3.132.0.10')), 645 }) 646 647 self.assertEqual('ecPrivkeyVer1', k['version'].native) 648 self.assertEqual(1, k['private_key'].native) 649 self.assertEqual('secp256k1', k['parameters'].native) 650 self.assertEqual( 651 b'\x04\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 652 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01', 653 k['private_key'].dump() 654 ) 655 656 def test_ec_private_key_info_width(self): 657 pki = keys.PrivateKeyInfo({ 658 'version': 0, 659 'private_key_algorithm': { 660 'algorithm': 'ec', 661 'parameters': ('named', 'secp256r1'), 662 }, 663 'private_key': { 664 'version': 1, 665 'private_key': 1 666 } 667 }) 668 669 k = pki['private_key'].parsed 670 self.assertEqual('ecPrivkeyVer1', k['version'].native) 671 self.assertEqual(1, k['private_key'].native) 672 self.assertEqual(None, k['parameters'].native) 673 self.assertEqual( 674 b'\x04\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 675 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01', 676 k['private_key'].dump() 677 ) 678