Switch to calling openssl

This commit is contained in:
Matt Baer
2018-06-25 11:31:30 -04:00
parent 602e989e59
commit b417e93a0f
+26 -11
View File
@@ -1,35 +1,50 @@
package activitypub package activitypub
import ( import (
"bytes"
"crypto" "crypto"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"github.com/spacemonkeygo/openssl"
"log" "log"
"os/exec"
) )
const keyBitSize = 2048 const keyBitSize = 2048
// GenerateKey creates an RSA keypair. func openssl(stdin []byte, args ...string) ([]byte, error) {
func GenerateKey() (openssl.PrivateKey, error) { cmd := exec.Command("openssl", args...)
return openssl.GenerateRSAKey(keyBitSize)
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
}
return out.Bytes(), nil
} }
// EncodeKeysToPEM encodes public and private key to PEM format, returning // GenerateKeys creates an RSA keypair and returns the public and private key,
// them in that order. // in that order.
func EncodeKeysToPEM(privKey openssl.PrivateKey) (pubPEM []byte, privPEM []byte) { func GenerateKeys() (pubPEM []byte, privPEM []byte) {
var err error var err error
privPEM, err = privKey.MarshalPKCS1PrivateKeyPEM() privPEM, err = openssl(nil, "genrsa", fmt.Sprintf("%d", keyBitSize))
if err != nil { if err != nil {
log.Printf("Unable to marshal private key: %v", err) log.Printf("Unable to generate private key: %v", err)
return nil, nil return nil, nil
} }
pubPEM, err = privKey.MarshalPKIXPublicKeyPEM() pubPEM, err = openssl(privPEM, "rsa", "-in", "/dev/stdin", "-pubout")
if err != nil { 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 nil, nil
} }
return return