Skip to content

Commit be3a4c4

Browse files
committed
Move AlgorithmIdentifier type into alg_id
1 parent 3ac5526 commit be3a4c4

File tree

2 files changed

+67
-61
lines changed

2 files changed

+67
-61
lines changed

src/alg_id.rs

+66-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//! Common values of the PKIX [`AlgorithmIdentifier`] type.
1+
//! The PKIX [`AlgorithmIdentifier`] type, and common values.
22
//!
33
//! If you need to use an [`AlgorithmIdentifier`] not defined here,
44
//! you can define it locally.
55
6-
use super::AlgorithmIdentifier;
6+
use core::fmt;
7+
use core::ops::Deref;
78

89
// See src/data/README.md.
910

@@ -269,3 +270,66 @@ pub const RSA_PSS_SHA512: AlgorithmIdentifier =
269270
/// ```
270271
pub const ED25519: AlgorithmIdentifier =
271272
AlgorithmIdentifier::from_slice(include_bytes!("data/alg-ed25519.der"));
273+
274+
/// A DER encoding of the PKIX AlgorithmIdentifier type:
275+
///
276+
/// ```ASN.1
277+
/// AlgorithmIdentifier ::= SEQUENCE {
278+
/// algorithm OBJECT IDENTIFIER,
279+
/// parameters ANY DEFINED BY algorithm OPTIONAL }
280+
/// -- contains a value of the type
281+
/// -- registered for use with the
282+
/// -- algorithm object identifier value
283+
/// ```
284+
/// (from <https://www.rfc-editor.org/rfc/rfc5280#section-4.1.1.2>)
285+
///
286+
/// The outer sequence encoding is *not included*, so this is the DER encoding
287+
/// of an OID for `algorithm` plus the `parameters` value.
288+
///
289+
/// For example, this is the `rsaEncryption` algorithm (but prefer to use the constant
290+
/// [`RSA_ENCRYPTION`] instead):
291+
///
292+
/// ```
293+
/// let rsa_encryption = rustls_pki_types::AlgorithmIdentifier::from_slice(
294+
/// &[
295+
/// // algorithm: 1.2.840.113549.1.1.1
296+
/// 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
297+
/// // parameters: NULL
298+
/// 0x05, 0x00
299+
/// ]
300+
/// );
301+
/// assert_eq!(rustls_pki_types::alg_id::RSA_ENCRYPTION, rsa_encryption);
302+
/// ```
303+
///
304+
/// Common values for this type are provided in this module.
305+
#[derive(Clone, Copy, PartialEq, Eq)]
306+
pub struct AlgorithmIdentifier(&'static [u8]);
307+
308+
impl AlgorithmIdentifier {
309+
/// Makes a new `AlgorithmIdentifier` from a static octet slice.
310+
///
311+
/// This does not validate the contents of the slice.
312+
pub const fn from_slice(bytes: &'static [u8]) -> Self {
313+
Self(bytes)
314+
}
315+
}
316+
317+
impl AsRef<[u8]> for AlgorithmIdentifier {
318+
fn as_ref(&self) -> &[u8] {
319+
self.0
320+
}
321+
}
322+
323+
impl fmt::Debug for AlgorithmIdentifier {
324+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
325+
super::hex(f, self.0)
326+
}
327+
}
328+
329+
impl Deref for AlgorithmIdentifier {
330+
type Target = [u8];
331+
332+
fn deref(&self) -> &Self::Target {
333+
self.as_ref()
334+
}
335+
}

src/lib.rs

+1-59
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ mod server_name;
9595
#[cfg(feature = "alloc")]
9696
pub mod pem;
9797

98+
pub use alg_id::AlgorithmIdentifier;
9899
pub use server_name::{
99100
AddrParseError, DnsName, InvalidDnsNameError, IpAddr, Ipv4Addr, Ipv6Addr, ServerName,
100101
};
@@ -900,65 +901,6 @@ pub trait SignatureVerificationAlgorithm: Send + Sync + fmt::Debug {
900901
#[derive(Debug, Copy, Clone)]
901902
pub struct InvalidSignature;
902903

903-
/// A DER encoding of the PKIX AlgorithmIdentifier type:
904-
///
905-
/// ```ASN.1
906-
/// AlgorithmIdentifier ::= SEQUENCE {
907-
/// algorithm OBJECT IDENTIFIER,
908-
/// parameters ANY DEFINED BY algorithm OPTIONAL }
909-
/// -- contains a value of the type
910-
/// -- registered for use with the
911-
/// -- algorithm object identifier value
912-
/// ```
913-
/// (from <https://www.rfc-editor.org/rfc/rfc5280#section-4.1.1.2>)
914-
///
915-
/// The outer sequence encoding is *not included*, so this is the DER encoding
916-
/// of an OID for `algorithm` plus the `parameters` value.
917-
///
918-
/// For example, this is the `rsaEncryption` algorithm:
919-
///
920-
/// ```
921-
/// let rsa_encryption = rustls_pki_types::AlgorithmIdentifier::from_slice(
922-
/// &[
923-
/// // algorithm: 1.2.840.113549.1.1.1
924-
/// 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
925-
/// // parameters: NULL
926-
/// 0x05, 0x00
927-
/// ]
928-
/// );
929-
/// ```
930-
#[derive(Clone, Copy, PartialEq, Eq)]
931-
pub struct AlgorithmIdentifier(&'static [u8]);
932-
933-
impl AlgorithmIdentifier {
934-
/// Makes a new `AlgorithmIdentifier` from a static octet slice.
935-
///
936-
/// This does not validate the contents of the slice.
937-
pub const fn from_slice(bytes: &'static [u8]) -> Self {
938-
Self(bytes)
939-
}
940-
}
941-
942-
impl AsRef<[u8]> for AlgorithmIdentifier {
943-
fn as_ref(&self) -> &[u8] {
944-
self.0
945-
}
946-
}
947-
948-
impl fmt::Debug for AlgorithmIdentifier {
949-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
950-
hex(f, self.0)
951-
}
952-
}
953-
954-
impl Deref for AlgorithmIdentifier {
955-
type Target = [u8];
956-
957-
fn deref(&self) -> &Self::Target {
958-
self.as_ref()
959-
}
960-
}
961-
962904
/// A timestamp, tracking the number of non-leap seconds since the Unix epoch.
963905
///
964906
/// The Unix epoch is defined January 1, 1970 00:00:00 UTC.

0 commit comments

Comments
 (0)