aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/ec_key.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2016-11-13 15:02:38 +0000
committerSteven Fackler <[email protected]>2016-11-13 15:02:38 +0000
commit08e0c4ca9061c3dc0c951db0c08909689b04a310 (patch)
tree117d95bb3cc28cf88cdedb97c985bbe9db436415 /openssl/src/ec_key.rs
parentNo need to use a raw string anymore (diff)
downloadrust-openssl-08e0c4ca9061c3dc0c951db0c08909689b04a310.tar.xz
rust-openssl-08e0c4ca9061c3dc0c951db0c08909689b04a310.zip
Some serialization support for EcKey
Diffstat (limited to 'openssl/src/ec_key.rs')
-rw-r--r--openssl/src/ec_key.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs
index ad85dc5e..99d62ad3 100644
--- a/openssl/src/ec_key.rs
+++ b/openssl/src/ec_key.rs
@@ -1,11 +1,27 @@
use ffi;
+use std::cmp;
+use libc::c_long;
+use std::ptr;
-use {cvt_p, init};
+use {cvt, cvt_p, init};
use error::ErrorStack;
use nid::Nid;
+use types::OpenSslTypeRef;
type_!(EcKey, EcKeyRef, ffi::EC_KEY, ffi::EC_KEY_free);
+impl EcKeyRef {
+ /// Serializes the private key components to DER.
+ pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
+ unsafe {
+ let len = try!(cvt(ffi::i2d_ECPrivateKey(self.as_ptr(), ptr::null_mut())));
+ let mut buf = vec![0; len as usize];
+ try!(cvt(ffi::i2d_ECPrivateKey(self.as_ptr(), &mut buf.as_mut_ptr())));
+ Ok(buf)
+ }
+ }
+}
+
impl EcKey {
pub fn new_by_curve_name(nid: Nid) -> Result<EcKey, ErrorStack> {
unsafe {
@@ -13,6 +29,16 @@ impl EcKey {
cvt_p(ffi::EC_KEY_new_by_curve_name(nid.as_raw())).map(EcKey)
}
}
+ /// Deserializes a DER-encoded private key.
+ pub fn private_key_from_der(der: &[u8]) -> Result<EcKey, ErrorStack> {
+ unsafe {
+ init();
+ let len = cmp::min(der.len(), c_long::max_value() as usize) as c_long;
+ cvt_p(ffi::d2i_ECPrivateKey(ptr::null_mut(), &mut der.as_ptr(), len)).map(EcKey)
+ }
+ }
+
+ private_key_from_pem!(EcKey, ffi::PEM_read_bio_ECPrivateKey);
}
#[cfg(test)]