pkcs1/lib.rs
1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![doc = include_str!("../README.md")]
4#![doc(
5 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
6 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
7)]
8#![forbid(unsafe_code)]
9#![warn(
10 clippy::mod_module_files,
11 clippy::unwrap_used,
12 missing_docs,
13 rust_2018_idioms,
14 unused_lifetimes,
15 unused_qualifications
16)]
17
18//! ## Usage
19//!
20//! ### Borrowed DER decoding
21//!
22//! [`RsaPrivateKeyRef`] and [`RsaPublicKeyRef`] borrow their integer data from the input.
23//! Import [`der::Decode`] to use `from_der`:
24//!
25//! ```rust
26//! use pkcs1::{RsaPrivateKeyRef, der::Decode};
27//!
28//! # let der = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/examples/rsa2048-priv.der"));
29//! let key = RsaPrivateKeyRef::from_der(der)?;
30//! # Ok::<(), pkcs1::Error>(())
31//! ```
32//!
33//! Here, `der` is a byte slice containing a PKCS#1 DER-encoded private key.
34//! `RsaPrivateKeyRef::try_from(der)` also decodes DER when `der` has type `&[u8]`.
35//! Both methods are available without enabling any features.
36//!
37//! ### Owned DER decoding and encoding
38//!
39//! Enable the `alloc` feature to use [`RsaPrivateKeyOwned`] and [`RsaPublicKeyOwned`].
40//! These types own their integer data and implement [`DecodeRsaPrivateKey`] and
41//! [`DecodeRsaPublicKey`], respectively:
42//!
43//! ```rust
44//! # #[cfg(feature = "alloc")]
45//! # {
46//! use pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey, RsaPrivateKeyOwned};
47//!
48//! # let der = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/examples/rsa2048-priv.der"));
49//! let key = RsaPrivateKeyOwned::from_pkcs1_der(der)?;
50//! let document = key.to_pkcs1_der()?;
51//! assert_eq!(document.as_bytes(), der);
52//! # }
53//! # Ok::<(), pkcs1::Error>(())
54//! ```
55//!
56//! [`EncodeRsaPrivateKey`] and [`EncodeRsaPublicKey`] support both borrowed and owned keys.
57//! The decoding traits require owned results; use [`der::Decode`] for the borrowed types as shown
58//! above. RSA implementations can also implement these traits for their own key types.
59//!
60//! ### PEM decoding and encoding
61//!
62//! Enable the `pem` feature (which also enables `alloc`) to use `from_pkcs1_pem` and
63//! `to_pkcs1_pem` with the PEM labels shown above:
64//!
65//! ```rust
66//! # #[cfg(feature = "pem")]
67//! # {
68//! use pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey, LineEnding, RsaPrivateKeyOwned};
69//!
70//! # let pem = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/examples/rsa2048-priv.pem"));
71//! let key = RsaPrivateKeyOwned::from_pkcs1_pem(pem)?;
72//! let encoded = key.to_pkcs1_pem(LineEnding::LF)?;
73//! assert_eq!(encoded.as_str(), pem);
74//! # }
75//! # Ok::<(), pkcs1::Error>(())
76//! ```
77//!
78//! Here, `pem` is a string containing a PEM-encoded RSA private key.
79//!
80//! For public keys, use `RsaPublicKeyOwned` with `DecodeRsaPublicKey` and `EncodeRsaPublicKey`
81//! (the method names are the same).
82
83#[cfg(feature = "alloc")]
84extern crate alloc;
85#[cfg(feature = "std")]
86extern crate std;
87
88mod error;
89mod params;
90mod private_key;
91mod public_key;
92mod traits;
93mod version;
94
95pub use der::{
96 self,
97 asn1::{ObjectIdentifier, UintRef},
98};
99
100pub use crate::{
101 error::{Error, Result},
102 params::{RsaOaepParams, RsaOaepParamsRef, RsaPssParams, RsaPssParamsRef, TrailerField},
103 private_key::{RsaPrivateKey, RsaPrivateKeyRef},
104 public_key::{RsaPublicKey, RsaPublicKeyRef},
105 traits::{DecodeRsaPrivateKey, DecodeRsaPublicKey},
106 version::Version,
107};
108
109#[cfg(feature = "alloc")]
110pub use crate::{
111 params::{RsaOaepParamsOwned, RsaPssParamsOwned},
112 private_key::{OtherPrimeInfos, RsaPrivateKeyOwned, other_prime_info::OtherPrimeInfo},
113 public_key::RsaPublicKeyOwned,
114 traits::{EncodeRsaPrivateKey, EncodeRsaPublicKey},
115};
116
117#[cfg(feature = "pem")]
118pub use der::pem::{self, LineEnding};
119
120/// `rsaEncryption` Object Identifier (OID)
121pub const ALGORITHM_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1");
122
123/// `AlgorithmIdentifier` for RSA.
124pub const ALGORITHM_ID: spki::AlgorithmIdentifierRef<'static> = spki::AlgorithmIdentifierRef {
125 oid: ALGORITHM_OID,
126 parameters: Some(der::asn1::AnyRef::NULL),
127};