-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
120 lines (96 loc) · 2.96 KB
/
main.go
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
// Copyright 2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/pflag"
)
const DefaultContentType = "application/rim+cbor"
var outfile *string = pflag.StringP("out", "o", "",
"Output will be written to this file. If not specified, defaults " +
"to the same path as the input with the extension changed to .cbor.")
var writeToStdout *bool = pflag.BoolP("stdout", "O", false,
"Write to standard output instead of a file.")
var signingKey *string = pflag.StringP("signing-key", "s", "",
"Path to a signing key in JWK format. If this is specified, a COSE " +
"Sign1Message will be generated with the encoded input as the payload")
var contentType *string = pflag.StringP("contentType", "c", DefaultContentType,
"When signing with -s/--signing-key, this will be used as the value " +
"of the content type COSE header.")
var metafile *string = pflag.StringP("meta", "m", "",
"Path to YAML file that will be encoded and used as the meta header in the " +
"COSE Sign1Message (when -s/--signing-key is also specified)")
func validateArgs() {
if pflag.NArg() != 1 {
log.Fatalf("error: must specify exactly one positional argument")
}
if *outfile != "" && *writeToStdout {
log.Fatalf("error: -o/--out and -O/--stdout cannot be both specified")
}
if *signingKey == "" { // not gonna be signing
if *contentType != DefaultContentType {
log.Fatalf("error: -c/--content-type should only be used with -s/--signing-key")
}
if *metafile != "" {
log.Fatalf("error: -m/--meta should only be used with -s/--signing-key")
}
} else { // gonna be signing
if *metafile == "" {
log.Print("warning: generating COSE Sign1Message without -m/--meta")
}
}
}
func encodeFileToCBOR(path string) ([]byte, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error: %w", err)
}
return yaml2cbor(data)
}
func main() {
pflag.Parse()
validateArgs()
inFile := pflag.Arg(0)
out, err := encodeFileToCBOR(inFile)
if err != nil {
log.Fatalf("error: %v", err)
}
outPath := *outfile
if outPath == "" {
outPath = strings.TrimSuffix(inFile, filepath.Ext(inFile)) + ".cbor"
}
if *signingKey != "" {
keyData, err := os.ReadFile(*signingKey)
if err != nil {
log.Fatalf("error: %v", err)
}
signer, err := coseSignerFromJWK(keyData)
if err != nil {
log.Fatalf("error: %v", err)
}
var meta []byte
if *metafile != "" {
meta, err = encodeFileToCBOR(*metafile)
if err != nil {
log.Fatalf("error: %v", err)
}
}
kid, err := getKidFromJWK(keyData)
if err != nil {
log.Fatalf("error: %v", err)
}
out, err = sign(out, meta, *contentType, kid, signer)
}
if outPath == "-" || *writeToStdout {
fmt.Print(string(out))
} else {
err = os.WriteFile(outPath, out, 0666)
if err != nil {
log.Fatalf("error: %v", err)
}
}
}