pub struct RsaKeyPair { /* private fields */ }
Expand description

An RSA key pair, used for signing.

Implementations§

source§

impl RsaKeyPair

source

pub fn from_pkcs8(pkcs8: &[u8]) -> Result<Self, KeyRejected>

Parses an unencrypted PKCS#8-encoded RSA private key.

Only two-prime (not multi-prime) keys are supported. The public modulus (n) must be at least 2047 bits. The public modulus must be no larger than 4096 bits. It is recommended that the public modulus be exactly 2048 or 3072 bits. The public exponent must be at least 65537.

The following will generate a 2048-bit RSA private key of the correct form using OpenSSL’s command line tool:

   openssl genpkey -algorithm RSA \
       -pkeyopt rsa_keygen_bits:2048 \
       -pkeyopt rsa_keygen_pubexp:65537 | \
     openssl pkcs8 -topk8 -nocrypt -outform der > rsa-2048-private-key.pk8

The following will generate a 3072-bit RSA private key of the correct form:

   openssl genpkey -algorithm RSA \
       -pkeyopt rsa_keygen_bits:3072 \
       -pkeyopt rsa_keygen_pubexp:65537 | \
     openssl pkcs8 -topk8 -nocrypt -outform der > rsa-3072-private-key.pk8

Often, keys generated for use in OpenSSL-based software are stored in the Base64 “PEM” format without the PKCS#8 wrapper. Such keys can be converted to binary PKCS#8 form using the OpenSSL command line tool like this:

openssl pkcs8 -topk8 -nocrypt -outform der \
    -in rsa-2048-private-key.pem > rsa-2048-private-key.pk8

Base64 (“PEM”) PKCS#8-encoded keys can be converted to the binary PKCS#8 form like this:

openssl pkcs8 -nocrypt -outform der \
    -in rsa-2048-private-key.pem > rsa-2048-private-key.pk8
Errors

error::KeyRejected if bytes do not encode an RSA private key or if the key is otherwise not acceptable.

source

pub fn from_der(input: &[u8]) -> Result<Self, KeyRejected>

Parses a DER-encoded RSAPrivateKey structure (RFC 8017).

Errors

error:KeyRejected on error.

source§

impl RsaKeyPair

source

pub fn sign( &self, padding_alg: &'static dyn RsaEncoding, _rng: &dyn SecureRandom, msg: &[u8], signature: &mut [u8] ) -> Result<(), Unspecified>

Sign msg. msg is digested using the digest algorithm from padding_alg and the digest is then padded using the padding algorithm from padding_alg. The signature it written into signature; signature’s length must be exactly the length returned by public_modulus_len().

Many other crypto libraries have signing functions that takes a precomputed digest as input, instead of the message to digest. This function does not take a precomputed digest; instead, sign calculates the digest itself.

ring Compatibility

Our implementation ignored the SecureRandom parameter.

Errors

error::Unspecified on error. With “fips” feature enabled, errors if digest length is greater than u32::MAX.

source

pub fn public_modulus_len(&self) -> usize

Returns the length in bytes of the key pair’s public modulus.

A signature has the same length as the public modulus.

Trait Implementations§

source§

impl Debug for RsaKeyPair

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl KeyPair for RsaKeyPair

§

type PublicKey = RsaSubjectPublicKey

The type of the public key.
source§

fn public_key(&self) -> &Self::PublicKey

The public key for the key pair.
source§

impl Send for RsaKeyPair

source§

impl Sync for RsaKeyPair

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.