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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
// 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/.
//! Apple trust policies.
//!
//! Apple operating systems have a number of pre-canned trust policies
//! that must be fulfilled in order to trust signed code. These are
//! often based off the presence of specific X.509 certificates in the
//! issuing chain and/or the presence of attributes in X.509 certificates.
//!
//! Trust policies are often engraved in code signatures as part of the
//! signed code requirements expression.
//!
//! This module defines a bunch of metadata for describing Apple trust
//! entities and also provides pre-canned policies that can be easily
//! constructed to match those employed by Apple's official signing tools.
//!
//! Apple's certificates can be found at
//! <https://www.apple.com/certificateauthority/>.
use {
crate::{
certificate::{
AppleCertificate, CertificateAuthorityExtension, CertificateProfile,
CodeSigningCertificateExtension,
},
code_requirement::{CodeRequirementExpression, CodeRequirementMatchExpression},
error::AppleCodesignError,
},
once_cell::sync::Lazy,
std::ops::Deref,
x509_certificate::CapturedX509Certificate,
};
/// Code signing requirement for Mac Developer ID.
///
/// `anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists and
/// (certificate leaf[field.1.2.840.113635.100.6.1.14] or certificate leaf[field.1.2.840.113635.100.6.1.13])`
static POLICY_MAC_DEVELOPER_ID: Lazy<CodeRequirementExpression<'static>> = Lazy::new(|| {
CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::AnchorAppleGeneric),
Box::new(CodeRequirementExpression::CertificateGeneric(
1,
CertificateAuthorityExtension::DeveloperId.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
)),
Box::new(CodeRequirementExpression::Or(
Box::new(CodeRequirementExpression::CertificateGeneric(
0,
CodeSigningCertificateExtension::DeveloperIdInstaller.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
Box::new(CodeRequirementExpression::CertificateGeneric(
0,
CodeSigningCertificateExtension::DeveloperIdApplication.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
)),
)
});
/// Notarized executable.
///
/// `anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists and
/// certificate leaf[field.1.2.840.113635.100.6.1.13] exists and notarized'`
///
static POLICY_NOTARIZED_EXECUTABLE: Lazy<CodeRequirementExpression<'static>> = Lazy::new(|| {
CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::AnchorAppleGeneric),
Box::new(CodeRequirementExpression::CertificateGeneric(
1,
CertificateAuthorityExtension::DeveloperId.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
)),
Box::new(CodeRequirementExpression::CertificateGeneric(
0,
CodeSigningCertificateExtension::DeveloperIdApplication.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
)),
Box::new(CodeRequirementExpression::Notarized),
)
});
/// Notarized installer.
///
/// `'anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists
/// and (certificate leaf[field.1.2.840.113635.100.6.1.14] or certificate
/// leaf[field.1.2.840.113635.100.6.1.13]) and notarized'`
static POLICY_NOTARIZED_INSTALLER: Lazy<CodeRequirementExpression<'static>> = Lazy::new(|| {
CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::AnchorAppleGeneric),
Box::new(CodeRequirementExpression::CertificateGeneric(
1,
CertificateAuthorityExtension::DeveloperId.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
)),
Box::new(CodeRequirementExpression::Or(
Box::new(CodeRequirementExpression::CertificateGeneric(
0,
CodeSigningCertificateExtension::DeveloperIdInstaller.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
Box::new(CodeRequirementExpression::CertificateGeneric(
0,
CodeSigningCertificateExtension::DeveloperIdApplication.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
)),
)),
Box::new(CodeRequirementExpression::Notarized),
)
});
/// Defines well-known execution policies for signed code.
///
/// Instances can be obtained from a human-readable string for convenience. Those
/// strings are:
///
/// * `developer-id-signed`
/// * `developer-id-notarized-executable`
/// * `developer-id-notarized-installer`
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, clap::ValueEnum)]
pub enum ExecutionPolicy {
/// Code is signed by a certificate authorized for signing Mac applications or
/// installers and that certificate was issued by
/// [crate::apple_certificates::KnownCertificate::DeveloperIdG1] or
/// [crate::apple_certificates::KnownCertificate::DeveloperIdG2].
///
/// This is the policy that applies when you get a `Developer ID Application` or
/// `Developer ID Installer` certificate from Apple.
DeveloperIdSigned,
/// Like [Self::DeveloperIdSigned] but only applies to executables (not installers)
/// and the executable must be notarized.
///
/// If you notarize an individual executable, you effectively convert the
/// [Self::DeveloperIdSigned] policy into this variant.
DeveloperIdNotarizedExecutable,
/// Like [Self::DeveloperIdSigned] but only applies to installers (not executables)
/// and the installer must be notarized.
///
/// If you notarize an individual installer, you effectively convert the
/// [Self::DeveloperIdSigned] policy into this variant.
DeveloperIdNotarizedInstaller,
}
impl Deref for ExecutionPolicy {
type Target = CodeRequirementExpression<'static>;
fn deref(&self) -> &Self::Target {
match self {
Self::DeveloperIdSigned => POLICY_MAC_DEVELOPER_ID.deref(),
Self::DeveloperIdNotarizedExecutable => POLICY_NOTARIZED_EXECUTABLE.deref(),
Self::DeveloperIdNotarizedInstaller => POLICY_NOTARIZED_INSTALLER.deref(),
}
}
}
impl TryFrom<&str> for ExecutionPolicy {
type Error = AppleCodesignError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
"developer-id-signed" => Ok(Self::DeveloperIdSigned),
"developer-id-notarized-executable" => Ok(Self::DeveloperIdNotarizedExecutable),
"developer-id-notarized-installer" => Ok(Self::DeveloperIdNotarizedInstaller),
_ => Err(AppleCodesignError::UnknownPolicy(s.to_string())),
}
}
}
/// Derive a designated requirements expression given a code signing certificate.
///
/// This function figures out what the run-time requirements of a signed binary
/// should be given its code signing certificate.
///
/// We determine the flavor of Apple code signing certificate in use and apply an
/// appropriate requirements policy. We strive for behavior equivalence with
/// Apple's `codesign` tool.
pub fn derive_designated_requirements(
cert: &CapturedX509Certificate,
identifier: Option<String>,
) -> Result<Option<CodeRequirementExpression<'static>>, AppleCodesignError> {
let profile = if let Some(profile) = cert.apple_guess_profile() {
profile
} else {
return Ok(None);
};
match profile {
// These appear to be the same policy.
CertificateProfile::AppleDevelopment | CertificateProfile::AppleDistribution => {
let cn = cert.subject_common_name().ok_or_else(|| {
AppleCodesignError::PolicyFormulationError(format!(
"(deriving for {profile:?}) certificate common name not available"
))
})?;
let expr = CodeRequirementExpression::And(
// It chains to Apple root CA.
Box::new(CodeRequirementExpression::AnchorAppleGeneric),
Box::new(CodeRequirementExpression::And(
// It was signed by this cert.
Box::new(CodeRequirementExpression::CertificateField(
0,
"subject.CN".to_string().into(),
CodeRequirementMatchExpression::Equal(cn.into()),
)),
// That cert was signed by a CA with WWDR extension.
Box::new(CodeRequirementExpression::CertificateGeneric(
1,
CertificateAuthorityExtension::AppleWorldwideDeveloperRelations.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
)),
);
Ok(Some(if let Some(identifier) = identifier {
CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::Identifier(identifier.into())),
Box::new(expr),
)
} else {
expr
}))
}
CertificateProfile::DeveloperIdApplication => {
let team_id = cert.apple_team_id().ok_or_else(|| {
AppleCodesignError::PolicyFormulationError(format!(
"(deriving for {profile:?}) could not find team identifier in signing certificate"
))
})?;
let expr = CodeRequirementExpression::And(
// Chains to Apple root CA.
Box::new(CodeRequirementExpression::AnchorAppleGeneric),
Box::new(CodeRequirementExpression::And(
// Certificate issued by CA with Developer ID extension.
Box::new(CodeRequirementExpression::CertificateGeneric(
1,
CertificateAuthorityExtension::DeveloperId.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
Box::new(CodeRequirementExpression::And(
// A certificate entrusted with Developer ID Application signing rights.
Box::new(CodeRequirementExpression::CertificateGeneric(
0,
CodeSigningCertificateExtension::DeveloperIdApplication.as_oid(),
CodeRequirementMatchExpression::Exists,
)),
// Signed by this team ID.
Box::new(CodeRequirementExpression::CertificateField(
0,
"subject.OU".to_string().into(),
CodeRequirementMatchExpression::Equal(team_id.into()),
)),
)),
)),
);
Ok(Some(if let Some(identifier) = identifier {
CodeRequirementExpression::And(
Box::new(CodeRequirementExpression::Identifier(identifier.into())),
Box::new(expr),
)
} else {
expr
}))
}
CertificateProfile::MacInstallerDistribution | CertificateProfile::DeveloperIdInstaller => {
Err(AppleCodesignError::PolicyFormulationError(format!(
"(deriving for {profile:?}) a designated requirement cannot be derived for installer code signing certificates; if you see this message you should probably be using a different code signing certificate for signing applications"
)))
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn get_policies() {
ExecutionPolicy::DeveloperIdSigned.to_bytes().unwrap();
ExecutionPolicy::DeveloperIdNotarizedExecutable
.to_bytes()
.unwrap();
ExecutionPolicy::DeveloperIdNotarizedInstaller
.to_bytes()
.unwrap();
}
}