| 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 |
"crypto" |
| 21 |
"encoding/base64" |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
|
| 25 |
"github.com/go-jose/go-jose/jose-util/generator" |
| 26 |
"github.com/go-jose/go-jose/v3" |
| 27 |
) |
| 28 |
|
| 29 |
func generate() { |
| 30 |
var privKey crypto.PrivateKey |
| 31 |
var pubKey crypto.PublicKey |
| 32 |
var err error |
| 33 |
|
| 34 |
switch *generateUseFlag { |
| 35 |
case "sig": |
| 36 |
pubKey, privKey, err = generator.NewSigningKey(jose.SignatureAlgorithm(*generateAlgFlag), *generateKeySizeFlag) |
| 37 |
case "enc": |
| 38 |
pubKey, privKey, err = generator.NewEncryptionKey(jose.KeyAlgorithm(*generateAlgFlag), *generateKeySizeFlag) |
| 39 |
default: |
| 40 |
// According to RFC 7517 section-8.2. This is unlikely to change in the |
| 41 |
// near future. If it were, new values could be found in the registry under |
| 42 |
// "JSON Web Key Use": https://www.iana.org/assignments/jose/jose.xhtml |
| 43 |
app.FatalIfError(errors.New("invalid key use. Must be \"sig\" or \"enc\""), "unable to generate key") |
| 44 |
} |
| 45 |
app.FatalIfError(err, "unable to generate key") |
| 46 |
|
| 47 |
kid := *generateKeyIdentFlag |
| 48 |
|
| 49 |
priv := jose.JSONWebKey{Key: privKey, KeyID: kid, Algorithm: *generateAlgFlag, Use: *generateUseFlag} |
| 50 |
|
| 51 |
// Generate a canonical kid based on RFC 7638 |
| 52 |
if kid == "" { |
| 53 |
thumb, err := priv.Thumbprint(crypto.SHA256) |
| 54 |
app.FatalIfError(err, "unable to compute thumbprint") |
| 55 |
|
| 56 |
kid = base64.URLEncoding.EncodeToString(thumb) |
| 57 |
priv.KeyID = kid |
| 58 |
} |
| 59 |
|
| 60 |
// I'm not sure why we couldn't use `pub := priv.Public()` here as the private |
| 61 |
// key should contain the public key. In case for some reason it doesn't, |
| 62 |
// this builds a public JWK from scratch. |
| 63 |
pub := jose.JSONWebKey{Key: pubKey, KeyID: kid, Algorithm: *generateAlgFlag, Use: *generateUseFlag} |
| 64 |
|
| 65 |
if priv.IsPublic() || !pub.IsPublic() || !priv.Valid() || !pub.Valid() { |
| 66 |
app.Fatalf("invalid keys were generated") |
| 67 |
} |
| 68 |
|
| 69 |
privJSON, err := priv.MarshalJSON() |
| 70 |
app.FatalIfError(err, "failed to marshal private key to JSON") |
| 71 |
pubJSON, err := pub.MarshalJSON() |
| 72 |
app.FatalIfError(err, "failed to marshal public key to JSON") |
| 73 |
|
| 74 |
name := fmt.Sprintf("jwk-%s-%s", *generateUseFlag, kid) |
| 75 |
pubFile := fmt.Sprintf("%s-pub.json", name) |
| 76 |
privFile := fmt.Sprintf("%s-priv.json", name) |
| 77 |
|
| 78 |
err = writeNewFile(pubFile, pubJSON, 0444) |
| 79 |
app.FatalIfError(err, "error on write to file %s", pubFile) |
| 80 |
|
| 81 |
err = writeNewFile(privFile, privJSON, 0400) |
| 82 |
app.FatalIfError(err, "error on write to file %s", privFile) |
| 83 |
} |