| 1 |
/*- |
| 2 |
* Copyright 2019 Square Inc. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"encoding/base64" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"os" |
| 25 |
|
| 26 |
kingpin "gopkg.in/alecthomas/kingpin.v2" |
| 27 |
|
| 28 |
generator "github.com/go-jose/go-jose/jose-util/generator" |
| 29 |
jose "github.com/go-jose/go-jose/v3" |
| 30 |
) |
| 31 |
|
| 32 |
var ( |
| 33 |
app = kingpin.New("jose-util", "A command-line utility for dealing with JOSE objects") |
| 34 |
|
| 35 |
// Util-wide flags |
| 36 |
keyFile = app.Flag("key", "Path to key file (if applicable, PEM, DER or JWK format)").ExistingFile() |
| 37 |
inFile = app.Flag("in", "Path to input file (if applicable, stdin if missing)").ExistingFile() |
| 38 |
outFile = app.Flag("out", "Path to output file (if applicable, stdout if missing)").ExistingFile() |
| 39 |
|
| 40 |
// Encrypt |
| 41 |
encryptCommand = app.Command("encrypt", "Encrypt a plaintext, output ciphertext") |
| 42 |
encryptAlgFlag = encryptCommand.Flag("alg", "Key management algorithm (e.g. RSA-OAEP)").Required().String() |
| 43 |
encryptEncFlag = encryptCommand.Flag("enc", "Content encryption algorithm (e.g. A128GCM)").Required().String() |
| 44 |
encryptFullFlag = encryptCommand.Flag("full", "Use JSON Serialization format (instead of compact)").Bool() |
| 45 |
|
| 46 |
// Decrypt |
| 47 |
decryptCommand = app.Command("decrypt", "Decrypt a ciphertext, output plaintext") |
| 48 |
|
| 49 |
// Sign |
| 50 |
signCommand = app.Command("sign", "Sign a payload, output signed message") |
| 51 |
signAlgFlag = signCommand.Flag("alg", "Key management algorithm (e.g. RSA-OAEP)").Required().String() |
| 52 |
signFullFlag = signCommand.Flag("full", "Use JSON Serialization format (instead of compact)").Bool() |
| 53 |
|
| 54 |
// Verify |
| 55 |
verifyCommand = app.Command("verify", "Verify a signed message, output payload") |
| 56 |
|
| 57 |
// Expand |
| 58 |
expandCommand = app.Command("expand", "Expand JOSE object to JSON Serialization format") |
| 59 |
expandFormatFlag = expandCommand.Flag("format", "Type of message to expand (JWS or JWE, defaults to JWE)").String() |
| 60 |
|
| 61 |
// Base64-decode |
| 62 |
base64DecodeCommand = app.Command("b64decode", "Decode a base64-encoded payload (auto-selects standard/url-safe)") |
| 63 |
|
| 64 |
// Generate key |
| 65 |
generateCommand = app.Command("generate-key", "Generate a public/private key pair in JWK format") |
| 66 |
generateUseFlag = generateCommand.Flag("use", "Desired public key usage (use header), one of [enc sig]").Required().Enum("enc", "sig") |
| 67 |
generateAlgFlag = generateCommand.Flag("alg", "Desired key pair algorithm (alg header)").Required().Enum( |
| 68 |
// For signing |
| 69 |
string(jose.EdDSA), |
| 70 |
string(jose.ES256), string(jose.ES384), string(jose.ES512), |
| 71 |
string(jose.RS256), string(jose.RS384), string(jose.RS512), |
| 72 |
string(jose.PS256), string(jose.PS384), string(jose.PS512), |
| 73 |
// For encryption |
| 74 |
string(jose.RSA1_5), string(jose.RSA_OAEP), string(jose.RSA_OAEP_256), |
| 75 |
string(jose.ECDH_ES), string(jose.ECDH_ES_A128KW), string(jose.ECDH_ES_A192KW), string(jose.ECDH_ES_A256KW), |
| 76 |
) |
| 77 |
generateKeySizeFlag = generateCommand.Flag("size", "Key size in bits (e.g. 2048 if generating an RSA key)").Int() |
| 78 |
generateKeyIdentFlag = generateCommand.Flag("kid", "Optional Key ID (kid header, generate random kid if not set)").String() |
| 79 |
) |
| 80 |
|
| 81 |
func main() { |
| 82 |
app.Version("v3") |
| 83 |
app.UsageTemplate(kingpin.LongHelpTemplate) |
| 84 |
|
| 85 |
command := kingpin.MustParse(app.Parse(os.Args[1:])) |
| 86 |
|
| 87 |
switch command { |
| 88 |
case encryptCommand.FullCommand(): |
| 89 |
encrypt() |
| 90 |
case decryptCommand.FullCommand(): |
| 91 |
decrypt() |
| 92 |
case signCommand.FullCommand(): |
| 93 |
sign() |
| 94 |
case verifyCommand.FullCommand(): |
| 95 |
verify() |
| 96 |
case expandCommand.FullCommand(): |
| 97 |
expand() |
| 98 |
case generateCommand.FullCommand(): |
| 99 |
generate() |
| 100 |
case base64DecodeCommand.FullCommand(): |
| 101 |
in := inputStream(*inFile) |
| 102 |
out := outputStream(*outFile) |
| 103 |
io.Copy(out, base64.NewDecoder(base64.RawStdEncoding, generator.Base64Reader{In: bufio.NewReader(in)})) |
| 104 |
defer in.Close() |
| 105 |
defer out.Close() |
| 106 |
default: |
| 107 |
fmt.Fprintf(os.Stderr, "invalid command: %s\n", command) |
| 108 |
os.Exit(1) |
| 109 |
} |
| 110 |
} |