@@ -209,6 +209,155 @@ def test_load_from_disk(self):
209
209
self .assertEqual (14617195220284816877 , privkey .q )
210
210
211
211
212
+ MP_B64PRIV_DER = b"MEoCAQACCDsGeWW1Om5xAgMBAAECBQDHn4npAgMA+nsCAwDHDQICcw0CAwCWDQICTjYCAwCZlQICaukCAioVAgMAgVMCAksjAgIq3g=="
213
+ MP_PRIVATE_DER = base64 .standard_b64decode (MP_B64PRIV_DER )
214
+
215
+ MP_B64PUB_DER = b"MA8CCDsGeWW1Om5xAgMBAAE="
216
+ MP_PUBLIC_DER = base64 .standard_b64decode (MP_B64PUB_DER )
217
+
218
+ MP_PRIVATE_PEM = (
219
+ b"""\
220
+ -----BEGIN CONFUSING STUFF-----
221
+ Cruft before the key
222
+
223
+ -----BEGIN RSA PRIVATE KEY-----
224
+ Comment: something blah
225
+
226
+ """
227
+ + MP_B64PRIV_DER
228
+ + b"""
229
+ -----END RSA PRIVATE KEY-----
230
+
231
+ Stuff after the key
232
+ -----END CONFUSING STUFF-----
233
+ """
234
+ )
235
+
236
+ MP_CLEAN_PRIVATE_PEM = (
237
+ b"""\
238
+ -----BEGIN RSA PRIVATE KEY-----
239
+ """
240
+ + MP_B64PRIV_DER
241
+ + b"""
242
+ -----END RSA PRIVATE KEY-----
243
+ """
244
+ )
245
+
246
+ MP_PUBLIC_PEM = (
247
+ b"""\
248
+ -----BEGIN CONFUSING STUFF-----
249
+ Cruft before the key
250
+
251
+ -----BEGIN RSA PUBLIC KEY-----
252
+ Comment: something blah
253
+
254
+ """
255
+ + MP_B64PUB_DER
256
+ + b"""
257
+ -----END RSA PUBLIC KEY-----
258
+
259
+ Stuff after the key
260
+ -----END CONFUSING STUFF-----
261
+ """
262
+ )
263
+
264
+ MP_CLEAN_PUBLIC_PEM = (
265
+ b"""\
266
+ -----BEGIN RSA PUBLIC KEY-----
267
+ """
268
+ + MP_B64PUB_DER
269
+ + b"""
270
+ -----END RSA PUBLIC KEY-----
271
+ """
272
+ )
273
+
274
+
275
+ class MultiprimeDerTest (unittest .TestCase ):
276
+ """Test saving and loading multiprime DER keys."""
277
+
278
+ def test_load_multiprime_private_key (self ):
279
+ """Test loading private DER keys."""
280
+
281
+ key = rsa .key .PrivateKey .load_pkcs1 (MP_PRIVATE_DER , "DER" )
282
+ expected = rsa .key .PrivateKey (4253220375837175409 , 65537 , 3349121513 , 64123 , 50957 , [39317 , 33107 ])
283
+
284
+ self .assertEqual (expected , key )
285
+ self .assertEqual (key .exp1 , 29453 )
286
+ self .assertEqual (key .exp2 , 38413 )
287
+ self .assertEqual (key .coef , 20022 )
288
+ self .assertEqual (key .ds , [27369 , 19235 ])
289
+ self .assertEqual (key .ts , [10773 , 10974 ])
290
+
291
+ def test_save_multiprime_private_key (self ):
292
+ """Test saving private DER keys."""
293
+
294
+ key = rsa .key .PrivateKey (4253220375837175409 , 65537 , 3349121513 , 64123 , 50957 , [39317 , 33107 ])
295
+ der = key .save_pkcs1 ("DER" )
296
+
297
+ self .assertIsInstance (der , bytes )
298
+ self .assertEqual (MP_PRIVATE_DER , der )
299
+
300
+ def test_load_multiprime_public_key (self ):
301
+ """Test loading public DER keys."""
302
+
303
+ key = rsa .key .PublicKey .load_pkcs1 (MP_PUBLIC_DER , "DER" )
304
+ expected = rsa .key .PublicKey (4253220375837175409 , 65537 )
305
+
306
+ self .assertEqual (expected , key )
307
+
308
+ def test_save_multiprime_public_key (self ):
309
+ """Test saving public DER keys."""
310
+
311
+ key = rsa .key .PublicKey (4253220375837175409 , 65537 )
312
+ der = key .save_pkcs1 ("DER" )
313
+
314
+ self .assertIsInstance (der , bytes )
315
+ self .assertEqual (MP_PUBLIC_DER , der )
316
+
317
+
318
+ class MultiprimePemTest (unittest .TestCase ):
319
+ """Test saving and loading multiprime PEM keys."""
320
+
321
+ def test_load_multiprime_private_key (self ):
322
+ """Test loading private PEM files."""
323
+
324
+ key = rsa .key .PrivateKey .load_pkcs1 (MP_PRIVATE_PEM , "PEM" )
325
+ expected = rsa .key .PrivateKey (4253220375837175409 , 65537 , 3349121513 , 64123 , 50957 , [39317 , 33107 ])
326
+
327
+ self .assertEqual (expected , key )
328
+ self .assertEqual (key .exp1 , 29453 )
329
+ self .assertEqual (key .exp2 , 38413 )
330
+ self .assertEqual (key .coef , 20022 )
331
+ self .assertEqual (key .ds , [27369 , 19235 ])
332
+ self .assertEqual (key .ts , [10773 , 10974 ])
333
+
334
+ def test_save_multiprime_private_key (self ):
335
+ """Test saving private PEM files."""
336
+
337
+ key = rsa .key .PrivateKey (4253220375837175409 , 65537 , 3349121513 , 64123 , 50957 , [39317 , 33107 ])
338
+ pem = key .save_pkcs1 ("PEM" )
339
+
340
+ self .assertIsInstance (pem , bytes )
341
+ self .assertEqual (MP_CLEAN_PRIVATE_PEM .replace (b"\n " , b"" ), pem .replace (b"\n " , b"" ))
342
+
343
+ def test_load_multiprime_public_key (self ):
344
+ """Test loading public PEM files."""
345
+
346
+ key = rsa .key .PublicKey .load_pkcs1 (MP_PUBLIC_PEM , "PEM" )
347
+ expected = rsa .key .PublicKey (4253220375837175409 , 65537 )
348
+
349
+ self .assertEqual (expected , key )
350
+
351
+ def test_save_multiprime_public_key (self ):
352
+ """Test saving public PEM files."""
353
+
354
+ key = rsa .key .PublicKey (4253220375837175409 , 65537 )
355
+ pem = key .save_pkcs1 ("PEM" )
356
+
357
+ self .assertIsInstance (pem , bytes )
358
+ self .assertEqual (MP_CLEAN_PUBLIC_PEM , pem )
359
+
360
+
212
361
class PickleTest (unittest .TestCase ):
213
362
"""Test saving and loading keys by pickling."""
214
363
@@ -222,6 +371,16 @@ def test_private_key(self):
222
371
for attr in rsa .key .AbstractKey .__slots__ :
223
372
self .assertTrue (hasattr (unpickled , attr ))
224
373
374
+ def test_multiprime_private_key (self ):
375
+ pk = rsa .key .PrivateKey (4253220375837175409 , 65537 , 3349121513 , 64123 , 50957 , [39317 , 33107 ])
376
+
377
+ pickled = pickle .dumps (pk )
378
+ unpickled = pickle .loads (pickled )
379
+ self .assertEqual (pk , unpickled )
380
+
381
+ for attr in rsa .key .AbstractKey .__slots__ :
382
+ self .assertTrue (hasattr (unpickled , attr ))
383
+
225
384
def test_public_key (self ):
226
385
pk = rsa .key .PublicKey (3727264081 , 65537 )
227
386
0 commit comments