nethsm::key

Struct PrivateKeyImport

source
pub struct PrivateKeyImport {
    key_data: PrivateKeyData,
}
Expand description

The key data required when importing a secret key

Fields§

§key_data: PrivateKeyData

Implementations§

source§

impl PrivateKeyImport

source

pub fn new(key_type: KeyType, key_data: &[u8]) -> Result<Self, Error>

Creates a new PrivateKeyImport

Accepts a KeyType (all except KeyType::Generic) and a bytes array representing a matching PKCS#8 private key in ASN.1 DER-encoded format.

§Errors

Returns an crate::Error::Key if

  • key_data can not be deserialized to a respective private key format.
  • an RSA private key does not have prime P or prime Q.
  • an RSA private key is shorter than MIN_RSA_BIT_LENGTH.
  • key_type is the unsupported KeyType::Generic.
§Examples
use ed25519_dalek::{pkcs8::EncodePrivateKey, SigningKey};
use nethsm::{KeyType, PrivateKeyImport};
use rand::rngs::OsRng;

let key_data = {
    let mut csprng = OsRng;
    let signing_key: SigningKey = SigningKey::generate(&mut csprng);
    signing_key.to_pkcs8_der()?.as_bytes().to_vec()
};

assert!(PrivateKeyImport::new(KeyType::Curve25519, &key_data).is_ok());
source

pub fn from_pkcs8_pem(key_type: KeyType, key_data: &str) -> Result<Self, Error>

Creates a new PrivateKeyImport

Accepts a KeyType (all except KeyType::Generic) and a string slice representing a matching PKCS#8 private key in PEM-encoded format.

§Errors

Returns an crate::Error::Key if

  • key_data can not be deserialized to a respective private key format.
  • an RSA private key does not have prime P or prime Q.
  • an RSA private key is shorter than MIN_RSA_BIT_LENGTH.
  • key_type is the unsupported KeyType::Generic.
§Examples
use std::ops::Deref;

use ed25519_dalek::{pkcs8::spki::der::pem::LineEnding, pkcs8::EncodePrivateKey, SigningKey};
use nethsm::{KeyType, PrivateKeyImport};
use rand::rngs::OsRng;

let key_data = {
    let mut csprng = OsRng;
    let signing_key: SigningKey = SigningKey::generate(&mut csprng);
    signing_key.to_pkcs8_pem(LineEnding::default())?
};

assert!(PrivateKeyImport::from_pkcs8_pem(KeyType::Curve25519, key_data.deref()).is_ok());
source

pub fn from_rsa( prime_p: Vec<u8>, prime_q: Vec<u8>, public_exponent: Vec<u8>, ) -> Self

Create PrivateKeyImport object from raw, private RSA key parts.

The function takes two primes (p and q) and the public exponent, which usually is 65537 ([0x01, 0x00, 0x01]).

§Examples
use nethsm::PrivateKeyImport;

let prime_p = vec![7];
let prime_q = vec![11];
let public_exponent = vec![1, 0, 1];

let _import = PrivateKeyImport::from_rsa(prime_p, prime_q, public_exponent);
source

pub fn from_raw_bytes( ec: KeyType, bytes: impl AsRef<[u8]>, ) -> Result<Self, Error>

Create PrivateKeyImport object from raw, private Elliptic Curve bytes.

The function takes two parameters:

  • the type of elliptic curve,
  • raw bytes in a curve-specific encoding

Elliptic curve keys require the bytes to be zero-padded to be of correct size. This function automatically applies padding accordingly.

§Examples
use nethsm::{KeyType, PrivateKeyImport};

let bytes = vec![0x00; 32];

let _import = PrivateKeyImport::from_raw_bytes(KeyType::Curve25519, bytes)?;
source

pub fn key_type(&self) -> KeyType

Get the matching KeyType for the data contained in the PrivateKeyImport

Trait Implementations§

source§

impl From<PrivateKeyImport> for KeyPrivateData

source§

fn from(value: PrivateKeyImport) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T