Debian Codegraph @

status.go

raw symbols SCIP
1 package main
2
3 import (
4 "crypto"
5 "crypto/x509"
6 "fmt"
7 "os"
8 "sync"
9 "time"
10
11 "golang.org/x/crypto/openpgp/packet"
12 "golang.org/x/crypto/openpgp/s2k"
13 )
14
15 // This file implements gnupg's "status protocol". When the --status-fd argument
16 // is passed, gpg will output machine-readable status updates to that fd.
17 // Details on the "protocol" can be found at https://git.io/vFFKC
18
19 type status string
20
21 const (
22 // BEGIN_SIGNING
23 // Mark the start of the actual signing process. This may be used as an
24 // indication that all requested secret keys are ready for use.
25 sBeginSigning status = "BEGIN_SIGNING"
26
27 // SIG_CREATED <type> <pk_algo> <hash_algo> <class> <timestamp> <keyfpr>
28 // A signature has been created using these parameters.
29 // Values for type <type> are:
30 // - D :: detached
31 // - C :: cleartext
32 // - S :: standard
33 // (only the first character should be checked)
34 //
35 // <class> are 2 hex digits with the OpenPGP signature class.
36 //
37 // Note, that TIMESTAMP may either be a number of seconds since Epoch
38 // or an ISO 8601 string which can be detected by the presence of the
39 // letter 'T'.
40 sSigCreated status = "SIG_CREATED"
41
42 // NEWSIG [<signers_uid>]
43 // Is issued right before a signature verification starts. This is
44 // useful to define a context for parsing ERROR status messages.
45 // arguments are currently defined. If SIGNERS_UID is given and is
46 // not "-" this is the percent escape value of the OpenPGP Signer's
47 // User ID signature sub-packet.
48 sNewSig status = "NEWSIG"
49
50 // GOODSIG <long_keyid_or_fpr> <username>
51 // The signature with the keyid is good. For each signature only one
52 // of the codes GOODSIG, BADSIG, EXPSIG, EXPKEYSIG, REVKEYSIG or
53 // ERRSIG will be emitted. In the past they were used as a marker
54 // for a new signature; new code should use the NEWSIG status
55 // instead. The username is the primary one encoded in UTF-8 and %XX
56 // escaped. The fingerprint may be used instead of the long keyid if
57 // it is available. This is the case with CMS and might eventually
58 // also be available for OpenPGP.
59 sGoodSig status = "GOODSIG"
60
61 // BADSIG <long_keyid_or_fpr> <username>
62 // The signature with the keyid has not been verified okay. The username is
63 // the primary one encoded in UTF-8 and %XX escaped. The fingerprint may be
64 // used instead of the long keyid if it is available. This is the case with
65 // CMS and might eventually also be available for OpenPGP.
66 sBadSig status = "BADSIG"
67
68 // ERRSIG <keyid> <pkalgo> <hashalgo> <sig_class> <time> <rc>
69 //
70 // It was not possible to check the signature. This may be caused by a
71 // missing public key or an unsupported algorithm. A RC of 4 indicates
72 // unknown algorithm, a 9 indicates a missing public key. The other fields
73 // give more information about this signature. sig_class is a 2 byte hex-
74 // value. The fingerprint may be used instead of the keyid if it is
75 // available. This is the case with gpgsm and might eventually also be
76 // available for OpenPGP.
77 //
78 // Note, that TIME may either be the number of seconds since Epoch or an ISO
79 // 8601 string. The latter can be detected by the presence of the letter
80 // ‘T’.
81 sErrSig status = "ERRSIG"
82
83 // TRUST_
84 // These are several similar status codes:
85 //
86 // - TRUST_UNDEFINED <error_token>
87 // - TRUST_NEVER <error_token>
88 // - TRUST_MARGINAL [0 [<validation_model>]]
89 // - TRUST_FULLY [0 [<validation_model>]]
90 // - TRUST_ULTIMATE [0 [<validation_model>]]
91 //
92 // For good signatures one of these status lines are emitted to
93 // indicate the validity of the key used to create the signature.
94 // The error token values are currently only emitted by gpgsm.
95 //
96 // VALIDATION_MODEL describes the algorithm used to check the
97 // validity of the key. The defaults are the standard Web of Trust
98 // model for gpg and the standard X.509 model for gpgsm. The
99 // defined values are
100 //
101 // - pgp :: The standard PGP WoT.
102 // - shell :: The standard X.509 model.
103 // - chain :: The chain model.
104 // - steed :: The STEED model.
105 // - tofu :: The TOFU model
106 //
107 // Note that the term =TRUST_= in the status names is used for
108 // historic reasons; we now speak of validity.
109 sTrustFully status = "TRUST_FULLY"
110 )
111
112 var (
113 _setupStatus sync.Once
114 statusFile *os.File
115 )
116
117 func setupStatus() {
118 _setupStatus.Do(func() {
119 if *statusFdOpt <= 0 {
120 return
121 }
122
123 const (
124 unixStdout = 1
125 unixStderr = 2
126 )
127
128 // Even though Windows uses different numbers, we always equate 1/2 with
129 // stdout/stderr because Git always passes `--status-fd=1`.
130 switch *statusFdOpt {
131 case unixStdout:
132 statusFile = os.Stdout
133 case unixStderr:
134 statusFile = os.Stderr
135 default:
136 // TODO: debugging output if this fails
137 statusFile = os.NewFile(uintptr(*statusFdOpt), "status")
138 }
139 })
140 }
141
142 func (s status) emitf(format string, args ...interface{}) {
143 setupStatus()
144
145 if statusFile == nil {
146 return
147 }
148
149 const prefix = "[GNUPG:] "
150 statusFile.WriteString(prefix)
151 statusFile.WriteString(string(s))
152 fmt.Fprintf(statusFile, " "+format+"\n", args...)
153 }
154
155 func (s status) emit() {
156 setupStatus()
157
158 if statusFile == nil {
159 return
160 }
161
162 const prefix = "[GNUPG:] "
163 statusFile.WriteString(prefix + string(s) + "\n")
164 }
165
166 func emitSigCreated(cert *x509.Certificate, isDetached bool) {
167 // SIG_CREATED arguments
168 var (
169 sigType string
170 pkAlgo, hashAlgo, sigClass byte
171 now int64
172 fpr string
173 )
174
175 if isDetached {
176 sigType = "D"
177 } else {
178 sigType = "S"
179 }
180
181 switch cert.SignatureAlgorithm {
182 case x509.SHA1WithRSA, x509.SHA256WithRSA, x509.SHA384WithRSA, x509.SHA512WithRSA:
183 pkAlgo = byte(packet.PubKeyAlgoRSA)
184 case x509.ECDSAWithSHA1, x509.ECDSAWithSHA256, x509.ECDSAWithSHA384, x509.ECDSAWithSHA512:
185 pkAlgo = byte(packet.PubKeyAlgoECDSA)
186 }
187
188 switch cert.SignatureAlgorithm {
189 case x509.SHA1WithRSA, x509.ECDSAWithSHA1:
190 hashAlgo, _ = s2k.HashToHashId(crypto.SHA1)
191 case x509.SHA256WithRSA, x509.ECDSAWithSHA256:
192 hashAlgo, _ = s2k.HashToHashId(crypto.SHA256)
193 case x509.SHA384WithRSA, x509.ECDSAWithSHA384:
194 hashAlgo, _ = s2k.HashToHashId(crypto.SHA384)
195 case x509.SHA512WithRSA, x509.ECDSAWithSHA512:
196 hashAlgo, _ = s2k.HashToHashId(crypto.SHA512)
197 }
198
199 // gpgsm seems to always use 0x00
200 sigClass = 0
201 now = time.Now().Unix()
202 fpr = certHexFingerprint(cert)
203
204 sSigCreated.emitf("%s %d %d %02x %d %s", sigType, pkAlgo, hashAlgo, sigClass, now, fpr)
205 }
206
207 func emitGoodSig(chains [][][]*x509.Certificate) {
208 cert := chains[0][0][0]
209 subj := cert.Subject.String()
210 fpr := certHexFingerprint(cert)
211
212 sGoodSig.emitf("%s %s", fpr, subj)
213 }
214
215 func emitBadSig(chains [][][]*x509.Certificate) {
216 cert := chains[0][0][0]
217 subj := cert.Subject.String
218 fpr := certHexFingerprint(cert)
219
220 sBadSig.emitf("%s %s", fpr, subj)
221 }
222
223 func emitTrustFully() {
224 sTrustFully.emitf("0 shell")
225 }