-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy.go
More file actions
463 lines (423 loc) · 14.2 KB
/
Copy pathpolicy.go
File metadata and controls
463 lines (423 loc) · 14.2 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// SPDX-FileCopyrightText: 2026 Jijie Wei (varwof)
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
"fmt"
"math/big"
"os"
"path/filepath"
"time"
)
var (
pkcs7OIDData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}
pkcs7OIDSignedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}
oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
oidECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
oidRSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
oidEd25519 = asn1.ObjectIdentifier{1, 3, 101, 112}
oidContentType = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 3}
oidMessageDigest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 4}
)
type pkcs7AlgorithmIdentifier struct {
Algorithm asn1.ObjectIdentifier
Parameters asn1.RawValue `asn1:"optional"`
}
type pkcs7IssuerAndSerial struct {
Issuer asn1.RawValue
SerialNumber *big.Int
}
type pkcs7Attribute struct {
Type asn1.ObjectIdentifier
Values []asn1.RawValue `asn1:"set"`
}
type pkcs7SignerInfo struct {
Version int
IssuerAndSerial pkcs7IssuerAndSerial
DigestAlgorithm pkcs7AlgorithmIdentifier
SignedAttributes []pkcs7Attribute `asn1:"optional,implicit,tag:0"`
SignatureAlgorithm pkcs7AlgorithmIdentifier
Signature []byte
}
type pkcs7EncapsulatedContentInfo struct {
ContentType asn1.ObjectIdentifier
Content asn1.RawValue `asn1:"optional"`
}
type pkcs7SignedData struct {
Version int
DigestAlgorithms []pkcs7AlgorithmIdentifier `asn1:"set"`
EncapContentInfo pkcs7EncapsulatedContentInfo
Certificates []asn1.RawValue `asn1:"optional,implicit,tag:0"`
SignerInfos []pkcs7SignerInfo `asn1:"set"`
}
type pkcs7ContentInfo struct {
ContentType asn1.ObjectIdentifier
Content asn1.RawValue `asn1:"explicit,tag:0"`
}
// buildPolicySignature creates a PKCS#7 detached signature (eContentType=OIDData, SHA-256).
// The signer certificate is embedded in the signature (for the verifier to extract).
// SignedAttributes contain contentType + messageDigest.
func buildPolicySignature(data []byte, cert *x509.Certificate, signer crypto.Signer) ([]byte, error) {
hash := sha256.New()
hash.Write(data)
digest := hash.Sum(nil)
signedAttrs := []pkcs7Attribute{
{
Type: oidContentType,
Values: []asn1.RawValue{
{FullBytes: mustMarshal(pkcs7OIDData)},
},
},
{
Type: oidMessageDigest,
Values: []asn1.RawValue{
{Class: 0, Tag: asn1.TagOctetString, Bytes: digest},
},
},
}
attrDER, err := marshalSignedAttrs(signedAttrs)
if err != nil {
return nil, err
}
var signature []byte
var sigAlg pkcs7AlgorithmIdentifier
hashed := sha256.Sum256(attrDER)
switch signer.Public().(type) {
case *ecdsa.PublicKey:
// crypto.Signer.Sign on an ECDSA key returns an ASN.1 DER signature
// (equivalent to ecdsa.SignASN1), which is what the PKCS#7 verifier
// expects. Using the interface method keeps wrapped signers working.
signature, err = signer.Sign(rand.Reader, hashed[:], crypto.SHA256)
if err != nil {
return nil, err
}
sigAlg = pkcs7AlgorithmIdentifier{Algorithm: oidECDSAWithSHA256, Parameters: asn1.RawValue{Tag: 5}}
case *rsa.PublicKey:
signature, err = signer.Sign(rand.Reader, hashed[:], crypto.SHA256)
if err != nil {
return nil, err
}
sigAlg = pkcs7AlgorithmIdentifier{Algorithm: oidRSAWithSHA256, Parameters: asn1.RawValue{Tag: 5}}
case ed25519.PublicKey:
// Ed25519 ignores opts and signs the message directly (pure Ed25519).
signature, err = signer.Sign(rand.Reader, attrDER, crypto.Hash(0))
if err != nil {
return nil, err
}
sigAlg = pkcs7AlgorithmIdentifier{Algorithm: oidEd25519}
default:
return nil, fmt.Errorf("unsupported signer public key type %T", signer.Public())
}
sd := pkcs7SignedData{
Version: 1,
DigestAlgorithms: []pkcs7AlgorithmIdentifier{
{Algorithm: oidSHA256, Parameters: asn1.RawValue{Tag: 5}},
},
EncapContentInfo: pkcs7EncapsulatedContentInfo{
ContentType: pkcs7OIDData,
},
Certificates: []asn1.RawValue{
{FullBytes: cert.Raw},
},
SignerInfos: []pkcs7SignerInfo{
{
Version: 1,
IssuerAndSerial: pkcs7IssuerAndSerial{
Issuer: asn1.RawValue{FullBytes: cert.RawIssuer},
SerialNumber: cert.SerialNumber,
},
DigestAlgorithm: pkcs7AlgorithmIdentifier{Algorithm: oidSHA256, Parameters: asn1.RawValue{Tag: 5}},
SignedAttributes: signedAttrs,
SignatureAlgorithm: sigAlg,
Signature: signature,
},
},
}
sdDER, err := asn1.Marshal(sd)
if err != nil {
return nil, fmt.Errorf("marshal signed data: %w", err)
}
ci := pkcs7ContentInfo{
ContentType: pkcs7OIDSignedData,
Content: asn1.RawValue{Class: 2, Tag: 0, IsCompound: true, Bytes: sdDER},
}
return asn1.Marshal(ci)
}
// marshalSignedAttrs encodes SignedAttributes as the content bytes of [0] IMPLICIT SET
// (the signature input = DER after stripping the SET header).
func marshalSignedAttrs(attrs []pkcs7Attribute) ([]byte, error) {
wrapped, err := asn1.Marshal(struct {
Attrs []pkcs7Attribute `asn1:"set"`
}{Attrs: attrs})
if err != nil {
return nil, err
}
skip := 2
if len(wrapped) > 1 && wrapped[1]&0x80 != 0 {
skip = 2 + int(wrapped[1]&0x7f)
}
return wrapped[skip:], nil
}
func mustMarshal(v any) []byte {
b, err := asn1.Marshal(v)
if err != nil {
panic(err)
}
return b
}
// verifyPolicySignature verifies a PKCS#7 detached signature and returns the
// signer certificate. It is the local self-verify entry point: it selects the
// signer by Issuer+Serial, checks validity and rejects self-signed/CA signer
// certs, but does NOT establish a trust chain. Callers that verify a policy
// file fetched from an untrusted source MUST use verifyPolicySignatureWithRoots.
func verifyPolicySignature(sigDER, data []byte) (*x509.Certificate, error) {
return verifyPolicySignatureWithTrust(sigDER, data, nil, nil)
}
// verifyPolicySignatureWithRoots verifies the policy signature AND establishes
// that the signer certificate chains to a trusted root pool. A nil pool is
// fail-closed (refuse to verify) — prevents an attacker's self-signed cert from
// passing as a policy signer (CL1).
func verifyPolicySignatureWithRoots(sigDER, data []byte, roots *x509.CertPool) (*x509.Certificate, error) {
if roots == nil {
return nil, fmt.Errorf("policy signer trust roots not configured — refusing to verify (fail-closed)")
}
return verifyPolicySignatureWithTrust(sigDER, data, roots, nil)
}
// verifyPolicySignatureWithTrust is the shared implementation. When roots is
// non-nil the signer must chain to roots. When expected is non-nil the embedded
// signer must also match expected (issuer, serial and SPKI) — used by the local
// self-verify path so the embedded cert cannot be swapped for an attacker cert
// with the same serial number.
func verifyPolicySignatureWithTrust(sigDER, data []byte, roots *x509.CertPool, expected *x509.Certificate) (*x509.Certificate, error) {
var ci pkcs7ContentInfo
if _, err := asn1.Unmarshal(sigDER, &ci); err != nil {
return nil, fmt.Errorf("unmarshal ContentInfo: %w", err)
}
var sd pkcs7SignedData
if _, err := asn1.Unmarshal(ci.Content.Bytes, &sd); err != nil {
return nil, fmt.Errorf("unmarshal SignedData: %w", err)
}
if len(sd.SignerInfos) == 0 {
return nil, fmt.Errorf("no signer infos")
}
si := sd.SignerInfos[0]
// Select the signer certificate by Issuer AND SerialNumber (CL1: matching by
// serial only let an attacker's self-signed cert with the same serial win).
var signerCert *x509.Certificate
for _, cr := range sd.Certificates {
cert, err := x509.ParseCertificate(cr.FullBytes)
if err != nil {
continue
}
if cert.SerialNumber.Cmp(si.IssuerAndSerial.SerialNumber) != 0 {
continue
}
if !bytes.Equal(cert.RawIssuer, si.IssuerAndSerial.Issuer.FullBytes) {
continue
}
signerCert = cert
break
}
if signerCert == nil {
return nil, fmt.Errorf("no signer certificate matching issuer and serial")
}
// Reject CA and self-signed signer certificates outright (CL1: an embedded
// self-signed root must never be accepted as a policy signer).
if signerCert.IsCA {
return nil, fmt.Errorf("signer certificate is a CA (subject=%s)", signerCert.Subject.String())
}
if bytes.Equal(signerCert.RawIssuer, signerCert.RawSubject) {
return nil, fmt.Errorf("signer certificate is self-signed (subject=%s)", signerCert.Subject.String())
}
// Validity window check.
now := time.Now()
if now.Before(signerCert.NotBefore) || now.After(signerCert.NotAfter) {
return nil, fmt.Errorf("signer certificate expired or not yet valid (subject=%s, validity %s→%s)",
signerCert.Subject.String(), signerCert.NotBefore.Format(time.RFC3339), signerCert.NotAfter.Format(time.RFC3339))
}
// Optionally bind to an expected certificate (self-verify path).
if expected != nil {
if expected.SerialNumber.Cmp(signerCert.SerialNumber) != 0 {
return nil, fmt.Errorf("embedded signer serial does not match expected cert")
}
expPKI, err := x509.MarshalPKIXPublicKey(expected.PublicKey)
if err != nil {
return nil, err
}
sigPKI, err := x509.MarshalPKIXPublicKey(signerCert.PublicKey)
if err != nil {
return nil, err
}
if !bytes.Equal(expPKI, sigPKI) {
return nil, fmt.Errorf("embedded signer public key does not match expected cert")
}
}
// Compute content digest
hash := sha256.New()
hash.Write(data)
contentDigest := hash.Sum(nil)
// Re-encode signedAttrs as DER (SET OF)
attrDER, err := asn1.Marshal(struct {
Attrs []pkcs7Attribute `asn1:"set"`
}{Attrs: si.SignedAttributes})
if err != nil {
return nil, err
}
// Strip SET tag header (2 bytes) to get [0] IMPLICIT content
skip := 2
if len(attrDER) > 1 && attrDER[1]&0x80 != 0 {
skip = 2 + int(attrDER[1]&0x7f)
}
signedAttrContent := attrDER[skip:]
// Validate contentType attribute (CL1: previously never checked).
contentTypeFound := false
for _, a := range si.SignedAttributes {
if a.Type.Equal(oidContentType) && len(a.Values) > 0 {
var oid asn1.ObjectIdentifier
if rest, err := asn1.Unmarshal(a.Values[0].FullBytes, &oid); err == nil && len(rest) == 0 {
if oid.Equal(pkcs7OIDData) {
contentTypeFound = true
}
}
}
}
if !contentTypeFound {
return nil, fmt.Errorf("signed content type attribute missing or not data (OIDData)")
}
// Verify messageDigest attribute
found := false
for _, a := range si.SignedAttributes {
if a.Type.Equal(oidMessageDigest) && len(a.Values) > 0 {
if !bytes.Equal(a.Values[0].Bytes, contentDigest) {
return nil, fmt.Errorf("content digest mismatch")
}
found = true
}
}
if !found {
return nil, fmt.Errorf("no messageDigest attribute")
}
switch k := signerCert.PublicKey.(type) {
case *ecdsa.PublicKey:
hashed := sha256.Sum256(signedAttrContent)
var sig struct{ R, S *big.Int }
if _, err := asn1.Unmarshal(si.Signature, &sig); err != nil {
return nil, err
}
if !ecdsa.Verify(k, hashed[:], sig.R, sig.S) {
return nil, fmt.Errorf("ECDSA signature mismatch")
}
case *rsa.PublicKey:
hashed := sha256.Sum256(signedAttrContent)
if err := rsa.VerifyPKCS1v15(k, crypto.SHA256, hashed[:], si.Signature); err != nil {
return nil, fmt.Errorf("RSA signature mismatch: %w", err)
}
case ed25519.PublicKey:
if !ed25519.Verify(k, signedAttrContent, si.Signature) {
return nil, fmt.Errorf("Ed25519 signature mismatch")
}
default:
return nil, fmt.Errorf("unsupported public key type %T", k)
}
// Chain verification against trusted roots (CL1).
if roots != nil {
if _, err := signerCert.Verify(x509.VerifyOptions{
Roots: roots,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
}); err != nil {
return nil, fmt.Errorf("policy signer cert chain not trusted: %w", err)
}
}
return signerCert, nil
}
// policySignerHasAdminOU checks if the certificate OU contains admin (compatible with admin and gateway:admin).
func policySignerHasAdminOU(cert *x509.Certificate) bool {
for _, ou := range cert.Subject.OrganizationalUnit {
if ou == "admin" || ou == "gateway:admin" {
return true
}
}
return false
}
// cmdPolicySign signs a policy file with an admin certificate using PKCS#7 detached signature.
func cmdPolicySign(args map[string]string) {
file := args["--file"]
certPath := args["--cert"]
keyPath := args["--key"]
if file == "" || certPath == "" || keyPath == "" {
fmt.Fprintln(os.Stderr, "Error: --file, --cert and --key are required")
os.Exit(1)
}
data, err := os.ReadFile(filepath.Clean(file))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: read %s: %v\n", file, err)
os.Exit(1)
}
certPEM, err := os.ReadFile(filepath.Clean(certPath))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: read cert: %v\n", err)
os.Exit(1)
}
block, _ := pem.Decode(certPEM)
if block == nil {
fmt.Fprintf(os.Stderr, "Error: %s: not a PEM certificate\n", certPath)
os.Exit(1)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: parse cert: %v\n", err)
os.Exit(1)
}
keyData, err := os.ReadFile(filepath.Clean(keyPath))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: read key: %v\n", err)
os.Exit(1)
}
var signer crypto.Signer
if isEncryptedPEM(keyData) {
pw := os.Getenv("PKI_KEY_PASSWORD")
signer, err = decryptPrivateKeyPEM(keyData, pw)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: decrypt key: %v\n", err)
os.Exit(1)
}
} else {
signer, err = parsePrivateKeyPEM(keyData)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: parse key: %v\n", err)
os.Exit(1)
}
}
if !policySignerHasAdminOU(cert) {
fmt.Fprintf(os.Stderr, "Error: signer cert must carry admin OU (got %s)\n", cert.Subject.String())
os.Exit(1)
}
sig, err := buildPolicySignature(data, cert, signer)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: build signature: %v\n", err)
os.Exit(1)
}
out := args["--out"]
if out == "" {
out = file + ".sig"
}
if err := os.WriteFile(filepath.Clean(out), sig, 0600); err != nil {
fmt.Fprintf(os.Stderr, "Error: write %s: %v\n", out, err)
os.Exit(1)
}
if _, err := verifyPolicySignatureWithTrust(sig, data, nil, cert); err != nil {
fmt.Fprintf(os.Stderr, "Error: self-verify failed (do not deploy): %v\n", err)
os.Exit(1)
}
fmt.Printf("policy signed: %s -> %s (signer=%s, serial=%s)\n",
file, out, cert.Subject.String(), cert.SerialNumber.String())
}