mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-13 22:51:08 -05:00
Fix PrivateKeyBytes to error out and fail tests on error.
This commit is contained in:
parent
a682100c5e
commit
376e1090a3
1 changed files with 15 additions and 11 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -95,22 +96,25 @@ func TestSaveAndLoadECCPrivateKey(t *testing.T) {
|
||||||
|
|
||||||
// PrivateKeysSame compares the bytes of a and b and returns true if they are the same.
|
// PrivateKeysSame compares the bytes of a and b and returns true if they are the same.
|
||||||
func PrivateKeysSame(a, b crypto.PrivateKey) bool {
|
func PrivateKeysSame(a, b crypto.PrivateKey) bool {
|
||||||
return bytes.Equal(PrivateKeyBytes(a), PrivateKeyBytes(b))
|
var abytes, bbytes []byte
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if abytes, err = PrivateKeyBytes(a); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if bbytes, err = PrivateKeyBytes(b); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return bytes.Equal(abytes, bbytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrivateKeyBytes returns the bytes of DER-encoded key.
|
// PrivateKeyBytes returns the bytes of DER-encoded key.
|
||||||
func PrivateKeyBytes(key crypto.PrivateKey) []byte {
|
func PrivateKeyBytes(key crypto.PrivateKey) ([]byte, error) {
|
||||||
var keyBytes []byte
|
|
||||||
switch key := key.(type) {
|
switch key := key.(type) {
|
||||||
case *rsa.PrivateKey:
|
case *rsa.PrivateKey:
|
||||||
keyBytes = x509.MarshalPKCS1PrivateKey(key)
|
return x509.MarshalPKCS1PrivateKey(key), nil
|
||||||
case *ecdsa.PrivateKey:
|
case *ecdsa.PrivateKey:
|
||||||
var err error
|
return x509.MarshalECPrivateKey(key)
|
||||||
var t *testing.T
|
|
||||||
keyBytes, err = x509.MarshalECPrivateKey(key)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return keyBytes
|
return nil, errors.New("Bad juju")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue