@@ -23,12 +23,18 @@ use std::path::Path;
2323
2424/// Parse a private key object from a PKCS#8 encoded document.
2525pub trait DecodePrivateKey : Sized {
26- /// Deserialize PKCS#8 private key from ASN.1 DER-encoded data
27- /// (binary format).
26+ /// Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
27+ ///
28+ /// # Errors
29+ /// Returns format-specific errors in the event the document failed to parse.
2830 fn from_pkcs8_der ( bytes : & [ u8 ] ) -> Result < Self > ;
2931
30- /// Deserialize encrypted PKCS#8 private key from ASN.1 DER-encoded data
31- /// (binary format) and attempt to decrypt it using the provided password.
32+ /// Deserialize encrypted PKCS#8 private key from ASN.1 DER-encoded data (binary format) and
33+ /// attempt to decrypt it using the provided password.
34+ ///
35+ /// # Errors
36+ /// - Returns errors if the DER failed to decode
37+ /// - Returns errors if the ciphertext failed to decrypt under the given password
3238 #[ cfg( feature = "encryption" ) ]
3339 fn from_pkcs8_encrypted_der ( bytes : & [ u8 ] , password : impl AsRef < [ u8 ] > ) -> Result < Self > {
3440 let doc = EncryptedPrivateKeyInfoRef :: try_from ( bytes) ?. decrypt ( password) ?;
@@ -42,6 +48,10 @@ pub trait DecodePrivateKey: Sized {
4248 /// ```text
4349 /// -----BEGIN PRIVATE KEY-----
4450 /// ```
51+ ///
52+ /// # Errors
53+ /// - Returns [`Error::Asn1`] in the event of a decoding error (PEM or DER).
54+ /// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_der`].
4555 #[ cfg( feature = "pem" ) ]
4656 fn from_pkcs8_pem ( s : & str ) -> Result < Self > {
4757 // Validate PEM label
@@ -52,29 +62,42 @@ pub trait DecodePrivateKey: Sized {
5262 Self :: from_pkcs8_der ( doc. as_bytes ( ) )
5363 }
5464
55- /// Deserialize encrypted PKCS#8-encoded private key from PEM and attempt
56- /// to decrypt it using the provided password.
65+ /// Deserialize encrypted PKCS#8-encoded private key from PEM and attempt to decrypt it using
66+ /// the provided password.
5767 ///
5868 /// Keys in this format begin with the following delimiter:
5969 ///
6070 /// ```text
6171 /// -----BEGIN ENCRYPTED PRIVATE KEY-----
6272 /// ```
73+ ///
74+ /// # Errors
75+ /// - Returns [`Error::Asn1`] in the event of a decoding error (PEM or DER).
76+ /// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_encrypted_der`].
6377 #[ cfg( all( feature = "encryption" , feature = "pem" ) ) ]
6478 fn from_pkcs8_encrypted_pem ( s : & str , password : impl AsRef < [ u8 ] > ) -> Result < Self > {
6579 let ( label, doc) = SecretDocument :: from_pem ( s) ?;
6680 EncryptedPrivateKeyInfoRef :: validate_pem_label ( label) ?;
6781 Self :: from_pkcs8_encrypted_der ( doc. as_bytes ( ) , password)
6882 }
6983
70- /// Load PKCS#8 private key from an ASN.1 DER-encoded file on the local
71- /// filesystem (binary format).
84+ /// Load PKCS#8 private key from an ASN.1 DER-encoded file (binary format) on the local
85+ /// filesystem.
86+ ///
87+ /// # Errors
88+ /// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_der`].
89+ /// - Returns errors in event the file cannot be read from the filesystem.
7290 #[ cfg( feature = "std" ) ]
7391 fn read_pkcs8_der_file ( path : impl AsRef < Path > ) -> Result < Self > {
7492 Self :: from_pkcs8_der ( SecretDocument :: read_der_file ( path) ?. as_bytes ( ) )
7593 }
7694
7795 /// Load PKCS#8 private key from a PEM-encoded file on the local filesystem.
96+ ///
97+ /// # Errors
98+ /// - Returns the same errors as [`SecretDocument::read_pem_file`].
99+ /// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_der`].
100+ /// - Returns errors in event the file cannot be read from the filesystem.
78101 #[ cfg( all( feature = "pem" , feature = "std" ) ) ]
79102 fn read_pkcs8_pem_file ( path : impl AsRef < Path > ) -> Result < Self > {
80103 let ( label, doc) = SecretDocument :: read_pem_file ( path) ?;
@@ -96,10 +119,17 @@ where
96119#[ cfg( feature = "alloc" ) ]
97120pub trait EncodePrivateKey {
98121 /// Serialize a [`SecretDocument`] containing a PKCS#8-encoded private key.
122+ ///
123+ /// # Errors
124+ /// Returns format-specific errors in the event the document failed to serialize.
99125 fn to_pkcs8_der ( & self ) -> Result < SecretDocument > ;
100126
101- /// Create an [`SecretDocument`] containing the ciphertext of
102- /// a PKCS#8 encoded private key encrypted under the given `password`.
127+ /// Create an [`SecretDocument`] containing the ciphertext of a PKCS#8 encoded private key
128+ /// encrypted under the given `password`.
129+ ///
130+ /// # Errors
131+ /// - Returns format-specific errors in the event the document failed to serialize.
132+ /// - Returns algorithm-specific errors in the event the document couldn't be encrypted.
103133 #[ cfg( feature = "encryption" ) ]
104134 fn to_pkcs8_encrypted_der < R : CryptoRng > (
105135 & self ,
@@ -110,6 +140,10 @@ pub trait EncodePrivateKey {
110140 }
111141
112142 /// Serialize this private key as PEM-encoded PKCS#8 with the given [`LineEnding`].
143+ ///
144+ /// # Errors
145+ /// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_der`].
146+ /// - Returns the same errors as [`SecretDocument::to_pem`].
113147 #[ cfg( feature = "pem" ) ]
114148 fn to_pkcs8_pem ( & self , line_ending : LineEnding ) -> Result < Zeroizing < String > > {
115149 let doc = self . to_pkcs8_der ( ) ?;
@@ -118,6 +152,10 @@ pub trait EncodePrivateKey {
118152
119153 /// Serialize this private key as an encrypted PEM-encoded PKCS#8 private
120154 /// key using the `provided` to derive an encryption key.
155+ ///
156+ /// # Errors
157+ /// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_encrypted_der`].
158+ /// - Returns the same errors as [`SecretDocument::to_pem`].
121159 #[ cfg( all( feature = "encryption" , feature = "pem" ) ) ]
122160 fn to_pkcs8_encrypted_pem < R : CryptoRng > (
123161 & self ,
@@ -129,13 +167,21 @@ pub trait EncodePrivateKey {
129167 Ok ( doc. to_pem ( EncryptedPrivateKeyInfoRef :: PEM_LABEL , line_ending) ?)
130168 }
131169
132- /// Write ASN.1 DER-encoded PKCS#8 private key to the given path
170+ /// Write ASN.1 DER-encoded PKCS#8 private key to the given path.
171+ ///
172+ /// # Errors
173+ /// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_der`].
174+ /// - Returns errors in the event the file could not be written to the filesystem.
133175 #[ cfg( feature = "std" ) ]
134176 fn write_pkcs8_der_file ( & self , path : impl AsRef < Path > ) -> Result < ( ) > {
135177 Ok ( self . to_pkcs8_der ( ) ?. write_der_file ( path) ?)
136178 }
137179
138- /// Write ASN.1 PEM-encoded PKCS#8 private key to the given path
180+ /// Write ASN.1 PEM-encoded PKCS#8 private key to the given path.
181+ ///
182+ /// # Errors
183+ /// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_der`].
184+ /// - Returns errors in the event the file could not be written to the filesystem.
139185 #[ cfg( all( feature = "pem" , feature = "std" ) ) ]
140186 fn write_pkcs8_pem_file ( & self , path : impl AsRef < Path > , line_ending : LineEnding ) -> Result < ( ) > {
141187 let doc = self . to_pkcs8_der ( ) ?;
0 commit comments