forked from ory/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: Allows import of PEM/DER/JSON encoded keys
Closes #98 Signed-off-by: arekkas <aeneas@ory.am>
- Loading branch information
Showing
14 changed files
with
326 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright © 2018 NAME HERE <EMAIL ADDRESS> | ||
// | ||
// 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 cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// keysImportCmd represents the import command | ||
var keysImportCmd = &cobra.Command{ | ||
Use: "import <set> <file-1> [<file-2> [<file-3 [<...>]]]", | ||
Short: "Imports cryptographic keys of any format to the JSON Web Key Store", | ||
Long: `This command allows you to import cryptographic keys to the JSON Web Key Store. | ||
Currently supported formats are raw JSON Web Keys or PEM/DER encoded data. If the JSON Web Key Set exists already, | ||
the imported keys will be added to that set. Otherwise, a new set will be created. | ||
Please be aware that importing a private key does not automatically import its public key as well. | ||
Examples: | ||
hydra keys import my-set ./path/to/jwk.json ./path/to/jwk-2.json | ||
hydra keys import my-set ./path/to/rsa.key ./path/to/rsa.pub | ||
`, | ||
Run: cmdHandler.Keys.ImportKeys, | ||
} | ||
|
||
func init() { | ||
keysCmd.AddCommand(keysImportCmd) | ||
keysImportCmd.Flags().String("use", "sig", "Sets the \"use\" value of the JSON Web Key if not \"use\" value was defined by the key itself") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/*- | ||
* Copyright 2014 Square Inc. | ||
* | ||
* 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 pkg | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
|
||
"gopkg.in/square/go-jose.v2" | ||
) | ||
|
||
func LoadJSONWebKey(json []byte, pub bool) (*jose.JSONWebKey, error) { | ||
var jwk jose.JSONWebKey | ||
err := jwk.UnmarshalJSON(json) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !jwk.Valid() { | ||
return nil, errors.New("invalid JWK key") | ||
} | ||
if jwk.IsPublic() != pub { | ||
return nil, errors.New("priv/pub JWK key mismatch") | ||
} | ||
return &jwk, nil | ||
} | ||
|
||
// LoadPublicKey loads a public key from PEM/DER/JWK-encoded data. | ||
func LoadPublicKey(data []byte) (interface{}, error) { | ||
input := data | ||
|
||
block, _ := pem.Decode(data) | ||
if block != nil { | ||
input = block.Bytes | ||
} | ||
|
||
// Try to load SubjectPublicKeyInfo | ||
pub, err0 := x509.ParsePKIXPublicKey(input) | ||
if err0 == nil { | ||
return pub, nil | ||
} | ||
|
||
cert, err1 := x509.ParseCertificate(input) | ||
if err1 == nil { | ||
return cert.PublicKey, nil | ||
} | ||
|
||
jwk, err2 := LoadJSONWebKey(data, true) | ||
if err2 == nil { | ||
return jwk, nil | ||
} | ||
|
||
return nil, fmt.Errorf("square/go-jose: parse error, got '%s', '%s' and '%s'", err0, err1, err2) | ||
} | ||
|
||
// LoadPrivateKey loads a private key from PEM/DER/JWK-encoded data. | ||
func LoadPrivateKey(data []byte) (interface{}, error) { | ||
input := data | ||
|
||
block, _ := pem.Decode(data) | ||
if block != nil { | ||
input = block.Bytes | ||
} | ||
|
||
var priv interface{} | ||
priv, err0 := x509.ParsePKCS1PrivateKey(input) | ||
if err0 == nil { | ||
return priv, nil | ||
} | ||
|
||
priv, err1 := x509.ParsePKCS8PrivateKey(input) | ||
if err1 == nil { | ||
return priv, nil | ||
} | ||
|
||
priv, err2 := x509.ParseECPrivateKey(input) | ||
if err2 == nil { | ||
return priv, nil | ||
} | ||
|
||
jwk, err3 := LoadJSONWebKey(input, false) | ||
if err3 == nil { | ||
return jwk, nil | ||
} | ||
|
||
return nil, fmt.Errorf("square/go-jose: parse error, got '%s', '%s', '%s' and '%s'", err0, err1, err2, err3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-----BEGIN EC PRIVATE KEY----- | ||
MIGkAgEBBDDvoj/bM1HokUjYWO/IDFs26Jo0GIFtU3tMQQu7ZabKscDMK3dZA0mK | ||
v97ij7BBFbCgBwYFK4EEACKhZANiAAT3KhQQCDFN32y/B72g+qOFw/5/aNx1MvZa | ||
rwDDa/2G3V0HLTS0VE82sLEUKS8xwkWFI+gNRXk0vvN+Hf+myJI1jOIY+tYQlh+C | ||
ZiKGNJ6g5/Su7V6ukGtN+UiY+sx+0LI= | ||
-----END EC PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE9yoUEAgxTd9svwe9oPqjhcP+f2jcdTL2 | ||
Wq8Aw2v9ht1dBy00tFRPNrCxFCkvMcJFhSPoDUV5NL7zfh3/psiSNYziGPrWEJYf | ||
gmYihjSeoOf0ru1erpBrTflImPrMftCy | ||
-----END PUBLIC KEY----- |
Oops, something went wrong.