[docs] Fix build-docs.sh
[llvm-project.git] / lldb / scripts / macos-setup-codesign.sh
blob6e8ad768d5fe4f6b2f6ac8cde1c40364253a8bd7
1 #!/bin/bash
3 CERT="lldb_codesign"
5 function error() {
6 echo error: "$@"
7 exit 1
10 function cleanup {
11 # Remove generated files
12 rm -f "$TMPDIR/$CERT.tmpl" "$TMPDIR/$CERT.cer" "$TMPDIR/$CERT.key" > /dev/null 2>&1
15 trap cleanup EXIT
17 # Check if the certificate is already present in the system keychain
18 security find-certificate -Z -p -c "$CERT" /Library/Keychains/System.keychain > /dev/null 2>&1
19 if [ $? -eq 0 ]; then
20 echo Certificate has already been generated and installed
21 exit 0
24 # Create the certificate template
25 cat <<EOF >$TMPDIR/$CERT.tmpl
26 [ req ]
27 default_bits = 2048 # RSA key size
28 encrypt_key = no # Protect private key
29 default_md = sha512 # MD to use
30 prompt = no # Prompt for DN
31 distinguished_name = codesign_dn # DN template
32 [ codesign_dn ]
33 commonName = "$CERT"
34 [ codesign_reqext ]
35 keyUsage = critical,digitalSignature
36 extendedKeyUsage = critical,codeSigning
37 EOF
39 echo Generating and installing lldb_codesign certificate
41 # Generate a new certificate
42 openssl req -new -newkey rsa:2048 -x509 -days 3650 -nodes -config "$TMPDIR/$CERT.tmpl" -extensions codesign_reqext -batch -out "$TMPDIR/$CERT.cer" -keyout "$TMPDIR/$CERT.key" > /dev/null 2>&1
43 [ $? -eq 0 ] || error Something went wrong when generating the certificate
45 # Install the certificate in the system keychain
46 sudo security add-trusted-cert -d -r trustRoot -p codeSign -k /Library/Keychains/System.keychain "$TMPDIR/$CERT.cer" > /dev/null 2>&1
47 [ $? -eq 0 ] || error Something went wrong when installing the certificate
49 # Install the key for the certificate in the system keychain
50 sudo security import "$TMPDIR/$CERT.key" -A -k /Library/Keychains/System.keychain > /dev/null 2>&1
51 [ $? -eq 0 ] || error Something went wrong when installing the key
53 # Kill task_for_pid access control daemon
54 sudo pkill -f /usr/libexec/taskgated > /dev/null 2>&1
56 # Exit indicating the certificate is now generated and installed
57 exit 0