| 1 |
// Copyright 2021 The Sigstore Authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package goodkey |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto/ecdsa" |
| 19 |
"crypto/ed25519" |
| 20 |
"crypto/elliptic" |
| 21 |
"crypto/rand" |
| 22 |
"crypto/rsa" |
| 23 |
"errors" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/letsencrypt/boulder/goodkey" |
| 27 |
) |
| 28 |
|
| 29 |
func TestValidatePubKeyUnsupported(t *testing.T) { |
| 30 |
// Fails with unexpected key type |
| 31 |
type PublicKey struct{} |
| 32 |
err := ValidatePubKey(PublicKey{}) |
| 33 |
if err == nil || err.Error() != "unsupported public key type: goodkey.PublicKey" { |
| 34 |
t.Errorf("expected unsupported public key type, got %v", err) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func TestValidatePubKeyRsa(t *testing.T) { |
| 39 |
// Validate common RSA key sizes |
| 40 |
for _, bits := range []int{2048, 3072, 4096} { |
| 41 |
priv, err := rsa.GenerateKey(rand.Reader, bits) |
| 42 |
if err != nil { |
| 43 |
t.Fatalf("rsa.GenerateKey failed: %v", err) |
| 44 |
} |
| 45 |
if err := ValidatePubKey(priv.Public()); err != nil { |
| 46 |
t.Errorf("unexpected error validating public key: %v", err) |
| 47 |
} |
| 48 |
} |
| 49 |
// Fails with small key size |
| 50 |
priv, err := rsa.GenerateKey(rand.Reader, 1024) |
| 51 |
if err != nil { |
| 52 |
t.Fatalf("rsa.GenerateKey failed: %v", err) |
| 53 |
} |
| 54 |
if err := ValidatePubKey(priv.Public()); err == nil || err.Error() != "key size not supported: 1024" { |
| 55 |
t.Errorf("expected rsa key size not supported, got %v", err) |
| 56 |
} |
| 57 |
// Fails with large key size |
| 58 |
priv, err = rsa.GenerateKey(rand.Reader, 5000) |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("rsa.GenerateKey failed: %v", err) |
| 61 |
} |
| 62 |
if err := ValidatePubKey(priv.Public()); err == nil || err.Error() != "key size not supported: 5000" { |
| 63 |
t.Errorf("expected rsa key size not supported, got %v", err) |
| 64 |
} |
| 65 |
// Fails with key size that's not a multiple of 8 |
| 66 |
priv, err = rsa.GenerateKey(rand.Reader, 4095) |
| 67 |
if err != nil { |
| 68 |
t.Fatalf("rsa.GenerateKey failed: %v", err) |
| 69 |
} |
| 70 |
if err := ValidatePubKey(priv.Public()); err == nil || err.Error() != "key size not supported: 4095" { |
| 71 |
t.Errorf("expected rsa key size not supported, got %v", err) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
type testCurve struct { |
| 76 |
elliptic.Curve |
| 77 |
} |
| 78 |
|
| 79 |
func (t testCurve) Params() *elliptic.CurveParams { |
| 80 |
return &elliptic.CurveParams{} |
| 81 |
} |
| 82 |
|
| 83 |
func TestValidatePubKeyEcdsa(t *testing.T) { |
| 84 |
for _, curve := range []elliptic.Curve{elliptic.P256(), elliptic.P384(), elliptic.P521()} { |
| 85 |
priv, err := ecdsa.GenerateKey(curve, rand.Reader) |
| 86 |
if err != nil { |
| 87 |
t.Fatalf("ecdsa.GenerateKey failed: %v", err) |
| 88 |
} |
| 89 |
if err := ValidatePubKey(priv.Public()); err != nil { |
| 90 |
t.Errorf("unexpected error validating public key: %v", err) |
| 91 |
} |
| 92 |
// Should fail with negative coordinates |
| 93 |
priv.X.Neg(priv.X) |
| 94 |
if err := ValidatePubKey(priv.Public()); err == nil { |
| 95 |
t.Errorf("expected error when validating public key") |
| 96 |
} |
| 97 |
} |
| 98 |
// Fails with smaller curve |
| 99 |
priv, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader) |
| 100 |
if err != nil { |
| 101 |
t.Fatalf("ecdsa.GenerateKey failed: %v", err) |
| 102 |
} |
| 103 |
if err := ValidatePubKey(priv.Public()); err == nil || !errors.Is(err, goodkey.ErrBadKey) { |
| 104 |
t.Errorf("expected unsupported curve, got %v", err) |
| 105 |
} |
| 106 |
// Fails with unknown curve |
| 107 |
err = ValidatePubKey(&ecdsa.PublicKey{ |
| 108 |
Curve: testCurve{}, |
| 109 |
}) |
| 110 |
if err == nil || !errors.Is(err, goodkey.ErrBadKey) { |
| 111 |
t.Errorf("expected unexpected curve, got %v", err) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestValidatePubKeyEd25519(t *testing.T) { |
| 116 |
pub, _, err := ed25519.GenerateKey(rand.Reader) |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("ed25519.GenerateKey failed: %v", err) |
| 119 |
} |
| 120 |
if err := ValidatePubKey(pub); err != nil { |
| 121 |
t.Errorf("unexpected error validating public key: %v", err) |
| 122 |
} |
| 123 |
// Only success, ED25519 keys do not support customization |
| 124 |
} |