tsumiki_pkix_types/lib.rs
1//! # tsumiki-pkix-types
2//!
3//! Common types for PKIX (Public Key Infrastructure using X.509).
4//!
5//! This crate provides shared type definitions used by both X.509 certificates
6//! and PKCS standards.
7//!
8//! ## Standards
9//!
10//! - [RFC 5280](https://datatracker.ietf.org/doc/html/rfc5280) - X.509 Certificate and CRL Profile
11//! - [RFC 3279](https://datatracker.ietf.org/doc/html/rfc3279) - Algorithm identifiers
12//! - [RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480) - Elliptic Curve algorithms
13//!
14//! ## Key Types
15//!
16//! - `AlgorithmIdentifier` - Algorithm OID with optional parameters
17//! - `Name` - X.500 distinguished name (e.g., "CN=example.com, O=Example Org")
18//! - `SubjectPublicKeyInfo` - Public key with algorithm information
19//! - `CertificateSerialNumber` - Certificate serial number
20//!
21//! ## Example
22//!
23//! ```
24//! use std::str::FromStr;
25//! use tsumiki_asn1::{Element, ObjectIdentifier};
26//! use tsumiki::decoder::Decoder;
27//! use tsumiki::encoder::Encoder;
28//! use tsumiki_pkix_types::AlgorithmIdentifier;
29//!
30//! // Create AlgorithmIdentifier for RSA
31//! let oid = ObjectIdentifier::from_str("1.2.840.113549.1.1.1")?; // rsaEncryption
32//! let alg = AlgorithmIdentifier::new(oid);
33//!
34//! // Encode and decode
35//! let element: Element = alg.encode()?;
36//! let decoded: AlgorithmIdentifier = element.decode()?;
37//! # Ok::<(), Box<dyn std::error::Error>>(())
38//! ```
39
40#![forbid(unsafe_code)]
41
42use std::cell::Cell;
43
44pub mod algorithm;
45pub mod directory_string;
46pub mod error;
47pub mod extension;
48pub mod key_identifier;
49pub mod name;
50pub mod oid_name;
51pub mod serial_number;
52pub mod subject_public_key_info;
53
54pub use algorithm::{
55 AlgorithmIdentifier, AlgorithmParameters,
56 parameters::{AlgorithmParameter, DsaParameters, EcParameters, RawAlgorithmParameter},
57};
58pub use directory_string::DirectoryString;
59pub use error::{Error, Result};
60pub use extension::Extension;
61pub use key_identifier::KeyIdentifier;
62pub use name::{AttributeTypeAndValue, Name, RelativeDistinguishedName};
63pub use oid_name::OidName;
64pub use serial_number::CertificateSerialNumber;
65pub use subject_public_key_info::SubjectPublicKeyInfo;
66
67thread_local! {
68 /// Global flag to control whether to use OID values or human-readable names in serialization
69 static USE_OID_VALUES: Cell<bool> = const { Cell::new(false) };
70}
71
72/// Set whether to use OID values instead of human-readable names in serialization.
73///
74/// When set to `true`, types like `AlgorithmIdentifier` and `AttributeTypeAndValue`
75/// will serialize using raw OID strings (e.g., "1.2.840.10045.3.1.7").
76///
77/// When set to `false` (default), they use human-readable names when available
78/// (e.g., "secp256r1", "CN").
79///
80/// # Example
81///
82/// ```
83/// use tsumiki_pkix_types::set_use_oid_values;
84///
85/// // Use human-readable names (default)
86/// set_use_oid_values(false);
87///
88/// // Use raw OID values
89/// set_use_oid_values(true);
90/// ```
91pub fn set_use_oid_values(use_oid: bool) {
92 USE_OID_VALUES.with(|flag| flag.set(use_oid));
93}
94
95/// Get whether to use OID values instead of human-readable names in serialization.
96///
97/// Returns `true` if raw OID values should be used, `false` if human-readable
98/// names should be used when available.
99///
100/// # Example
101///
102/// ```
103/// use tsumiki_pkix_types::{get_use_oid_values, set_use_oid_values};
104///
105/// set_use_oid_values(true);
106/// assert!(get_use_oid_values());
107///
108/// set_use_oid_values(false);
109/// assert!(!get_use_oid_values());
110/// ```
111pub fn get_use_oid_values() -> bool {
112 USE_OID_VALUES.with(|flag| flag.get())
113}