22// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33// for complete details.
44
5- use pyo3:: IntoPyObject ;
5+ use pyo3:: { IntoPyObject , PyTypeInfo } ;
6+ use std:: ffi:: CStr ;
67
78use crate :: buf:: CffiBuf ;
89use crate :: error:: { CryptographyError , CryptographyResult } ;
@@ -27,14 +28,22 @@ fn load_der_private_key<'p>(
2728 )
2829}
2930
31+ fn py_warn ( py : pyo3:: Python < ' _ > ) -> impl Fn ( & CStr ) + ' _ {
32+ move |msg| {
33+ let warning_cls = pyo3:: exceptions:: PyUserWarning :: type_object ( py) ;
34+ // If warn fails, we ignore it.
35+ _ = pyo3:: PyErr :: warn ( py, & warning_cls, msg, 1 ) ;
36+ }
37+ }
38+
3039pub ( crate ) fn load_der_private_key_bytes < ' p > (
3140 py : pyo3:: Python < ' p > ,
3241 data : & [ u8 ] ,
3342 password : Option < & [ u8 ] > ,
3443 unsafe_skip_rsa_key_validation : bool ,
3544) -> CryptographyResult < pyo3:: Bound < ' p , pyo3:: PyAny > > {
36- let pkey = cryptography_key_parsing:: pkcs8:: parse_private_key ( data)
37- . or_else ( |_| cryptography_key_parsing:: ec:: parse_pkcs1_private_key ( data, None ) )
45+ let pkey = cryptography_key_parsing:: pkcs8:: parse_private_key ( data, py_warn ( py ) )
46+ . or_else ( |_| cryptography_key_parsing:: ec:: parse_pkcs1_private_key ( data, None , py_warn ( py ) ) )
3847 . or_else ( |_| cryptography_key_parsing:: rsa:: parse_pkcs1_private_key ( data) )
3948 . or_else ( |_| cryptography_key_parsing:: dsa:: parse_pkcs1_private_key ( data) ) ;
4049
@@ -49,7 +58,8 @@ pub(crate) fn load_der_private_key_bytes<'p>(
4958 return private_key_from_pkey ( py, & pkey, unsafe_skip_rsa_key_validation) ;
5059 }
5160
52- let pkey = cryptography_key_parsing:: pkcs8:: parse_encrypted_private_key ( data, password) ?;
61+ let pkey =
62+ cryptography_key_parsing:: pkcs8:: parse_encrypted_private_key ( data, password, py_warn ( py) ) ?;
5363
5464 private_key_from_pkey ( py, & pkey, unsafe_skip_rsa_key_validation)
5565}
@@ -74,11 +84,11 @@ fn load_pem_private_key<'p>(
7484 let ( data, mut password_used) = cryptography_key_parsing:: pem:: decrypt_pem ( & p, password) ?;
7585
7686 let pkey = match p. tag ( ) {
77- "PRIVATE KEY" => cryptography_key_parsing:: pkcs8:: parse_private_key ( & data) ?,
87+ "PRIVATE KEY" => cryptography_key_parsing:: pkcs8:: parse_private_key ( & data, py_warn ( py ) ) ?,
7888 "RSA PRIVATE KEY" => cryptography_key_parsing:: rsa:: parse_pkcs1_private_key ( & data) . map_err ( |e| {
7989 CryptographyError :: from ( e) . add_note ( py, "If your key is in PKCS#8 format, you must use BEGIN/END PRIVATE KEY PEM delimiters" )
8090 } ) ?,
81- "EC PRIVATE KEY" => cryptography_key_parsing:: ec:: parse_pkcs1_private_key ( & data, None ) . map_err ( |e| {
91+ "EC PRIVATE KEY" => cryptography_key_parsing:: ec:: parse_pkcs1_private_key ( & data, None , py_warn ( py ) ) . map_err ( |e| {
8292 CryptographyError :: from ( e) . add_note ( py, "If your key is in PKCS#8 format, you must use BEGIN/END PRIVATE KEY PEM delimiters" )
8393 } ) ?,
8494 "DSA PRIVATE KEY" => cryptography_key_parsing:: dsa:: parse_pkcs1_private_key ( & data) . map_err ( |e| {
@@ -87,7 +97,7 @@ fn load_pem_private_key<'p>(
8797 _ => {
8898 assert_eq ! ( p. tag( ) , "ENCRYPTED PRIVATE KEY" ) ;
8999 password_used = true ;
90- cryptography_key_parsing:: pkcs8:: parse_encrypted_private_key ( & data, password) ?
100+ cryptography_key_parsing:: pkcs8:: parse_encrypted_private_key ( & data, password, py_warn ( py ) ) ?
91101 }
92102 } ;
93103 if password. is_some ( ) && !password_used {
0 commit comments