apple_codesign/
signing.rs

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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// 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/.

//! High level signing primitives.

use {
    crate::{
        bundle_signing::BundleSigner,
        dmg::DmgSigner,
        error::AppleCodesignError,
        macho_signing::{write_macho_file, MachOSigner},
        reader::PathType,
        signing_settings::{SettingsScope, SigningSettings},
    },
    apple_xar::{reader::XarReader, signing::XarSigner},
    log::{info, warn},
    std::{fs::File, path::Path},
};

/// An entity for performing signing that is able to handle all supported target types.
pub struct UnifiedSigner<'key> {
    settings: SigningSettings<'key>,
}

impl<'key> UnifiedSigner<'key> {
    /// Construct a new instance bound to a [SigningSettings].
    pub fn new(settings: SigningSettings<'key>) -> Self {
        Self { settings }
    }

    /// Signs `input_path` and writes the signed output to `output_path`.
    pub fn sign_path(
        &self,
        input_path: impl AsRef<Path>,
        output_path: impl AsRef<Path>,
    ) -> Result<(), AppleCodesignError> {
        let input_path = input_path.as_ref();

        match PathType::from_path(input_path)? {
            PathType::Bundle => self.sign_bundle(input_path, output_path),
            PathType::Dmg => self.sign_dmg(input_path, output_path),
            PathType::MachO => self.sign_macho(input_path, output_path),
            PathType::Xar => self.sign_xar(input_path, output_path),
            PathType::Zip | PathType::Other => Err(AppleCodesignError::UnrecognizedPathType),
        }
    }

    /// Sign a filesystem path in place.
    ///
    /// This is just a convenience wrapper for [Self::sign_path()] with the same path passed
    /// to both the input and output path.
    pub fn sign_path_in_place(&self, path: impl AsRef<Path>) -> Result<(), AppleCodesignError> {
        let path = path.as_ref();

        self.sign_path(path, path)
    }

    /// Sign a Mach-O binary.
    pub fn sign_macho(
        &self,
        input_path: impl AsRef<Path>,
        output_path: impl AsRef<Path>,
    ) -> Result<(), AppleCodesignError> {
        let input_path = input_path.as_ref();
        let output_path = output_path.as_ref();

        warn!("signing {} as a Mach-O binary", input_path.display());
        let macho_data = std::fs::read(input_path)?;

        let mut settings = self.settings.clone();

        settings.import_settings_from_macho(&macho_data)?;

        if settings.binary_identifier(SettingsScope::Main).is_none() {
            let identifier = path_identifier(input_path)?;

            warn!("setting binary identifier to {}", identifier);
            settings.set_binary_identifier(SettingsScope::Main, identifier);
        }

        warn!("parsing Mach-O");
        let signer = MachOSigner::new(&macho_data)?;

        let mut macho_data = vec![];
        signer.write_signed_binary(&settings, &mut macho_data)?;
        warn!("writing Mach-O to {}", output_path.display());
        write_macho_file(input_path, output_path, &macho_data)?;

        Ok(())
    }

    /// Sign a `.dmg` file.
    pub fn sign_dmg(
        &self,
        input_path: impl AsRef<Path>,
        output_path: impl AsRef<Path>,
    ) -> Result<(), AppleCodesignError> {
        let input_path = input_path.as_ref();
        let output_path = output_path.as_ref();

        warn!("signing {} as a DMG", input_path.display());

        // There must be a binary identifier on the DMG. So try to derive one
        // from the filename if one isn't present in the settings.
        let mut settings = self.settings.clone();

        if settings.binary_identifier(SettingsScope::Main).is_none() {
            let file_name = input_path
                .file_stem()
                .ok_or_else(|| {
                    AppleCodesignError::CliGeneralError("unable to resolve file name of DMG".into())
                })?
                .to_string_lossy();

            warn!(
                "setting binary identifier to {} (derived from file name)",
                file_name
            );
            settings.set_binary_identifier(SettingsScope::Main, file_name);
        }

        // The DMG signer signs in place because it needs a `File` handle. So if
        // the output path is different, copy the DMG first.

        // This is not robust same file detection.
        if input_path != output_path {
            info!(
                "copying {} to {} in preparation for signing",
                input_path.display(),
                output_path.display()
            );
            if let Some(parent) = output_path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            std::fs::copy(input_path, output_path)?;
        }

        let signer = DmgSigner::default();
        let mut fh = std::fs::File::options()
            .read(true)
            .write(true)
            .open(output_path)?;
        signer.sign_file(&settings, &mut fh)?;

        Ok(())
    }

    /// Sign a bundle.
    pub fn sign_bundle(
        &self,
        input_path: impl AsRef<Path>,
        output_path: impl AsRef<Path>,
    ) -> Result<(), AppleCodesignError> {
        let input_path = input_path.as_ref();
        warn!("signing bundle at {}", input_path.display());

        let mut signer = BundleSigner::new_from_path(input_path)?;
        signer.collect_nested_bundles()?;
        signer.write_signed_bundle(output_path, &self.settings)?;

        Ok(())
    }

    pub fn sign_xar(
        &self,
        input_path: impl AsRef<Path>,
        output_path: impl AsRef<Path>,
    ) -> Result<(), AppleCodesignError> {
        let input_path = input_path.as_ref();
        let output_path = output_path.as_ref();

        // The XAR can get corrupted if we sign into place. So we always go through a temporary
        // file. We could potentially avoid the overhead if we're not signing in place...

        let output_path_temp =
            output_path.with_file_name(if let Some(file_name) = output_path.file_name() {
                file_name.to_string_lossy().to_string() + ".tmp"
            } else {
                "xar.tmp".to_string()
            });

        warn!(
            "signing XAR pkg installer at {} to {}",
            input_path.display(),
            output_path_temp.display()
        );

        let (signing_key, signing_cert) = self
            .settings
            .signing_key()
            .ok_or(AppleCodesignError::XarNoAdhoc)?;

        {
            let reader = XarReader::new(File::open(input_path)?)?;
            let mut signer = XarSigner::new(reader);

            let mut fh = File::create(&output_path_temp)?;
            signer.sign(
                &mut fh,
                signing_key,
                signing_cert,
                self.settings.time_stamp_url(),
                self.settings.certificate_chain().iter().cloned(),
            )?;
        }

        if output_path.exists() {
            warn!("removing existing {}", output_path.display());
            std::fs::remove_file(output_path)?;
        }

        warn!(
            "renaming {} -> {}",
            output_path_temp.display(),
            output_path.display()
        );
        std::fs::rename(&output_path_temp, output_path)?;

        Ok(())
    }
}

pub fn path_identifier(path: impl AsRef<Path>) -> Result<String, AppleCodesignError> {
    let path = path.as_ref();

    // We only care about the file name.
    let file_name = path
        .file_name()
        .ok_or_else(|| {
            AppleCodesignError::PathIdentifier(format!("path {} lacks a file name", path.display()))
        })?
        .to_string_lossy()
        .to_string();

    // Remove the final file extension unless it is numeric.
    let id = if let Some((prefix, extension)) = file_name.rsplit_once('.') {
        if extension.chars().all(|c| c.is_ascii_digit()) {
            file_name.as_str()
        } else {
            prefix
        }
    } else {
        file_name.as_str()
    };

    let is_digit_or_dot = |c: char| c == '.' || c.is_ascii_digit();

    // If begins with digit or dot, use as is, handling empty string special
    // case.
    let id = match id.chars().next() {
        Some(first) => {
            if is_digit_or_dot(first) {
                return Ok(id.to_string());
            } else {
                id
            }
        }
        None => {
            return Ok(id.to_string());
        }
    };

    // Strip all components having numeric *suffixes* except the first
    // one. This doesn't strip extension components but *suffixes*. So
    // e.g. libFoo1.2.3 -> libFoo1. Logically, we strip trailing digits
    // + dot after the first dot preceded by digits.

    let prefix = id.trim_end_matches(is_digit_or_dot);
    let stripped = &id[prefix.len()..];

    if stripped.is_empty() {
        Ok(id.to_string())
    } else {
        // If the next character is a dot, add it back in.
        let (prefix, stripped) = if matches!(stripped.chars().next(), Some('.')) {
            (&id[0..prefix.len() + 1], &stripped[1..])
        } else {
            (prefix, stripped)
        };

        // Add back in any leading digits.

        let id = prefix
            .chars()
            .chain(stripped.chars().take_while(|c| c.is_ascii_digit()))
            .collect::<String>();

        Ok(id)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn path_identifier_normalization() {
        assert_eq!(path_identifier("foo").unwrap(), "foo");
        assert_eq!(path_identifier("foo.dylib").unwrap(), "foo");
        assert_eq!(path_identifier("/etc/foo.dylib").unwrap(), "foo");
        assert_eq!(path_identifier("/etc/foo").unwrap(), "foo");

        // Starts with digit or dot is preserved module final extension.
        assert_eq!(path_identifier(".foo").unwrap(), "");
        assert_eq!(path_identifier("123").unwrap(), "123");
        assert_eq!(path_identifier(".foo.dylib").unwrap(), ".foo");
        assert_eq!(path_identifier("123.dylib").unwrap(), "123");
        assert_eq!(path_identifier("123.42").unwrap(), "123.42");

        // Digit final extension preserved.

        assert_eq!(path_identifier("foo1").unwrap(), "foo1");
        assert_eq!(path_identifier("foo1.dylib").unwrap(), "foo1");
        assert_eq!(path_identifier("foo1.2.dylib").unwrap(), "foo1");
        assert_eq!(path_identifier("foo1.2").unwrap(), "foo1");
        assert_eq!(path_identifier("foo1.2.3.4.dylib").unwrap(), "foo1");
        assert_eq!(path_identifier("foo.1").unwrap(), "foo.1");
        assert_eq!(path_identifier("foo.1.2.3").unwrap(), "foo.1");
        assert_eq!(path_identifier("foo.1.2.dylib").unwrap(), "foo.1");
        assert_eq!(path_identifier("foo.1.dylib").unwrap(), "foo.1");
    }
}