/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
pubmod error;
use byteorder::{BigEndian, ReadBytesExt};
usecrate::error::{Error, ErrorType};
// The following OID_BYTES_* consist of the contents of the bytes of an ASN.1 // OBJECT IDENTIFIER specifying the indicated OID (in other words, just the // value, and not the tag or length). pubconst OID_BYTES_SHA_256: &[u8] = &[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01]; pubconst OID_BYTES_SHA_384: &[u8] = &[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02]; pubconst OID_BYTES_SHA_512: &[u8] = &[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03]; pubconst OID_BYTES_SHA_1: &[u8] = &[0x2b, 0x0e, 0x03, 0x02, 0x1a];
/// Given a slice of DER bytes representing an RSA public key, extracts the bytes of the modulus /// as an unsigned integer. Also verifies that the public exponent is present (again as an /// unsigned integer). Finally verifies that reading these values consumes the entirety of the /// slice. /// RSAPublicKey ::= SEQUENCE { /// modulus INTEGER, -- n /// publicExponent INTEGER -- e /// } pubfn read_rsa_modulus(public_key: &[u8]) -> Result<Vec<u8>, Error> { letmut sequence = Sequence::new(public_key)?; let modulus_value = sequence.read_unsigned_integer()?; let _exponent = sequence.read_unsigned_integer()?; if !sequence.at_end() { return Err(error_here!(ErrorType::ExtraInput));
}
Ok(modulus_value.to_vec())
}
/// Given a slice of DER bytes representing a SubjectPublicKeyInfo, extracts /// the bytes of the parameters of the algorithm. Does not verify that all /// input is consumed. /// PublicKeyInfo ::= SEQUENCE { /// algorithm AlgorithmIdentifier, /// PublicKey BIT STRING /// } /// /// AlgorithmIdentifier ::= SEQUENCE { /// algorithm OBJECT IDENTIFIER, /// parameters ANY DEFINED BY algorithm OPTIONAL /// } pubfn read_spki_algorithm_parameters(spki: &[u8]) -> Result<Vec<u8>, Error> { letmut public_key_info = Sequence::new(spki)?; letmut algorithm_identifier = public_key_info.read_sequence()?; let _algorithm = algorithm_identifier.read_oid()?;
Ok(algorithm_identifier.read_rest().to_vec())
}
/// Given a slice of DER bytes representing a DigestInfo, extracts the bytes of /// the OID of the hash algorithm and the digest. /// DigestInfo ::= SEQUENCE { /// digestAlgorithm DigestAlgorithmIdentifier, /// digest Digest } /// /// DigestAlgorithmIdentifier ::= AlgorithmIdentifier /// /// AlgorithmIdentifier ::= SEQUENCE { /// algorithm OBJECT IDENTIFIER, /// parameters ANY DEFINED BY algorithm OPTIONAL } /// /// Digest ::= OCTET STRING pubfn read_digest_info(digest_info: &[u8]) -> Result<(&[u8], &[u8]), Error> { letmut sequence = Sequence::new(digest_info)?; letmut algorithm = sequence.read_sequence()?; let oid = algorithm.read_oid()?;
algorithm.read_null()?; if !algorithm.at_end() { return Err(error_here!(ErrorType::ExtraInput));
} let digest = sequence.read_octet_string()?; if !sequence.at_end() { return Err(error_here!(ErrorType::ExtraInput));
}
Ok((oid, digest))
}
/// Converts a slice of DER bytes representing an ECDSA signature to the concatenation of the bytes /// of `r` and `s`, each 0-padded to `coordinate_width`. Also verifies that this consumes the /// entirety of the slice. /// Ecdsa-Sig-Value ::= SEQUENCE { /// r INTEGER, /// s INTEGER } pubfn der_ec_sig_to_raw(encoded: &[u8], coordinate_width: usize) -> Result<Vec<u8>, Error> { let (r, s) = read_ec_sig_point(encoded)?; if r.len() > coordinate_width || s.len() > coordinate_width { return Err(error_here!(ErrorType::InvalidInput));
} letmut raw_signature = Vec::with_capacity(2 * coordinate_width);
raw_signature.resize(coordinate_width - r.len(), 0);
raw_signature.extend_from_slice(r);
raw_signature.resize((2 * coordinate_width) - s.len(), 0);
raw_signature.extend_from_slice(s);
Ok(raw_signature)
}
/// Given a slice of DER bytes representing an ECDSA signature, extracts the bytes of `r` and `s` /// as unsigned integers. Also verifies that this consumes the entirety of the slice. /// Ecdsa-Sig-Value ::= SEQUENCE { /// r INTEGER, /// s INTEGER } fn read_ec_sig_point(signature: &[u8]) -> Result<(&[u8], &[u8]), Error> { letmut sequence = Sequence::new(signature)?; let r = sequence.read_unsigned_integer()?; let s = sequence.read_unsigned_integer()?; if !sequence.at_end() { return Err(error_here!(ErrorType::ExtraInput));
}
Ok((r, s))
}
/// Given a slice of DER bytes representing an X.509 certificate, extracts the encoded serial /// number, issuer, and subject. Does not verify that the remainder of the certificate is in any /// way well-formed. /// Certificate ::= SEQUENCE { /// tbsCertificate TBSCertificate, /// signatureAlgorithm AlgorithmIdentifier, /// signatureValue BIT STRING } /// /// TBSCertificate ::= SEQUENCE { /// version [0] EXPLICIT Version DEFAULT v1, /// serialNumber CertificateSerialNumber, /// signature AlgorithmIdentifier, /// issuer Name, /// validity Validity, /// subject Name, /// ... /// /// CertificateSerialNumber ::= INTEGER /// /// Name ::= CHOICE { -- only one possibility for now -- /// rdnSequence RDNSequence } /// /// RDNSequence ::= SEQUENCE OF RelativeDistinguishedName /// /// Validity ::= SEQUENCE { /// notBefore Time, /// notAfter Time } #[allow(clippy::type_complexity)] pubfn read_encoded_certificate_identifiers(
certificate: &[u8],
) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>), Error> { letmut certificate_sequence = Sequence::new(certificate)?; letmut tbs_certificate_sequence = certificate_sequence.read_sequence()?; let _version = tbs_certificate_sequence.read_optional_tagged_value(0)?; let serial_number = tbs_certificate_sequence.read_encoded_sequence_component(INTEGER)?; let _signature = tbs_certificate_sequence.read_sequence()?; let issuer =
tbs_certificate_sequence.read_encoded_sequence_component(SEQUENCE | CONSTRUCTED)?; let _validity = tbs_certificate_sequence.read_sequence()?; let subject =
tbs_certificate_sequence.read_encoded_sequence_component(SEQUENCE | CONSTRUCTED)?;
Ok((serial_number, issuer, subject))
}
/// PrivateKeyInfo ::= SEQUENCE { /// version Version, /// privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, /// privateKey PrivateKey, /// attributes [0] IMPLICIT Attributes OPTIONAL } /// /// Version ::= INTEGER /// /// PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier /// /// PrivateKey ::= OCTET STRING /// /// Attributes ::= SET OF Attribute /// /// RSAPrivateKey ::= SEQUENCE { /// version Version, /// modulus INTEGER, -- n /// publicExponent INTEGER, -- e /// privateExponent INTEGER, -- d /// prime1 INTEGER, -- p /// prime2 INTEGER, -- q /// exponent1 INTEGER, -- d mod (p-1) /// exponent2 INTEGER, -- d mod (q-1) /// coefficient INTEGER, -- (inverse of q) mod p /// otherPrimeInfos OtherPrimeInfos OPTIONAL /// } /// /// ECPrivateKey ::= SEQUENCE { /// version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), /// privateKey OCTET STRING, /// parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, /// publicKey [1] BIT STRING OPTIONAL /// } pubfn read_private_key_info(private_key_info: &[u8]) -> Result<PrivateKeyInfo, Error> { letmut private_key_info = Sequence::new(private_key_info)?; let _version = private_key_info.read_unsigned_integer()?; letmut algorithm_identifier = private_key_info.read_sequence()?; let algorithm = algorithm_identifier.read_oid()?; let private_key_bytes = private_key_info.read_octet_string()?; letmut private_key = Sequence::new(private_key_bytes)?; if algorithm == OID_BYTES_RSA_ENCRYPTION { let _version = private_key.read_unsigned_integer()?; let modulus = private_key.read_unsigned_integer()?; let _public_exponent = private_key.read_unsigned_integer()?; let private_exponent = private_key.read_unsigned_integer()?; return Ok(PrivateKeyInfo::RSA(RSAPrivateKey {
modulus: modulus.to_vec(),
private_exponent: private_exponent.to_vec(),
}));
} if algorithm == OID_BYTES_EC_PUBLIC_KEY { let algorithm_parameters = algorithm_identifier.read_oid()?; // Currently, only secp256r1 is supported. if algorithm_parameters != OID_BYTES_SECP256R1 { return Err(error_here!(ErrorType::UnsupportedInput));
} let _version = private_key.read_unsigned_integer()?; let private_key_bytes = private_key.read_octet_string()?; return Ok(PrivateKeyInfo::EC(ECPrivateKey {
private_key: private_key_bytes.to_vec(),
}));
}
Err(error_here!(ErrorType::UnsupportedInput))
}
/// Helper macro for reading some bytes from a slice while checking the slice is long enough. /// Returns a pair consisting of a slice of the bytes read and a slice of the rest of the bytes /// from the original slice.
macro_rules! try_read_bytes {
($data:ident, $len:expr) => {{ if $data.len() < $len { return Err(error_here!(ErrorType::TruncatedInput));
}
$data.split_at($len)
}};
}
/// ASN.1 tag identifying an integer. const INTEGER: u8 = 0x02; /// ASN.1 tag identifying an octet string. const OCTET_STRING: u8 = 0x04; /// ASN.1 tag identifying a null value. const NULL: u8 = 0x05; /// ASN.1 tag identifying an object identifier (OID). const OBJECT_IDENTIFIER: u8 = 0x06; /// ASN.1 tag identifying a sequence. const SEQUENCE: u8 = 0x10; /// ASN.1 tag modifier identifying an item as constructed. const CONSTRUCTED: u8 = 0x20; /// ASN.1 tag modifier identifying an item as context-specific. const CONTEXT_SPECIFIC: u8 = 0x80;
/// A helper struct for reading items from a DER SEQUENCE (in this case, all sequences are /// assumed to be CONSTRUCTED). struct Sequence<'a> { /// The contents of the SEQUENCE.
contents: Der<'a>,
}
impl<'a> Sequence<'a> { fn new(input: &'a [u8]) -> Result<Sequence<'a>, Error> { letmut der = Der::new(input); let (_, _, sequence_bytes) = der.read_tlv(SEQUENCE | CONSTRUCTED)?; // We're assuming we want to consume the entire input for now. if !der.at_end() { return Err(error_here!(ErrorType::ExtraInput));
}
Ok(Sequence {
contents: Der::new(sequence_bytes),
})
}
// TODO: we're not exhaustively validating this integer fn read_unsigned_integer(&mutself) -> Result<&'a [u8], Error> { let (_, _, bytes) = self.contents.read_tlv(INTEGER)?; if bytes.is_empty() { return Err(error_here!(ErrorType::InvalidInput));
} // There may be a leading zero (we should also check that the first bit // of the rest of the integer is set). if bytes[0] == 0 && bytes.len() > 1 { let (_, integer) = bytes.split_at(1);
Ok(integer)
} else {
Ok(bytes)
}
}
/// A helper struct for reading DER data. The contents are treated like a cursor, so its position /// is updated as data is read. struct Der<'a> {
contents: &'a [u8],
}
// In theory, a caller could encounter an error and try another operation, in which case we may // be in an inconsistent state. As long as this implementation isn't exposed to code that would // use it incorrectly (i.e. it stays in this module and we only expose a stateless API), it // should be safe. /// Given an expected tag, reads the next (tag, lengh, value) from the contents. Most /// consumers will only be interested in the value, but some may want the entire encoded /// contents, in which case the returned tuple can be concatenated. fn read_tlv(&mutself, tag: u8) -> Result<(u8, Vec<u8>, &'a [u8]), Error> { let contents = self.contents; let (tag_read, rest) = try_read_bytes!(contents, 1); if tag_read[0] != tag { return Err(error_here!(ErrorType::InvalidInput));
} letmut accumulated_length_bytes = Vec::with_capacity(4); let (length1, rest) = try_read_bytes!(rest, 1);
accumulated_length_bytes.extend_from_slice(length1); let (length, to_read_from) = if length1[0] < 0x80 {
(length1[0] as usize, rest)
} elseif length1[0] == 0x81 { let (length, rest) = try_read_bytes!(rest, 1);
accumulated_length_bytes.extend_from_slice(length); if length[0] < 0x80 { return Err(error_here!(ErrorType::InvalidInput));
}
(length[0] as usize, rest)
} elseif length1[0] == 0x82 { let (mut lengths, rest) = try_read_bytes!(rest, 2);
accumulated_length_bytes.extend_from_slice(lengths); let length = lengths
.read_u16::<BigEndian>()
.map_err(|_| error_here!(ErrorType::LibraryFailure))?; if length < 256 { return Err(error_here!(ErrorType::InvalidInput));
}
(length as usize, rest)
} else { return Err(error_here!(ErrorType::UnsupportedInput));
}; let (contents, rest) = try_read_bytes!(to_read_from, length); self.contents = rest;
Ok((tag, accumulated_length_bytes, contents))
}
#[test] fn der_test_not_shortest_two_byte_length_encoding() { let input = vec![SEQUENCE, 0x81, 1, 1]; letmut der = Der::new(&input);
assert!(der.read_tlv(SEQUENCE).is_err());
}
#[test] fn der_test_not_shortest_three_byte_length_encoding() { let input = vec![SEQUENCE, 0x82, 0, 1, 1]; letmut der = Der::new(&input);
assert!(der.read_tlv(SEQUENCE).is_err());
}
#[test] fn der_test_indefinite_length_unsupported() { let input = vec![SEQUENCE, 0x80, 1, 2, 3, 0x00, 0x00]; letmut der = Der::new(&input);
assert!(der.read_tlv(SEQUENCE).is_err());
}
#[test] fn der_test_input_too_long() { // This isn't valid DER (the contents of the SEQUENCE are truncated), but it demonstrates // that we don't try to read too much if we're given a long length (and also that we don't // support lengths 2^16 and up). let input = vec![SEQUENCE, 0x83, 0x01, 0x00, 0x01, 1, 1, 1, 1]; letmut der = Der::new(&input);
assert!(der.read_tlv(SEQUENCE).is_err());
}
#[test] fn test_read_rsa_modulus() { let rsa_key = include_bytes!("../test/rsa.bin"); let result = read_rsa_modulus(rsa_key);
assert!(result.is_ok()); let modulus = result.unwrap();
assert_eq!(modulus, include_bytes!("../test/modulus.bin").to_vec());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.