Skip to content

Commit 261cdec

Browse files
committed
x509-cert: Create a CrlNumber from native types
2 parents 58fb944 + 70b96f9 commit 261cdec

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

der/src/asn1/integer/int.rs

+69
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,75 @@ mod allocating {
302302
IntRef { inner }
303303
}
304304
}
305+
306+
macro_rules! impl_from_traits {
307+
($($int:ty),+) => {
308+
$(
309+
impl TryFrom<$int> for Int {
310+
type Error = $crate::Error;
311+
312+
fn try_from(value: $int) -> $crate::Result<Self> {
313+
let mut buf = [0u8; 16];
314+
let mut writer = $crate::SliceWriter::new(&mut buf[..]);
315+
value.encode_value(&mut writer)?;
316+
let buf = writer.finish()?;
317+
Int::new(buf)
318+
}
319+
}
320+
)+
321+
};
322+
}
323+
324+
impl_from_traits!(i8, i16, i32, i64, i128);
325+
326+
#[cfg(test)]
327+
#[allow(clippy::unwrap_used)]
328+
mod tests {
329+
use super::Int;
330+
331+
#[test]
332+
fn from_uint() {
333+
assert_eq!(Int::try_from(i8::MIN).unwrap().as_bytes(), &[0x80]);
334+
assert_eq!(Int::try_from(i8::MAX).unwrap().as_bytes(), &[0x7F]);
335+
assert_eq!(Int::try_from(i16::MIN).unwrap().as_bytes(), &[0x80, 0]);
336+
assert_eq!(Int::try_from(i16::MAX).unwrap().as_bytes(), &[0x7F, 0xFF]);
337+
assert_eq!(
338+
Int::try_from(i32::MIN).unwrap().as_bytes(),
339+
&[0x80, 0, 0, 0]
340+
);
341+
assert_eq!(
342+
Int::try_from(i32::MAX).unwrap().as_bytes(),
343+
&[0x7F, 0xFF, 0xFF, 0xFF]
344+
);
345+
assert_eq!(
346+
Int::try_from(i64::MIN).unwrap().as_bytes(),
347+
&[
348+
0x80, 0, 0, 0, //
349+
0, 0, 0, 0
350+
]
351+
);
352+
assert_eq!(
353+
Int::try_from(i64::MAX).unwrap().as_bytes(),
354+
&[
355+
0x7F, 0xFF, 0xFF, 0xFF, //
356+
0xFF, 0xFF, 0xFF, 0xFF //
357+
]
358+
);
359+
assert_eq!(
360+
Int::try_from(i128::MIN).unwrap().as_bytes(),
361+
&[0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
362+
);
363+
assert_eq!(
364+
Int::try_from(i128::MAX).unwrap().as_bytes(),
365+
&[
366+
0x7F, 0xFF, 0xFF, 0xFF, //
367+
0xFF, 0xFF, 0xFF, 0xFF, //
368+
0xFF, 0xFF, 0xFF, 0xFF, //
369+
0xFF, 0xFF, 0xFF, 0xFF, //
370+
]
371+
);
372+
}
373+
}
305374
}
306375

307376
/// Ensure `INTEGER` is canonically encoded.

der/src/asn1/integer/uint.rs

+40
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,46 @@ mod allocating {
273273
UintRef { inner }
274274
}
275275
}
276+
277+
macro_rules! impl_from_traits {
278+
($($uint:ty),+) => {
279+
$(
280+
impl TryFrom<$uint> for Uint {
281+
type Error = $crate::Error;
282+
283+
fn try_from(value: $uint) -> $crate::Result<Self> {
284+
let mut buf = [0u8; 17];
285+
let mut writer = $crate::SliceWriter::new(&mut buf[..]);
286+
value.encode_value(&mut writer)?;
287+
let buf = writer.finish()?;
288+
Uint::new(buf)
289+
}
290+
}
291+
)+
292+
};
293+
}
294+
295+
impl_from_traits!(u8, u16, u32, u64, u128);
296+
297+
#[cfg(test)]
298+
#[allow(clippy::unwrap_used)]
299+
mod tests {
300+
use super::Uint;
301+
302+
#[test]
303+
fn from_uint() {
304+
assert_eq!(Uint::try_from(u8::MIN).unwrap().as_bytes(), &[0]);
305+
assert_eq!(Uint::try_from(u8::MAX).unwrap().as_bytes(), &[0xFF]);
306+
assert_eq!(Uint::try_from(u16::MIN).unwrap().as_bytes(), &[0]);
307+
assert_eq!(Uint::try_from(u16::MAX).unwrap().as_bytes(), &[0xFF; 2]);
308+
assert_eq!(Uint::try_from(u32::MIN).unwrap().as_bytes(), &[0]);
309+
assert_eq!(Uint::try_from(u32::MAX).unwrap().as_bytes(), &[0xFF; 4]);
310+
assert_eq!(Uint::try_from(u64::MIN).unwrap().as_bytes(), &[0]);
311+
assert_eq!(Uint::try_from(u64::MAX).unwrap().as_bytes(), &[0xFF; 8]);
312+
assert_eq!(Uint::try_from(u128::MIN).unwrap().as_bytes(), &[0]);
313+
assert_eq!(Uint::try_from(u128::MAX).unwrap().as_bytes(), &[0xFF; 16]);
314+
}
315+
}
276316
}
277317

278318
/// Decode an unsigned integer into a big endian byte slice with all leading

x509-cert/src/ext/pkix/crl.rs

+16
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ impl AssociatedOid for CrlNumber {
3030
impl_newtype!(CrlNumber, Uint);
3131
impl_extension!(CrlNumber, critical = false);
3232

33+
macro_rules! impl_from_traits {
34+
($($uint:ty),+) => {
35+
$(
36+
impl TryFrom<$uint> for CrlNumber {
37+
type Error = der::Error;
38+
39+
fn try_from(value: $uint) -> der::Result<Self> {
40+
Uint::try_from(value).map(Self)
41+
}
42+
}
43+
)+
44+
}
45+
}
46+
47+
impl_from_traits!(u8, u16, u32, u64, u128);
48+
3349
/// BaseCRLNumber as defined in [RFC 5280 Section 5.2.4].
3450
///
3551
/// ```text

0 commit comments

Comments
 (0)