| 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 generator |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/x509" |
| 21 |
"encoding/pem" |
| 22 |
"errors" |
| 23 |
|
| 24 |
jose "github.com/go-jose/go-jose/v3" |
| 25 |
) |
| 26 |
|
| 27 |
func LoadJSONWebKey(json []byte, pub bool) (*jose.JSONWebKey, error) { |
| 28 |
var jwk jose.JSONWebKey |
| 29 |
err := jwk.UnmarshalJSON(json) |
| 30 |
if err != nil { |
| 31 |
return nil, err |
| 32 |
} |
| 33 |
if !jwk.Valid() { |
| 34 |
return nil, errors.New("invalid JWK key") |
| 35 |
} |
| 36 |
if jwk.IsPublic() != pub { |
| 37 |
return nil, errors.New("priv/pub JWK key mismatch") |
| 38 |
} |
| 39 |
return &jwk, nil |
| 40 |
} |
| 41 |
|
| 42 |
// LoadPublicKey loads a public key from PEM/DER/JWK-encoded data. |
| 43 |
func LoadPublicKey(data []byte) (interface{}, error) { |
| 44 |
input := data |
| 45 |
|
| 46 |
block, _ := pem.Decode(data) |
| 47 |
if block != nil { |
| 48 |
input = block.Bytes |
| 49 |
} |
| 50 |
|
| 51 |
// Try to load SubjectPublicKeyInfo |
| 52 |
pub, err0 := x509.ParsePKIXPublicKey(input) |
| 53 |
if err0 == nil { |
| 54 |
return pub, nil |
| 55 |
} |
| 56 |
|
| 57 |
cert, err1 := x509.ParseCertificate(input) |
| 58 |
if err1 == nil { |
| 59 |
return cert.PublicKey, nil |
| 60 |
} |
| 61 |
|
| 62 |
jwk, err2 := LoadJSONWebKey(data, true) |
| 63 |
if err2 == nil { |
| 64 |
return jwk, nil |
| 65 |
} |
| 66 |
|
| 67 |
return nil, errors.New("parse error, invalid public key") |
| 68 |
} |
| 69 |
|
| 70 |
// LoadPrivateKey loads a private key from PEM/DER/JWK-encoded data. |
| 71 |
func LoadPrivateKey(data []byte) (interface{}, error) { |
| 72 |
input := data |
| 73 |
|
| 74 |
block, _ := pem.Decode(data) |
| 75 |
if block != nil { |
| 76 |
input = block.Bytes |
| 77 |
} |
| 78 |
|
| 79 |
var priv interface{} |
| 80 |
priv, err0 := x509.ParsePKCS1PrivateKey(input) |
| 81 |
if err0 == nil { |
| 82 |
return priv, nil |
| 83 |
} |
| 84 |
|
| 85 |
priv, err1 := x509.ParsePKCS8PrivateKey(input) |
| 86 |
if err1 == nil { |
| 87 |
return priv, nil |
| 88 |
} |
| 89 |
|
| 90 |
priv, err2 := x509.ParseECPrivateKey(input) |
| 91 |
if err2 == nil { |
| 92 |
return priv, nil |
| 93 |
} |
| 94 |
|
| 95 |
jwk, err3 := LoadJSONWebKey(input, false) |
| 96 |
if err3 == nil { |
| 97 |
return jwk, nil |
| 98 |
} |
| 99 |
|
| 100 |
return nil, errors.New("parse error, invalid private key") |
| 101 |
} |