Skip to content

Commit

Permalink
cryptographic-message-syntax: new crate implementing support for RFC …
Browse files Browse the repository at this point in the history
…5652

Embedded signatures in Mach-O binaries appear to be
Cryptographic Message Syntax (CMS) as defined by RFC 5652. In order to
support signing Mach-O binaries without using Apple's `codesign` tool,
we'll need to implement support for CMS.

Because CMS is a standalone concept independent of Apple, we introduce
a new crate dedicated to implementing CMS. I'm not sure how much of
it will be implemented. Time will tell.

This commit implements basic ASN.1 (de)serialization for CMS - but only
the pieces found in some code signatures I extracted from a few select
Mach-O binaries. There are Rust types for all ASN.1 primitives
recursively references by CMS. But not all of them support decoding.
Implementing them should be relatively trivial, however.

In addition to the low-level ASN.1 types, we provide a higher-level Rust
interface. It supports useful operations, such as verifying the
signature on the signed data. Crate documentation takes care to warn
people about the limited nature of the verification.

To prove it works, we implement a test that attempts to read and verify
the signature integrity from CMS blobs in Mach-O binaries. Strangely,
we're getting malformed ASN.1 on binaries like /usr/bin/tee. Looks to
be something with UTCTime parsing. Most CMS blobs parse just fine
though. Weird. We'll figure this out later.
  • Loading branch information
indygreg committed Mar 14, 2021
1 parent 967ef99 commit b00a76a
Show file tree
Hide file tree
Showing 18 changed files with 6,956 additions and 17 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
'cryptographic-message-syntax',
'oxidized-importer',
'pyembed',
'pyoxidizer',
Expand Down
17 changes: 17 additions & 0 deletions cryptographic-message-syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "cryptographic-message-syntax"
version = "0.1.0"
authors = ["Gregory Szorc <gregory.szorc@gmail.com>"]
edition = "2018"
license = "MPL-2.0"
description = "A pure Rust implementation of Crypographic Message Syntax (RFC 5652)"
keywords = ["cms", "rfc5652", "apple", "codesign"]
homepage = "https://github.com/indygreg/PyOxidizer"
repository = "https://github.com/indygreg/PyOxidizer.git"
readme = "README.md"

[dependencies]
bcder = "0.6"
bytes = "1.0"
chrono = "0.4"
ring = "0.16"
20 changes: 20 additions & 0 deletions cryptographic-message-syntax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# cryptographic-message-syntax

`cryptographic-message-syntax` is a pure Rust implementation of
Cryptographic Message Syntax (CMS) as defined by RFC 5652.

From a high level CMS defines a way to digitally sign and authenticate
arbitrary content.

CMS is used to power the digital signatures embedded within Mach-O binaries
on Apple platforms such as macOS and iOS. The primitives in this crate could
be used to sign and authenticate Mach-O binaries. (See the sister
`tugger-apple-codesign` crate in the Git repository for code that does
just this.)

This crate is developed as part of the
[PyOxidizer](https://github.com/indygreg/PyOxidizer.git) project and is
developed in that repository. While this crate is developed as part of a
larger product, it is authored such that it can be used standalone and
modifications to supports its use outside of its original use case are
very much welcome!
218 changes: 218 additions & 0 deletions cryptographic-message-syntax/src/algorithm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use {
crate::CmsError,
bcder::{ConstOid, Oid},
bytes::Bytes,
ring::{digest::SHA256, signature::VerificationAlgorithm},
std::convert::TryFrom,
};

/// SHA-256 digest algorithm.
///
/// 2.16.840.1.101.3.4.2.1
const OID_SHA256: ConstOid = Oid(&[96, 134, 72, 1, 101, 3, 4, 2, 1]);

/// RSA+SHA-1 encryption.
///
/// 1.2.840.113549.1.1.5
const OID_SHA1_RSA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 1, 5]);

/// RSA+SHA-256 encryption.
///
/// 1.2.840.113549.1.1.11
const OID_SHA256_RSA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 1, 11]);

/// RSAES-PKCS1-v1_5
///
/// 1.2.840.113549.1.1.1
const OID_RSAES_PKCS_V15: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 1, 1]);

/// RSA encryption.
///
/// 1.2.840.113549.1.1.1
const OID_RSA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 1, 1]);

/// A hashing algorithm used for digesting data.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum DigestAlgorithm {
/// SHA-256.
///
/// Corresponds to OID 2.16.840.1.101.3.4.2.1.
Sha256,
}

impl TryFrom<&Oid> for DigestAlgorithm {
type Error = CmsError;

fn try_from(v: &Oid) -> Result<Self, Self::Error> {
if v == &OID_SHA256 {
Ok(Self::Sha256)
} else {
Err(CmsError::UnknownDigestAlgorithm(v.clone()))
}
}
}

impl TryFrom<&crate::asn1::rfc5652::DigestAlgorithmIdentifier> for DigestAlgorithm {
type Error = CmsError;

fn try_from(v: &crate::asn1::rfc5652::DigestAlgorithmIdentifier) -> Result<Self, Self::Error> {
Self::try_from(&v.algorithm)
}
}

impl From<DigestAlgorithm> for Oid {
fn from(alg: DigestAlgorithm) -> Self {
match alg {
DigestAlgorithm::Sha256 => Oid(Bytes::copy_from_slice(OID_SHA256.as_ref())),
}
}
}

impl From<DigestAlgorithm> for crate::asn1::rfc5652::DigestAlgorithmIdentifier {
fn from(alg: DigestAlgorithm) -> Self {
Self {
algorithm: alg.into(),
parameters: None,
}
}
}

impl DigestAlgorithm {
/// Create a new content hasher for this algorithm.
pub fn as_hasher(&self) -> ring::digest::Context {
match self {
Self::Sha256 => ring::digest::Context::new(&SHA256),
}
}
}

/// An algorithm used to digitally sign content.
///
/// Instances can be converted to/from the underlying ASN.1 type and
/// OIDs.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SignatureAlgorithm {
/// SHA-1 with RSA encryption.
///
/// Corresponds to OID 1.2.840.113549.1.1.5.
Sha1Rsa,

/// SHA-256 with RSA encryption.
///
/// Corresponds to OID 1.2.840.113549.1.1.11.
Sha256Rsa,

/// RSAES-PKCS1-v1_5 encryption scheme.
///
/// Corresponds to OID 1.2.840.113549.1.1.1.
RsaesPkcsV15,
}

impl SignatureAlgorithm {
/// Convert this algorithm into a verification algorithm.
///
/// This enables you to easily obtain a ring signature verified based on
/// the type of algorithm.
pub fn as_verification_algorithm(&self) -> &'static impl VerificationAlgorithm {
match self {
SignatureAlgorithm::Sha1Rsa => {
&ring::signature::RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY
}
SignatureAlgorithm::Sha256Rsa => &ring::signature::RSA_PKCS1_2048_8192_SHA256,
SignatureAlgorithm::RsaesPkcsV15 => {
&ring::signature::RSA_PKCS1_1024_8192_SHA256_FOR_LEGACY_USE_ONLY
}
}
}
}

impl TryFrom<&Oid> for SignatureAlgorithm {
type Error = CmsError;

fn try_from(v: &Oid) -> Result<Self, Self::Error> {
if v == &OID_SHA1_RSA {
Ok(Self::Sha1Rsa)
} else if v == &OID_SHA256_RSA {
Ok(Self::Sha256Rsa)
} else if v == &OID_RSAES_PKCS_V15 {
Ok(Self::RsaesPkcsV15)
} else {
Err(CmsError::UnknownSignatureAlgorithm(v.clone()))
}
}
}

impl TryFrom<&crate::asn1::rfc5652::SignatureAlgorithmIdentifier> for SignatureAlgorithm {
type Error = CmsError;

fn try_from(
v: &crate::asn1::rfc5652::SignatureAlgorithmIdentifier,
) -> Result<Self, Self::Error> {
Self::try_from(&v.algorithm)
}
}

impl From<SignatureAlgorithm> for Oid {
fn from(v: SignatureAlgorithm) -> Self {
match v {
SignatureAlgorithm::Sha1Rsa => Oid(Bytes::copy_from_slice(OID_SHA1_RSA.as_ref())),
SignatureAlgorithm::Sha256Rsa => Oid(Bytes::copy_from_slice(OID_SHA256_RSA.as_ref())),
SignatureAlgorithm::RsaesPkcsV15 => {
Oid(Bytes::copy_from_slice(OID_RSAES_PKCS_V15.as_ref()))
}
}
}
}

impl From<SignatureAlgorithm> for crate::asn1::rfc5652::SignatureAlgorithmIdentifier {
fn from(alg: SignatureAlgorithm) -> Self {
Self {
algorithm: alg.into(),
parameters: None,
}
}
}

/// An algorithm used to digitally sign content.
///
/// Instances can be converted to/from the underlying ASN.1 type and
/// OIDs.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum CertificateKeyAlgorithm {
/// RSA
///
/// Corresponds to OID 1.2.840.113549.1.1.1.
Rsa,
}

impl TryFrom<&Oid> for CertificateKeyAlgorithm {
type Error = CmsError;

fn try_from(v: &Oid) -> Result<Self, Self::Error> {
if v == &OID_RSA {
Ok(Self::Rsa)
} else {
Err(CmsError::UnknownSignatureAlgorithm(v.clone()))
}
}
}

impl TryFrom<&crate::asn1::rfc5280::AlgorithmIdentifier> for CertificateKeyAlgorithm {
type Error = CmsError;

fn try_from(v: &crate::asn1::rfc5280::AlgorithmIdentifier) -> Result<Self, Self::Error> {
Self::try_from(&v.algorithm)
}
}

impl From<CertificateKeyAlgorithm> for Oid {
fn from(v: CertificateKeyAlgorithm) -> Self {
match v {
CertificateKeyAlgorithm::Rsa => Oid(Bytes::copy_from_slice(OID_RSA.as_ref())),
}
}
}
Loading

0 comments on commit b00a76a

Please sign in to comment.