| | 1 | = Windows Code Signing |
| | 2 | |
| | 3 | This is how we do automated code signing of Windows binaries using a !YubiKey. We started with: |
| | 4 | |
| | 5 | * Enterprise Contract with Sectigo, https://www.sectigo.com |
| | 6 | * !YubiKey 5 FIPS |
| | 7 | * !YubiKey Smart Card Minidriver (Windows) from https://www.yubico.com/support/download/smart-card-drivers-tools/ |
| | 8 | * yubio-piv-tool from https://developers.yubico.com/yubico-piv-tool/ |
| | 9 | * signtool from Microsoft Visual Studio |
| | 10 | * scsigntool from https://www.mgtek.com/smartcard |
| | 11 | |
| | 12 | Outline: |
| | 13 | |
| | 14 | * Used !YubiKey Authenticator application from Windows Store to set PIN, PUK, and Management Key |
| | 15 | * Install !YubiKey Smart Card Minidriver |
| | 16 | * Install yubio-piv-tool |
| | 17 | * Get email invitation to submit code signing CSR to Sectigo initiated by corporate IT |
| | 18 | * Submit CSR and !YubiKey attestation |
| | 19 | * Get email from Sectigo with link to certificate |
| | 20 | * Install certificate on !YubiKey |
| | 21 | * Sign code |
| | 22 | |
| | 23 | == Submit CSR |
| | 24 | |
| | 25 | The code signing slot is the Authentication slot `9A`. |
| | 26 | |
| | 27 | === Create key in slot |
| | 28 | |
| | 29 | `yubico-piv-tool --slot=$SLOT --action=generate --touch-policy=never --algorithm=$ALGORITHM --key --output=public.key` |
| | 30 | |
| | 31 | === Use public key to create CSR |
| | 32 | |
| | 33 | `yubico-piv-tool --slot=$SLOT --action=verify-pin --action=request-certificate --subject='/CN=Sectigo/' --input=public.key --output=csr.txt` |
| | 34 | |
| | 35 | Upload `csr.txt` for the CSR |
| | 36 | |
| | 37 | === !YubiKey attestation |
| | 38 | |
| | 39 | ==== Get attestation |
| | 40 | |
| | 41 | `yubico-piv-tool --slot=$SLOT --action=attest --output=attest.crt` |
| | 42 | |
| | 43 | ==== Get !YubiKey's intermediate certificates |
| | 44 | |
| | 45 | `yubico-piv-tool --action=read-certificate --slot=f9 --output=intermediateCA.crt` |
| | 46 | |
| | 47 | ==== Combine and encode in base64 |
| | 48 | {{{ |
| | 49 | cat attest.crt intermediateCA.crt > attestation.pem |
| | 50 | base64 < attestation.pem > attestation.pem.b64 |
| | 51 | }}} |
| | 52 | |
| | 53 | |
| | 54 | Attestation is in `attestation.pem.b64`. Copy the contents and paste in the Key Attestation field. Make sure `YubiKey` is the HSM type. |
| | 55 | |
| | 56 | == Install Certificate |
| | 57 | |
| | 58 | Use "Certificate only, PEM encoded" certificate. Downloads as `$CERT_BASE_cert.cer`. |
| | 59 | |
| | 60 | === Install in Windows User Certificate manager |
| | 61 | |
| | 62 | Open `$CERT_BASE_cert.cer` in file explorer, then click on `Install Certificate...`. Choose `Current User`. Place in `Personal` Certificate Store. |
| | 63 | |
| | 64 | === Install on !YubiKey |
| | 65 | |
| | 66 | `yubico-piv-tool --action=import-certificate --slot=$SLOT --key --pin-policy=once --touch-policy=never --input=$CERT_BASE_cert.cer` |
| | 67 | |
| | 68 | == Install intermediate certificates onto !YubiKey |
| | 69 | |
| | 70 | Use "Root/Intermediate(s) only, PEM encoded" certificates. Downloads as `CERT_BASE_interm.cer`. |
| | 71 | |
| | 72 | {{{ |
| | 73 | interm=$CERT_BASE_interm.cer |
| | 74 | let slot=$((0x82)) |
| | 75 | let max_slot=$((0x95)) |
| | 76 | while openssl x509 -out cert.pem |
| | 77 | do |
| | 78 | if [ $slot -ge $max_slot ] |
| | 79 | then |
| | 80 | echo "too many intermediate certificates" |
| | 81 | exit 1 |
| | 82 | fi |
| | 83 | hex_slot=$(printf "%x" $slot) |
| | 84 | echo "Import slot $hex_slot: $(openssl x509 -in cert.pem -noout -subject | sed 's/.*CN = //')" |
| | 85 | yubico-piv-tool --action=import-certificate --slot=$hex_slot --key --input=cert.pem |
| | 86 | slot=$((slot + 1)) |
| | 87 | done < $interm |
| | 88 | }}} |
| | 89 | |
| | 90 | == Sign Code |
| | 91 | |
| | 92 | Microsoft's `signtool` doesn't have a way to provide the PIN needed use the private key. So use MGTEK's `scsigntool` to wrap signtool. In our case: |
| | 93 | {{{ |
| | 94 | scsigntool.exe -pin CODESIGN sign /v /a /sha1 CERT_SHA /fd sha256 /t http://timestamp.sectigo.com/authenticode /d "Application Name" app-installer.exe |
| | 95 | }}} |