Skip to content

Commit 980becb

Browse files
committed
Move utils.rs functions to x509.rs and remove module
1 parent 80c575d commit 980becb

File tree

8 files changed

+60
-69
lines changed

8 files changed

+60
-69
lines changed

src/certificate.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
use crate::error::{X509Error, X509Result};
44
use crate::extensions::*;
55
use crate::time::ASN1Time;
6-
use crate::utils::format_serial;
76
#[cfg(feature = "validate")]
87
use crate::validate::*;
98
use crate::x509::{
10-
parse_serial, parse_signature_value, AlgorithmIdentifier, SubjectPublicKeyInfo, X509Name,
11-
X509Version,
9+
format_serial, parse_serial, parse_signature_value, AlgorithmIdentifier, SubjectPublicKeyInfo,
10+
X509Name, X509Version,
1211
};
1312

1413
#[cfg(feature = "verify")]

src/cri_attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub enum ParsedCriAttribute<'a> {
121121

122122
pub(crate) mod parser {
123123
use crate::cri_attributes::*;
124-
use crate::utils::DirectoryString;
124+
use crate::x509::DirectoryString;
125125
use asn1_rs::{DerParser, Input};
126126
use lazy_static::lazy_static;
127127
use nom::combinator::map;

src/extensions/subject_key_identifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::Deref;
44
use asn1_rs::Alias;
55

66
use crate::error::X509Error;
7-
use crate::utils::format_serial;
7+
use crate::x509::format_serial;
88

99
/// <pre>
1010
/// SubjectKeyIdentifier ::= KeyIdentifier

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ pub mod revocation_list;
147147
pub mod signature_algorithm;
148148
pub mod signature_value;
149149
pub mod time;
150-
pub mod utils;
151150
#[cfg(feature = "validate")]
152151
#[cfg_attr(docsrs, doc(cfg(feature = "validate")))]
153152
pub mod validate;

src/prelude.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub use crate::objects::*;
99
pub use crate::pem::*;
1010
pub use crate::revocation_list::*;
1111
pub use crate::time::*;
12-
pub use crate::utils::*;
1312
#[cfg(feature = "validate")]
1413
pub use crate::validate::*;
1514
pub use crate::x509::*;

src/revocation_list.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::error::{X509Error, X509Result};
22
use crate::extensions::*;
33
use crate::time::ASN1Time;
4-
use crate::utils::format_serial;
5-
use crate::x509::{parse_serial, AlgorithmIdentifier, ReasonCode, X509Name, X509Version};
4+
use crate::x509::{
5+
format_serial, parse_serial, AlgorithmIdentifier, ReasonCode, X509Name, X509Version,
6+
};
67

78
#[cfg(feature = "verify")]
89
use crate::verify::verify_signature;

src/utils.rs

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/x509.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use crate::public_key::*;
1010

1111
use asn1_rs::num_bigint::BigUint;
1212
use asn1_rs::{
13-
Alias, Any, BerError, BitString, DerParser, Enumerated, FromDer, Header, Input, Integer,
14-
OptTaggedExplicit, Sequence, Tag, Tagged,
13+
Alias, Any, BerError, BitString, BmpString, Choice, DerParser, Enumerated, FromDer, Header,
14+
Input, Integer, OptTaggedExplicit, PrintableString, Sequence, Tag, Tagged, TeletexString,
15+
UniversalString, Utf8String,
1516
};
1617
use core::convert::TryFrom;
1718
use data_encoding::HEXUPPER;
@@ -71,6 +72,41 @@ newtype_enum! {
7172
}
7273
}
7374

75+
/// The DirectoryString type is defined as a choice of PrintableString, TeletexString,
76+
/// BMPString, UTF8String, and UniversalString.
77+
///
78+
/// <pre>
79+
/// RFC 5280, 4.1.2.4. Issuer
80+
/// DirectoryString ::= CHOICE {
81+
/// teletexString TeletexString (SIZE (1..MAX)),
82+
/// printableString PrintableString (SIZE (1..MAX)),
83+
/// universalString UniversalString (SIZE (1..MAX)),
84+
/// utf8String UTF8String (SIZE (1..MAX)),
85+
/// bmpString BMPString (SIZE (1..MAX))
86+
/// }
87+
/// </pre>
88+
#[derive(Debug, PartialEq, Eq, Choice)]
89+
#[asn1(parse = "DER", encode = "")]
90+
pub enum DirectoryString<'a> {
91+
Teletex(TeletexString<'a>),
92+
Printable(PrintableString<'a>),
93+
Universal(UniversalString<'a>),
94+
Utf8(Utf8String<'a>),
95+
Bmp(BmpString<'a>),
96+
}
97+
98+
impl fmt::Display for DirectoryString<'_> {
99+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100+
match self {
101+
DirectoryString::Teletex(s) => f.write_str(s.as_ref()),
102+
DirectoryString::Printable(s) => f.write_str(s.as_ref()),
103+
DirectoryString::Universal(s) => f.write_str(s.as_ref()),
104+
DirectoryString::Utf8(s) => f.write_str(s.as_ref()),
105+
DirectoryString::Bmp(s) => f.write_str(s.as_ref()),
106+
}
107+
}
108+
}
109+
74110
/// A generic attribute type and value
75111
///
76112
/// These objects are used as [`RelativeDistinguishedName`] components.
@@ -584,6 +620,15 @@ pub(crate) fn parse_serial(input: Input<'_>) -> IResult<Input<'_>, (&[u8], BigUi
584620
Ok((rem, (slice, big)))
585621
}
586622

623+
/// Formats a slice to a colon-separated hex string (for ex `01:02:ff:ff`)
624+
pub fn format_serial(i: &[u8]) -> String {
625+
let mut s = i.iter().fold(String::with_capacity(3 * i.len()), |a, b| {
626+
a + &format!("{b:02x}:")
627+
});
628+
s.pop();
629+
s
630+
}
631+
587632
#[cfg(test)]
588633
mod tests {
589634
use crate::certificate::Validity;
@@ -615,6 +660,12 @@ mod tests {
615660
assert!(r.is_ok());
616661
}
617662

663+
#[test]
664+
fn test_format_serial() {
665+
let b: &[u8] = &[1, 2, 3, 4, 0xff];
666+
assert_eq!("01:02:03:04:ff", format_serial(b));
667+
}
668+
618669
#[test]
619670
fn test_x509_name() {
620671
let name = X509Name {

0 commit comments

Comments
 (0)