| 1 |
// |
| 2 |
// Copyright 2021 The Sigstore Authors. |
| 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 |
// Package goodkey implements public key validation using the boulder/goodkey policy. |
| 17 |
package goodkey |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"crypto" |
| 22 |
"crypto/ecdsa" |
| 23 |
"crypto/ed25519" |
| 24 |
"crypto/rsa" // nolint:gosec |
| 25 |
"errors" |
| 26 |
"fmt" |
| 27 |
|
| 28 |
"github.com/letsencrypt/boulder/goodkey" |
| 29 |
) |
| 30 |
|
| 31 |
// ValidatePubKey validates the parameters of an RSA, ECDSA, or ED25519 public key. |
| 32 |
func ValidatePubKey(pub crypto.PublicKey) error { |
| 33 |
// goodkey policy enforces: |
| 34 |
// * RSA |
| 35 |
// * Size of key: 2048 <= size <= 4096, size % 8 = 0 |
| 36 |
// * Exponent E = 65537 (Default exponent for OpenSSL and Golang) |
| 37 |
// * Small primes check for modulus |
| 38 |
// * Weak keys generated by Infineon hardware (see https://crocs.fi.muni.cz/public/papers/rsa_ccs17) |
| 39 |
// * Key is easily factored with Fermat's factorization method |
| 40 |
// * EC |
| 41 |
// * Public key Q is not the identity element (Ø) |
| 42 |
// * Public key Q's x and y are within [0, p-1] |
| 43 |
// * Public key Q is on the curve |
| 44 |
// * Public key Q's order matches the subgroups (nQ = Ø) |
| 45 |
allowedKeys := &goodkey.AllowedKeys{ |
| 46 |
RSA2048: true, |
| 47 |
RSA3072: true, |
| 48 |
RSA4096: true, |
| 49 |
ECDSAP256: true, |
| 50 |
ECDSAP384: true, |
| 51 |
ECDSAP521: true, |
| 52 |
} |
| 53 |
cfg := &goodkey.Config{ |
| 54 |
FermatRounds: 100, |
| 55 |
AllowedKeys: allowedKeys, |
| 56 |
} |
| 57 |
p, err := goodkey.NewPolicy(cfg, nil) |
| 58 |
if err != nil { |
| 59 |
// Should not occur, only chances to return errors are if fermat rounds |
| 60 |
// are <0 or when loading blocked/weak keys from disk (not used here) |
| 61 |
return errors.New("unable to initialize key policy") |
| 62 |
} |
| 63 |
|
| 64 |
switch pk := pub.(type) { |
| 65 |
case *rsa.PublicKey: |
| 66 |
// ctx is unused |
| 67 |
return p.GoodKey(context.Background(), pub) |
| 68 |
case *ecdsa.PublicKey: |
| 69 |
// ctx is unused |
| 70 |
return p.GoodKey(context.Background(), pub) |
| 71 |
case ed25519.PublicKey: |
| 72 |
return validateEd25519Key(pk) |
| 73 |
} |
| 74 |
return fmt.Errorf("unsupported public key type: %T", pub) |
| 75 |
} |
| 76 |
|
| 77 |
// No validations currently, ED25519 supports only one key size. |
| 78 |
func validateEd25519Key(_ ed25519.PublicKey) error { |
| 79 |
return nil |
| 80 |
} |