-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (132 loc) · 3.07 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package main
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io"
"os"
"github.com/RealImage/bifrost"
"github.com/google/uuid"
)
const (
usageHeader = `bfid prints the UUID of a Bifrost public key, private key, or certificate.
Usage:
bfid [-v] [-ns=UUID] FILE
bfid [-v] [-ns=UUID] < FILE
`
usageTrailer = `
The environment variable BF_NS takes precedence over the -ns flag.
`
)
var (
namespace uuid.UUID
verbose bool
filename string = "-"
)
func init() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, usageHeader)
flag.PrintDefaults()
fmt.Fprint(os.Stderr, usageTrailer)
}
var ns string
flag.StringVar(&ns, "ns", bifrost.Namespace.String(),
"Bifrost Identity Namespace")
flag.BoolVar(&verbose, "v", false, "Verbose output")
flag.Parse()
// BF_NS env var overrides the flag.
if n, ok := os.LookupEnv("BF_NS"); ok {
ns = n
}
// 0 or empty string means no namespace.
if ns == "" || ns == "0" {
namespace = uuid.Nil
} else {
var err error
namespace, err = uuid.Parse(ns)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
if flag.NArg() > 1 {
fmt.Fprintf(os.Stderr, "too many arguments\n\n")
flag.Usage()
os.Exit(1)
}
if flag.Arg(0) != "" {
filename = flag.Arg(0)
}
}
func main() {
// Read the input file or stdin.
var data []byte
var err error
if filename == "-" {
data, err = io.ReadAll(os.Stdin)
} else {
data, err = os.ReadFile(filename)
}
if err != nil {
fmt.Fprintf(os.Stderr, "error reading input: %s\n", err)
os.Exit(1)
}
block, _ := pem.Decode(data)
if block == nil {
fmt.Fprint(os.Stderr, "no pem data found in input file\n")
os.Exit(1)
}
// Parse the key or certificate.
var pubkey *ecdsa.PublicKey
var unknownBlock bool
switch block.Type {
case "PRIVATE KEY":
var key any
if key, err = x509.ParsePKCS8PrivateKey(block.Bytes); err == nil {
if eckey, ok := key.(*ecdsa.PrivateKey); ok {
pubkey = &eckey.PublicKey
}
}
case "EC PRIVATE KEY":
var key *ecdsa.PrivateKey
if key, err = x509.ParseECPrivateKey(block.Bytes); err == nil {
pubkey = &key.PublicKey
}
case "PUBLIC KEY":
var key any
if key, err = x509.ParsePKIXPublicKey(block.Bytes); err == nil {
if eckey, ok := key.(*ecdsa.PublicKey); ok {
pubkey = eckey
}
}
case "CERTIFICATE":
var cert *x509.Certificate
if _, cert, err = bifrost.ParseCertificate(block.Bytes); err == nil {
if eckey, ok := cert.PublicKey.(*ecdsa.PublicKey); ok {
pubkey = eckey
}
}
default:
err = fmt.Errorf("unexpected block type: %s", block.Type)
unknownBlock = true
}
if unknownBlock && pubkey == nil {
err = bifrost.ErrUnsupportedAlgorithm
}
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing key: %s\n", err)
os.Exit(1)
}
// Print the UUID.
id := bifrost.UUID(namespace, pubkey)
if verbose {
fmt.Printf("namespace:\t%s\nuuid:\t\t%s\n", namespace, id)
} else {
fmt.Println(id)
}
}