Skip to content

Commit 5d7f262

Browse files
committed
Add function to export certificate's public key
1 parent 78c2b86 commit 5d7f262

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/signed_data.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ pub fn verify_signature(signature_alg: &SignatureAlgorithm,
145145
.map_err(|_| Error::InvalidSignatureForPublicKey)
146146
}
147147

148+
pub fn public_key_bytes(spki_value: untrusted::Input) ->
149+
Result<untrusted::Input, Error> {
150+
let spki = parse_spki_value(spki_value)?;
151+
Ok(spki.key_value)
152+
}
153+
148154

149155
struct SubjectPublicKeyInfo<'a> {
150156
algorithm_id_value: untrusted::Input<'a>,

src/webpki.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ impl <'a> EndEntityCert<'a> {
246246
signed_data::verify_signature(signature_alg, self.inner.spki, msg,
247247
signature)
248248
}
249+
250+
/// Returns the certificate's public key.
251+
pub fn public_key_bytes(&self) -> Result<untrusted::Input, Error> {
252+
signed_data::public_key_bytes(self.inner.spki)
253+
}
249254
}
250255

251256

tests/integration.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ pub fn ed25519()
104104
let _ = cert.verify_is_valid_tls_server_cert(ALL_SIGALGS, &anchors,
105105
&inter_vec, time)
106106
.unwrap();
107+
assert_eq!(cert.public_key_bytes(),
108+
Ok(untrusted::Input::from(
109+
&[0xfe, 0x5a, 0x1e, 0x36, 0x6c, 0x17, 0x27, 0x5b, 0xf1,
110+
0x58, 0x1e, 0x3a, 0x0e, 0xe6, 0x56, 0x29, 0x8d, 0x9e,
111+
0x1b, 0x3f, 0xd3, 0x3f, 0x96, 0x46, 0xef, 0xbf, 0x04,
112+
0x6b, 0xc7, 0x3d, 0x47, 0x5c])));
107113
}
108114

109115
#[cfg(feature = "trust_anchor_util")]

0 commit comments

Comments
 (0)