From b3bd64504bddcfe66eb192b38d52df10b5f77644 Mon Sep 17 00:00:00 2001 From: mamoniot Date: Mon, 13 Mar 2023 16:12:57 -0400 Subject: [PATCH] added support for cloning on EcKey --- crypto/src/ec.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/crypto/src/ec.rs b/crypto/src/ec.rs index a1b0ef15b..c0f367442 100644 --- a/crypto/src/ec.rs +++ b/crypto/src/ec.rs @@ -1,3 +1,4 @@ +use core::fmt; use std::ptr; use crate::bn::{BigNumContext, BigNumContextRef, BigNumRef}; @@ -5,13 +6,11 @@ use crate::error::{cvt, cvt_n, cvt_p, ErrorStack}; use foreign_types::{foreign_type, ForeignType, ForeignTypeRef}; foreign_type! { - #[derive(Clone)] pub unsafe type EcGroup: Send + Sync { type CType = ffi::EC_GROUP; fn drop = ffi::EC_GROUP_free; } /// Public and optional private key on the given curve. - #[derive(Clone)] pub unsafe type EcKey { type CType = ffi::EC_KEY; fn drop = ffi::EC_KEY_free; @@ -125,3 +124,27 @@ impl EcPointRef { } } } + +impl ToOwned for EcKeyRef { + type Owned = EcKey; + + fn to_owned(&self) -> EcKey { + unsafe { + let r = ffi::EC_KEY_up_ref(self.as_ptr()); + assert!(r == 1); + EcKey::from_ptr(self.as_ptr()) + } + } +} + +impl Clone for EcKey { + fn clone(&self) -> EcKey { + (**self).to_owned() + } +} + +impl fmt::Debug for EcKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "EcKey") + } +}