Source file codesign.ml

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
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2025 OCamlPro                                             *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

type signing_identity =
  | AdHoc
  | DeveloperID of string

type sign_options = {
  force : bool;
  timestamp : bool;
  entitlements : string option;
}

let default_sign_options = {
  force = false;
  timestamp = false;
  entitlements = None;
}

let sign_binary ?(options=default_sign_options) ~identity binary =
  try
    let identity_str = match identity with
      | AdHoc -> "-"
      | DeveloperID cert_name -> cert_name
    in
    let args : System.codesign_args = {
      binary;
      identity = identity_str;
      force = options.force;
      timestamp = options.timestamp;
      entitlements = options.entitlements;
    } in
    System.call_unit System.Codesign args
  with System.System_error e ->
    OpamConsole.warning "codesign failed: %s" e

let sign_binary_adhoc ?(force=true) binary =
  let options = { default_sign_options with force } in
  sign_binary ~options ~identity:AdHoc binary

let sign_binary_with_dev_id ?(force=true) ?(timestamp=true) ~cert_name binary =
  let options = { default_sign_options with force; timestamp } in
  sign_binary ~options ~identity:(DeveloperID cert_name) binary

let verify_signature binary =
  try
    let args : System.codesign_verify_args = {
      binary;
      verbose = false;
    } in
    System.call_unit System.CodesignVerify args;
    true
  with System.System_error _ ->
    false