Switch to calling openssl
This commit is contained in:
+26
-11
@@ -1,35 +1,50 @@
|
||||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"github.com/spacemonkeygo/openssl"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const keyBitSize = 2048
|
||||
|
||||
// GenerateKey creates an RSA keypair.
|
||||
func GenerateKey() (openssl.PrivateKey, error) {
|
||||
return openssl.GenerateRSAKey(keyBitSize)
|
||||
func openssl(stdin []byte, args ...string) ([]byte, error) {
|
||||
cmd := exec.Command("openssl", args...)
|
||||
|
||||
in := bytes.NewReader(stdin)
|
||||
out := &bytes.Buffer{}
|
||||
errs := &bytes.Buffer{}
|
||||
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = in, out, errs
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
if len(errs.Bytes()) > 0 {
|
||||
return nil, fmt.Errorf("error running %s (%s):\n %v", cmd.Args, err, errs.String())
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// EncodeKeysToPEM encodes public and private key to PEM format, returning
|
||||
// them in that order.
|
||||
func EncodeKeysToPEM(privKey openssl.PrivateKey) (pubPEM []byte, privPEM []byte) {
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
// GenerateKeys creates an RSA keypair and returns the public and private key,
|
||||
// in that order.
|
||||
func GenerateKeys() (pubPEM []byte, privPEM []byte) {
|
||||
var err error
|
||||
privPEM, err = privKey.MarshalPKCS1PrivateKeyPEM()
|
||||
privPEM, err = openssl(nil, "genrsa", fmt.Sprintf("%d", keyBitSize))
|
||||
if err != nil {
|
||||
log.Printf("Unable to marshal private key: %v", err)
|
||||
log.Printf("Unable to generate private key: %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
pubPEM, err = privKey.MarshalPKIXPublicKeyPEM()
|
||||
pubPEM, err = openssl(privPEM, "rsa", "-in", "/dev/stdin", "-pubout")
|
||||
if err != nil {
|
||||
log.Printf("Unable to marshal public key: %v", err)
|
||||
log.Printf("Unable to get public key: %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user