Browse Source

Switch to calling openssl

master
Matt Baer 5 years ago
parent
commit
b417e93a0f
1 changed files with 26 additions and 11 deletions
  1. +26
    -11
      activitypub/keys.go

+ 26
- 11
activitypub/keys.go View File

@@ -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
}

return out.Bytes(), nil
}

// EncodeKeysToPEM encodes public and private key to PEM format, returning
// them in that order.
func EncodeKeysToPEM(privKey openssl.PrivateKey) (pubPEM []byte, privPEM []byte) {
// 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


Loading…
Cancel
Save