From 4ff5f4486f03e720840760b96f4c8097a40ad80e Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 18 Jan 2019 12:21:39 +0100 Subject: [PATCH] Add `Asn1Integer::from_bn` --- openssl/src/asn1.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/openssl/src/asn1.rs b/openssl/src/asn1.rs index 633407ea..c7e90832 100644 --- a/openssl/src/asn1.rs +++ b/openssl/src/asn1.rs @@ -33,7 +33,7 @@ use std::slice; use std::str; use bio::MemBio; -use bn::BigNum; +use bn::{BigNum, BigNumRef}; use error::ErrorStack; use nid::Nid; use string::OpensslString; @@ -191,6 +191,20 @@ foreign_type_and_impl_send_sync! { pub struct Asn1IntegerRef; } +impl Asn1Integer { + /// Converts a bignum to an `Asn1Integer`. + /// + /// Corresponds to [`BN_to_ASN1_INTEGER`]. + /// + /// [`BN_to_ASN1_INTEGER`]: https://www.openssl.org/docs/man1.1.0/crypto/BN_to_ASN1_INTEGER.html + pub fn from_bn(bn: &BigNumRef) -> Result { + unsafe { + cvt_p(::ffi::BN_to_ASN1_INTEGER(bn.as_ptr(), ptr::null_mut())) + .map(|p| Asn1Integer::from_ptr(p)) + } + } +} + impl Asn1IntegerRef { #[allow(missing_docs)] #[deprecated(since = "0.10.6", note = "use to_bn instead")] @@ -306,3 +320,24 @@ cfg_if! { } } } + +#[cfg(test)] +mod tests { + use super::*; + + use bn::BigNum; + + /// Tests conversion between BigNum and Asn1Integer. + #[test] + fn bn_cvt() { + fn roundtrip(bn: BigNum) { + let large = Asn1Integer::from_bn(&bn).unwrap(); + assert_eq!(large.to_bn().unwrap(), bn); + } + + roundtrip(BigNum::from_dec_str("1000000000000000000000000000000000").unwrap()); + roundtrip(-BigNum::from_dec_str("1000000000000000000000000000000000").unwrap()); + roundtrip(BigNum::from_u32(1234).unwrap()); + roundtrip(-BigNum::from_u32(1234).unwrap()); + } +}