1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
//! PKCS#1 version identifier.
use crate::Error;
use der::{Decodable, Decoder, Encodable, Encoder, FixedTag, Tag};
/// Version identifier for PKCS#1 documents as defined in
/// [RFC 8017 Appendix 1.2].
///
/// > version is the version number, for compatibility with future
/// > revisions of this document. It SHALL be 0 for this version of the
/// > document, unless multi-prime is used; in which case, it SHALL be 1.
///
/// ```text
/// Version ::= INTEGER { two-prime(0), multi(1) }
/// (CONSTRAINED BY
/// {-- version must be multi if otherPrimeInfos present --})
/// ```
///
/// [RFC 8017 Appendix 1.2]: https://datatracker.ietf.org/doc/html/rfc8017#appendix-A.1.2
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u8)]
pub enum Version {
/// Denotes a `two-prime` key
TwoPrime = 0,
/// Denotes a `multi` (i.e. multi-prime) key
Multi = 1,
}
impl Version {
/// Is this a multi-prime RSA key?
pub fn is_multi(self) -> bool {
self == Self::Multi
}
}
impl From<Version> for u8 {
fn from(version: Version) -> Self {
version as u8
}
}
impl TryFrom<u8> for Version {
type Error = Error;
fn try_from(byte: u8) -> Result<Version, Error> {
match byte {
0 => Ok(Version::TwoPrime),
1 => Ok(Version::Multi),
_ => Err(Error::Version),
}
}
}
impl Decodable<'_> for Version {
fn decode(decoder: &mut Decoder<'_>) -> der::Result<Self> {
Version::try_from(u8::decode(decoder)?).map_err(|_| Self::TAG.value_error())
}
}
impl Encodable for Version {
fn encoded_len(&self) -> der::Result<der::Length> {
der::Length::ONE.for_tlv()
}
fn encode(&self, encoder: &mut Encoder<'_>) -> der::Result<()> {
u8::from(*self).encode(encoder)
}
}
impl FixedTag for Version {
const TAG: Tag = Tag::Integer;
}