Switch keypair generation to OpenSSL
This commit is contained in:
+36
-38
@@ -1,68 +1,66 @@
|
|||||||
package activitypub
|
package activitypub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/spacemonkeygo/openssl"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const keyBitSize = 2048
|
const keyBitSize = 2048
|
||||||
|
|
||||||
// GenerateKey creates an RSA keypair.
|
// GenerateKey creates an RSA keypair.
|
||||||
func GenerateKey() (*rsa.PrivateKey, error) {
|
func GenerateKey() (openssl.PrivateKey, error) {
|
||||||
priv, err := rsa.GenerateKey(rand.Reader, keyBitSize)
|
return openssl.GenerateRSAKey(keyBitSize)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = priv.Validate()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return priv, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeKeysToPEM encodes public and private key to PEM format, returning
|
// EncodeKeysToPEM encodes public and private key to PEM format, returning
|
||||||
// them in that order.
|
// them in that order.
|
||||||
func EncodeKeysToPEM(privKey *rsa.PrivateKey) ([]byte, []byte) {
|
func EncodeKeysToPEM(privKey openssl.PrivateKey) (pubPEM []byte, privPEM []byte) {
|
||||||
privDER := x509.MarshalPKCS1PrivateKey(privKey)
|
var err error
|
||||||
|
privPEM, err = privKey.MarshalPKCS1PrivateKeyPEM()
|
||||||
// pem.Block
|
if err != nil {
|
||||||
privBlock := pem.Block{
|
log.Printf("Unable to marshal private key: %v", err)
|
||||||
Type: "RSA PRIVATE KEY",
|
|
||||||
Bytes: privDER,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Private key in PEM format
|
|
||||||
privPEM := pem.EncodeToMemory(&privBlock)
|
|
||||||
|
|
||||||
// Encode public key
|
|
||||||
pubKey, ok := privKey.Public().(*rsa.PublicKey)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("Public key isn't RSA!")
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
pubDER := x509.MarshalPKCS1PublicKey(pubKey)
|
|
||||||
pubBlock := pem.Block{
|
|
||||||
Type: "PUBLIC KEY",
|
|
||||||
Bytes: pubDER,
|
|
||||||
}
|
|
||||||
pubPEM := pem.EncodeToMemory(&pubBlock)
|
|
||||||
|
|
||||||
return pubPEM, privPEM
|
pubPEM, err = privKey.MarshalPKIXPublicKeyPEM()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to marshal public key: %v", err)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
|
||||||
|
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
|
||||||
|
switch key := key.(type) {
|
||||||
|
case *rsa.PrivateKey:
|
||||||
|
return key, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("found unknown private key type in PKCS#8 wrapping")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if key, err := x509.ParseECPrivateKey(der); err == nil {
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to parse private key")
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodePrivateKey encodes public and private key to PEM format, returning
|
// DecodePrivateKey encodes public and private key to PEM format, returning
|
||||||
// them in that order.
|
// them in that order.
|
||||||
func DecodePrivateKey(k []byte) (*rsa.PrivateKey, error) {
|
func DecodePrivateKey(k []byte) (crypto.PrivateKey, error) {
|
||||||
block, _ := pem.Decode(k)
|
block, _ := pem.Decode(k)
|
||||||
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
||||||
return nil, fmt.Errorf("failed to decode PEM block containing private key")
|
return nil, fmt.Errorf("failed to decode PEM block containing private key")
|
||||||
}
|
}
|
||||||
|
|
||||||
return x509.ParsePKCS1PrivateKey(block.Bytes)
|
return parsePrivateKey(block.Bytes)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user