mobileconfig.go RawHistoryFolder
1package pki
2
3import (
4 "encoding/base64"
5 "fmt"
6 "html"
7 "strings"
8 "unicode"
9
10 "github.com/google/uuid"
11)
12
13// GenerateMobileconfig generates an iOS configuration profile containing
14// the root CA certificate for one-tap trust installation.
15func GenerateMobileconfig(rootDER []byte, rootCN string) []byte {
16 b64Cert := base64.StdEncoding.EncodeToString(rootDER)
17 profileUUID := uuid.New().String()
18 certUUID := uuid.New().String()
19
20 safeCN := html.EscapeString(rootCN)
21 identifierCN := payloadIdentifierComponent(rootCN)
22 profile := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
23<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
24<plist version="1.0">
25<dict>
26 <key>PayloadContent</key>
27 <array>
28 <dict>
29 <key>PayloadCertificateFileName</key>
30 <string>gordon-ca.crt</string>
31 <key>PayloadContent</key>
32 <data>%s</data>
33 <key>PayloadDescription</key>
34 <string>Installs the Gordon internal CA root certificate</string>
35 <key>PayloadDisplayName</key>
36 <string>%s</string>
37 <key>PayloadIdentifier</key>
38 <string>dev.gordon.ca.%s</string>
39 <key>PayloadType</key>
40 <string>com.apple.security.root</string>
41 <key>PayloadUUID</key>
42 <string>%s</string>
43 <key>PayloadVersion</key>
44 <integer>1</integer>
45 </dict>
46 </array>
47 <key>PayloadDescription</key>
48 <string>Trust Gordon's internal Certificate Authority for HTTPS access</string>
49 <key>PayloadDisplayName</key>
50 <string>Gordon CA Trust</string>
51 <key>PayloadIdentifier</key>
52 <string>dev.gordon.ca-profile</string>
53 <key>PayloadType</key>
54 <string>Configuration</string>
55 <key>PayloadUUID</key>
56 <string>%s</string>
57 <key>PayloadVersion</key>
58 <integer>1</integer>
59 </dict>
60</plist>`, b64Cert, safeCN, identifierCN, certUUID, profileUUID)
61
62 return []byte(profile)
63}
64
65func payloadIdentifierComponent(rootCN string) string {
66 var b strings.Builder
67 lastHyphen := false
68
69 for _, r := range rootCN {
70 r = unicode.ToLower(r)
71 switch {
72 case (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '.':
73 b.WriteRune(r)
74 lastHyphen = false
75 case r == '-':
76 if !lastHyphen {
77 b.WriteRune(r)
78 lastHyphen = true
79 }
80 default:
81 if !lastHyphen {
82 b.WriteRune('-')
83 lastHyphen = true
84 }
85 }
86 }
87
88 identifier := strings.Trim(b.String(), "-.")
89 if identifier == "" {
90 return "gordon-ca"
91 }
92
93 return identifier
94}