Struct openssl::dsa::DsaSig

source ·
pub struct DsaSig(/* private fields */);
Expand description

Object representing DSA signature.

DSA signatures consist of two components: r and s.

Examples

use std::convert::TryInto;

use openssl::bn::BigNum;
use openssl::dsa::{Dsa, DsaSig};
use openssl::hash::MessageDigest;
use openssl::pkey::PKey;
use openssl::sign::{Signer, Verifier};

const TEST_DATA: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let dsa_ref = Dsa::generate(1024).unwrap();

let pub_key: PKey<_> = dsa_ref.clone().try_into().unwrap();
let priv_key: PKey<_> = dsa_ref.try_into().unwrap();

let mut signer = if let Ok(signer) = Signer::new(MessageDigest::sha256(), &priv_key) {
    signer
} else {
    // DSA signing is not supported (eg. BoringSSL)
    return;
};

signer.update(TEST_DATA).unwrap();

let signature = signer.sign_to_vec().unwrap();
// Parse DER-encoded DSA signature
let signature = DsaSig::from_der(&signature).unwrap();

// Extract components `r` and `s`
let r = BigNum::from_slice(&signature.r().to_vec()).unwrap();
let s = BigNum::from_slice(&signature.s().to_vec()).unwrap();

// Construct new DSA signature from components
let signature = DsaSig::from_private_components(r, s).unwrap();

// Serialize DSA signature to DER
let signature = signature.to_der().unwrap();

let mut verifier = Verifier::new(MessageDigest::sha256(), &pub_key).unwrap();
verifier.update(TEST_DATA).unwrap();
assert!(verifier.verify(&signature[..]).unwrap());

Implementations§

source§

impl DsaSig

source

pub fn from_private_components(r: BigNum, s: BigNum) -> Result<Self, ErrorStack>

Returns a new DsaSig by setting the r and s values associated with an DSA signature.

This corresponds to DSA_SIG_set0.

source

pub fn from_der(der: &[u8]) -> Result<DsaSig, ErrorStack>

Decodes a DER-encoded DSA signature.

This corresponds to d2i_DSA_SIG.

Methods from Deref<Target = DsaSigRef>§

source

pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack>

Serializes the DSA signature into a DER-encoded DSASignature structure.

This corresponds to i2d_DSA_SIG.

source

pub fn r(&self) -> &BigNumRef

Returns internal component r of an DsaSig.

This corresponds to DSA_SIG_get0.

source

pub fn s(&self) -> &BigNumRef

Returns internal component s of an DsaSig.

This corresponds to DSA_SIG_get0.

Trait Implementations§

source§

impl AsRef<DsaSigRef> for DsaSig

source§

fn as_ref(&self) -> &DsaSigRef

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<DsaSigRef> for DsaSig

source§

fn borrow(&self) -> &DsaSigRef

Immutably borrows from an owned value. Read more
source§

impl Debug for DsaSig

source§

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

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

impl Deref for DsaSig

§

type Target = DsaSigRef

The resulting type after dereferencing.
source§

fn deref(&self) -> &DsaSigRef

Dereferences the value.
source§

impl DerefMut for DsaSig

source§

fn deref_mut(&mut self) -> &mut DsaSigRef

Mutably dereferences the value.
source§

impl Drop for DsaSig

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl ForeignType for DsaSig

§

type CType = DSA_SIG

The raw C type.
§

type Ref = DsaSigRef

The type representing a reference to this type.
source§

unsafe fn from_ptr(ptr: *mut DSA_SIG) -> DsaSig

Constructs an instance of this type from its raw type.
source§

fn as_ptr(&self) -> *mut DSA_SIG

Returns a raw pointer to the wrapped value.
source§

impl Send for DsaSig

source§

impl Sync for DsaSig

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.