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
|
// Copyright 2018-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"crypto/rand"
"encoding/base32"
"encoding/base64"
"flag"
"fmt"
"log"
"os"
"runtime"
"strings"
"github.com/nats-io/nkeys"
)
// this will be set during compilation when a release is made on tools
var Version string
const defaultVanMax = 10_000_000
func usage() {
log.Fatalf(`Usage: nk [options]
-v Show version
-gen <type> Generate key for [user|account|server|cluster|operator|curve|x25519]
-sign <file> Sign <file> with -inkey <keyfile>
-verify <file> Verfify <file> with -inkey <keyfile> or -pubin <public> and -sigfile <file>
-inkey <file> Input key file (seed/private key)
-pubin <file> Public key file
-sigfile <file> Signature file
-pubout Output public key
-e Entropy file, e.g. /dev/urandom
-pre <vanity> Attempt to generate public key given prefix, e.g. nk -gen user -pre derek
-maxpre <N> Maximum attempts at generating the correct key prefix, default is 10,000,000
`)
}
type KeyPair interface {
Seed() ([]byte, error)
PublicKey() (string, error)
}
func main() {
var entropy = flag.String("e", "", "Entropy file, e.g. /dev/urandom")
var keyFile = flag.String("inkey", "", "Input key file (seed/private key)")
var pubFile = flag.String("pubin", "", "Public key file")
var signFile = flag.String("sign", "", "Sign <file> with -inkey <keyfile>")
var sigFile = flag.String("sigfile", "", "Signature file")
var verifyFile = flag.String("verify", "", "Verfify <file> with -inkey <keyfile> or -pubin <public> and -sigfile <file>")
var keyType = flag.String("gen", "", "Generate key for <type>, e.g. nk -gen user")
var pubout = flag.Bool("pubout", false, "Output public key")
var version = flag.Bool("v", false, "Show version")
var vanPre = flag.String("pre", "", "Attempt to generate public key given prefix, e.g. nk -gen user -pre derek")
var vanMax = flag.Int("maxpre", defaultVanMax, "Maximum attempts at generating the correct key prefix")
log.SetFlags(0)
log.SetOutput(os.Stdout)
flag.Usage = usage
flag.Parse()
if *version {
fmt.Printf("nk version %s\n", Version)
return
}
// Create Key
if *keyType != "" {
var kp KeyPair
// Check to see if we are trying to do a vanity public key.
if *vanPre != "" {
kp = createVanityKey(*keyType, *vanPre, *entropy, *vanMax)
} else {
kp = genKeyPair(preForType(*keyType), *entropy)
}
seed, err := kp.Seed()
if err != nil {
log.Fatal(err)
}
log.Printf("%s", seed)
if *pubout || *vanPre != "" {
pub, _ := kp.PublicKey()
log.Printf("%s", pub)
}
return
}
if *entropy != "" {
log.Fatalf("Entropy file only used when creating keys with -gen")
}
// Sign
if *signFile != "" {
sign(*signFile, *keyFile)
return
}
// Verfify
if *verifyFile != "" {
verify(*verifyFile, *keyFile, *pubFile, *sigFile)
return
}
// Show public key from seed/private
if *keyFile != "" && *pubout {
printPublicFromSeed(*keyFile)
return
}
usage()
}
func printPublicFromSeed(keyFile string) {
seed := readKeyFile(keyFile)
kp, err := nkeys.FromSeed(seed)
if err != nil {
log.Fatal(err)
}
pub, _ := kp.PublicKey()
log.Printf("%s", pub)
}
func sign(fname, keyFile string) {
if keyFile == "" {
log.Fatalf("Sign requires a seed/private key via -inkey <file>")
}
seed := readKeyFile(keyFile)
kp, err := nkeys.FromSeed(seed)
if err != nil {
log.Fatal(err)
}
content, err := os.ReadFile(fname)
if err != nil {
log.Fatal(err)
}
sigraw, err := kp.Sign(content)
if err != nil {
log.Fatal(err)
}
log.Printf("%s", base64.RawURLEncoding.EncodeToString(sigraw))
}
func verify(fname, keyFile, pubFile, sigFile string) {
if keyFile == "" && pubFile == "" {
log.Fatalf("Verify requires a seed key via -inkey or a public key via -pubin")
}
if sigFile == "" {
log.Fatalf("Verify requires a signature via -sigfile")
}
var err error
var kp nkeys.KeyPair
if keyFile != "" {
seed := readKeyFile(keyFile)
kp, err = nkeys.FromSeed(seed)
} else {
// Public Key
public := readKeyFile(pubFile)
kp, err = nkeys.FromPublicKey(string(public))
}
if err != nil {
log.Fatal(err)
}
content, err := os.ReadFile(fname)
if err != nil {
log.Fatal(err)
}
sigEnc, err := os.ReadFile(sigFile)
if err != nil {
log.Fatal(err)
}
sig, err := base64.RawURLEncoding.DecodeString(string(sigEnc))
if err != nil {
log.Fatal(err)
}
if err := kp.Verify(content, sig); err != nil {
log.Fatal(err)
}
log.Printf("Verified OK")
}
func preForType(keyType string) nkeys.PrefixByte {
keyType = strings.ToLower(keyType)
switch keyType {
case "user":
return nkeys.PrefixByteUser
case "account":
return nkeys.PrefixByteAccount
case "server":
return nkeys.PrefixByteServer
case "cluster":
return nkeys.PrefixByteCluster
case "operator":
return nkeys.PrefixByteOperator
case "curve", "x25519":
return nkeys.PrefixByteCurve
default:
log.Fatalf("Usage: nk -gen [user|account|server|cluster|operator|curve|x25519]\n")
}
return nkeys.PrefixByte(0)
}
func genKeyPair(pre nkeys.PrefixByte, entropy string) KeyPair {
// See if we override entropy.
ef := rand.Reader
if entropy != "" {
r, err := os.Open(entropy)
if err != nil {
log.Fatal(err)
}
ef = r
}
var kp KeyPair
var err error
if pre == nkeys.PrefixByteCurve {
kp, err = nkeys.CreateCurveKeysWithRand(ef)
if err != nil {
log.Fatalf("Error creating %c: %v", pre, err)
}
} else {
kp, err = nkeys.CreatePairWithRand(pre, ef)
if err != nil {
log.Fatalf("Error creating %c: %v", pre, err)
}
}
return kp
}
var b32Enc = base32.StdEncoding.WithPadding(base32.NoPadding)
func createVanityKey(keyType, vanity, entropy string, max int) KeyPair {
spinners := []rune(`⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏`)
pre := preForType(keyType)
vanity = strings.ToUpper(vanity)
// Check to make sure we can base32 into it by trying to decode it.
_, err := b32Enc.DecodeString(vanity)
if err != nil {
log.Fatalf("Can not generate base32 encoded strings to match '%s'", vanity)
}
ncpu := runtime.NumCPU()
// Work channel
wch := make(chan struct{})
defer close(wch)
// Found solution
found := make(chan KeyPair)
// Start NumCPU go routines.
for i := 0; i < ncpu; i++ {
go func() {
for range wch {
kp := genKeyPair(pre, entropy)
pub, _ := kp.PublicKey()
if strings.HasPrefix(pub[1:], vanity) {
found <- kp
return
}
}
}()
}
// Run through max iterations.
for i := 0; i < max; i++ {
spin := spinners[i%len(spinners)]
fmt.Fprintf(os.Stderr, "\r\033[mcomputing\033[m %s ", string(spin))
wch <- struct{}{}
select {
case kp := <-found:
fmt.Fprintf(os.Stderr, "\r")
return kp
default:
}
}
fmt.Fprintf(os.Stderr, "\r")
log.Fatalf("Failed to generate prefix after %d attempts", max)
return nil
}
func readKeyFile(filename string) []byte {
var key []byte
contents, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
defer wipeSlice(contents)
lines := bytes.Split(contents, []byte("\n"))
for _, line := range lines {
if nkeys.IsValidEncoding(line) {
key = make([]byte, len(line))
copy(key, line)
return key
}
}
if key == nil {
log.Fatalf("Could not find a valid key")
}
return key
}
func wipeSlice(buf []byte) {
for i := range buf {
buf[i] = 'x'
}
}
|