2021-08-22 23:55:33 +08:00
|
|
|
//go:build openssl
|
2019-05-23 01:31:11 +08:00
|
|
|
// +build openssl
|
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2020-01-16 09:29:26 +08:00
|
|
|
"sync"
|
|
|
|
|
2019-05-23 01:31:11 +08:00
|
|
|
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
|
|
|
|
2021-07-23 03:18:09 +08:00
|
|
|
"github.com/libp2p/go-openssl"
|
2019-05-23 01:31:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// define these as separate types so we can add more key types later and reuse
|
|
|
|
// code.
|
|
|
|
|
|
|
|
type opensslPublicKey struct {
|
|
|
|
key openssl.PublicKey
|
2020-01-16 09:29:26 +08:00
|
|
|
|
|
|
|
cacheLk sync.Mutex
|
|
|
|
cached []byte
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type opensslPrivateKey struct {
|
|
|
|
key openssl.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalOpensslPrivateKey(b []byte) (opensslPrivateKey, error) {
|
|
|
|
sk, err := openssl.LoadPrivateKeyFromDER(b)
|
|
|
|
if err != nil {
|
|
|
|
return opensslPrivateKey{}, err
|
|
|
|
}
|
|
|
|
return opensslPrivateKey{sk}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalOpensslPublicKey(b []byte) (opensslPublicKey, error) {
|
|
|
|
sk, err := openssl.LoadPublicKeyFromDER(b)
|
|
|
|
if err != nil {
|
|
|
|
return opensslPublicKey{}, err
|
|
|
|
}
|
2020-01-16 09:29:26 +08:00
|
|
|
return opensslPublicKey{key: sk, cached: b}, nil
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify compares a signature against input data
|
|
|
|
func (pk *opensslPublicKey) Verify(data, sig []byte) (bool, error) {
|
|
|
|
err := pk.key.VerifyPKCS1v15(openssl.SHA256_Method, data, sig)
|
|
|
|
return err == nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk *opensslPublicKey) Type() pb.KeyType {
|
|
|
|
switch pk.key.KeyType() {
|
|
|
|
case openssl.KeyTypeRSA:
|
|
|
|
return pb.KeyType_RSA
|
|
|
|
default:
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk *opensslPublicKey) Raw() ([]byte, error) {
|
|
|
|
return pk.key.MarshalPKIXPublicKeyDER()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals checks whether this key is equal to another
|
|
|
|
func (pk *opensslPublicKey) Equals(k Key) bool {
|
2019-06-28 20:04:56 +08:00
|
|
|
k0, ok := k.(*RsaPublicKey)
|
|
|
|
if !ok {
|
2019-06-28 23:23:16 +08:00
|
|
|
return basicEquals(pk, k)
|
2019-06-28 18:35:29 +08:00
|
|
|
}
|
2019-06-28 20:04:56 +08:00
|
|
|
|
|
|
|
return pk.key.Equal(k0.opensslPublicKey.key)
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign returns a signature of the input data
|
|
|
|
func (sk *opensslPrivateKey) Sign(message []byte) ([]byte, error) {
|
|
|
|
return sk.key.SignPKCS1v15(openssl.SHA256_Method, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPublic returns a public key
|
|
|
|
func (sk *opensslPrivateKey) GetPublic() PubKey {
|
2020-01-16 09:29:26 +08:00
|
|
|
return &opensslPublicKey{key: sk.key}
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sk *opensslPrivateKey) Type() pb.KeyType {
|
|
|
|
switch sk.key.KeyType() {
|
|
|
|
case openssl.KeyTypeRSA:
|
|
|
|
return pb.KeyType_RSA
|
|
|
|
default:
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sk *opensslPrivateKey) Raw() ([]byte, error) {
|
|
|
|
return sk.key.MarshalPKCS1PrivateKeyDER()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals checks whether this key is equal to another
|
|
|
|
func (sk *opensslPrivateKey) Equals(k Key) bool {
|
2019-06-28 20:04:56 +08:00
|
|
|
k0, ok := k.(*RsaPrivateKey)
|
|
|
|
if !ok {
|
2019-06-28 23:23:16 +08:00
|
|
|
return basicEquals(sk, k)
|
2019-06-28 18:35:29 +08:00
|
|
|
}
|
2019-06-28 20:04:56 +08:00
|
|
|
|
|
|
|
return sk.key.Equal(k0.opensslPrivateKey.key)
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|