@@ -516,6 +516,23 @@ def normalize_privatekey_pem(pem):
516
516
517
517
encryptedPrivateKeyPEMPassphrase = b"foobar"
518
518
519
+ cleartextPrivateKeyPEM = """-----BEGIN PRIVATE KEY-----
520
+ MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMcRMugJ4kvkOEuT
521
+ AvMFr9+3A6+HAB6nKYcXXZz93ube8rJpBZQEfWn73H10dQiQR/a+rhxYEeLy8dPc
522
+ UkFcGR9miVkukJ59zex7iySJY76bdBD8gyx1LTKrkCstP2XHKEYqgbj+tm7VzJnY
523
+ sQLqoaa5NeyWJnUC3MJympkAS7p3AgMBAAECgYAoBAcNqd75jnjaiETRgVUnTWzK
524
+ PgMCJmwsob/JrSa/lhWHU6Exbe2f/mcGOQDFpesxaIcrX3DJBDkkc2d9h/vsfo5v
525
+ JLk/rbHoItWxwuY5n5raAPeQPToKpTDxDrL6Ejhgcxd19wNht7/XSrYZ+dq3iU6G
526
+ mOEvU2hrnfIW3kwVYQJBAP62G6R0gucNfaKGtHzfR3TN9G/DnCItchF+TxGTtpdh
527
+ Cz32MG+7pirT/0xunekmUIp15QHdRy496sVxWTCooLkCQQDIEwXTAwhLNRGFEs5S
528
+ jSkxNfTVeNiOzlG8jPBJJDAdlLt1gUqjZWnk9yU+itMSGi/6eeuH2n04FFk+SV/T
529
+ 7ryvAkB0y0ZDk5VOozX/p2rtc2iNm77A3N4kIdiTQuq4sZXhNgN0pwWwxke8jbcb
530
+ 8gEAnqwBwWt//locTxHu9TmjgT8pAkEAlbF16B0atXptM02QxT8MlN8z4gxaqu4/
531
+ RX2FwpOq1FcVsqMbvwj/o+ouGY8wwRiK0TMrQCf/DFhdNTcc1aqHzQJBAKWtq4LI
532
+ uVZjCAuyrqEnt7R1bOiLrar+/ezJPY2z+f2rb1TGr31ztPeFvO3edLw+QdhzwJGp
533
+ QKImYzqMe+zkIOQ=
534
+ -----END PRIVATE KEY-----
535
+ """
519
536
520
537
cleartextPublicKeyPEM = b"""-----BEGIN PUBLIC KEY-----
521
538
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxszlc+b71LvlLS0ypt/l
@@ -3167,6 +3184,44 @@ def cb(ignored):
3167
3184
with pytest .raises (ValueError ):
3168
3185
dump_privatekey (FILETYPE_PEM , key , GOOD_CIPHER , cb )
3169
3186
3187
+ def test_dump_privatekey_truncated (self ):
3188
+ """
3189
+ `crypto.dump_privatekey` should not truncate a passphrase that contains
3190
+ a null byte.
3191
+ """
3192
+ key = load_privatekey (FILETYPE_PEM , cleartextPrivateKeyPEM )
3193
+ passphrase = b"foo\x00 bar"
3194
+ truncated_passphrase = passphrase .split (b"\x00 " , 1 )[0 ]
3195
+
3196
+ # By dumping with the full passphrase load should raise an error if we
3197
+ # try to load using the truncated passphrase. If dump truncated the
3198
+ # passphrase, then we WILL load the privatekey and the test fails
3199
+ encrypted_key_pem = dump_privatekey (
3200
+ FILETYPE_PEM , key , "AES-256-CBC" , passphrase
3201
+ )
3202
+ with pytest .raises (Error ):
3203
+ load_privatekey (
3204
+ FILETYPE_PEM , encrypted_key_pem , truncated_passphrase
3205
+ )
3206
+
3207
+ def test_load_privatekey_truncated (self ):
3208
+ """
3209
+ `crypto.load_privatekey` should not truncate a passphrase that contains
3210
+ a null byte.
3211
+ """
3212
+ key = load_privatekey (FILETYPE_PEM , cleartextPrivateKeyPEM )
3213
+ passphrase = b"foo\x00 bar"
3214
+ truncated_passphrase = passphrase .split (b"\x00 " , 1 )[0 ]
3215
+
3216
+ # By dumping using the truncated passphrase load should raise an error
3217
+ # if we try to load using the full passphrase. If load truncated the
3218
+ # passphrase, then we WILL load the privatekey and the test fails
3219
+ encrypted_key_pem = dump_privatekey (
3220
+ FILETYPE_PEM , key , "AES-256-CBC" , truncated_passphrase
3221
+ )
3222
+ with pytest .raises (Error ):
3223
+ load_privatekey (FILETYPE_PEM , encrypted_key_pem , passphrase )
3224
+
3170
3225
def test_load_pkcs7_data_pem (self ):
3171
3226
"""
3172
3227
`load_pkcs7_data` accepts a PKCS#7 string and returns an instance of
0 commit comments