aboutsummaryrefslogtreecommitdiff
path: root/crypto.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-18 16:48:21 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-18 16:48:21 +0100
commit6d1eef0c011cbe666300ee023ccdbeac80dc43c0 (patch)
tree681bf82a294bf18308d5453d0b65768b6d2cdff5 /crypto.go
parentbdcc98eaf07bdbc478223c38cf06266b76177776 (diff)
Add key funcs
Diffstat (limited to 'crypto.go')
-rw-r--r--crypto.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/crypto.go b/crypto.go
new file mode 100644
index 0000000..b2173e1
--- /dev/null
+++ b/crypto.go
@@ -0,0 +1,44 @@
+package acme
+
+import (
+ "crypto/rand"
+ "crypto/rsa"
+ "crypto/x509"
+ "crypto/x509/pkix"
+ "encoding/pem"
+ "io"
+ "io/ioutil"
+)
+
+func LoadKey(r io.Reader) (*rsa.PrivateKey, error) {
+ der, err := ioutil.ReadAll(r)
+ if err != nil {
+ return nil, err
+ }
+ block, _ := pem.Decode(der)
+ return x509.ParsePKCS1PrivateKey(block.Bytes)
+}
+
+func NewKey(w io.Writer, size int) (*rsa.PrivateKey, error) {
+ key, err := rsa.GenerateKey(rand.Reader, size)
+ if err != nil {
+ return nil, err
+ }
+ block := &pem.Block{
+ Type: "RSA PRIVATE KEY",
+ Bytes: x509.MarshalPKCS1PrivateKey(key),
+ }
+ return key, pem.Encode(w, block)
+}
+
+func NewCSR(altnames []string, key *rsa.PrivateKey) ([]byte, error) {
+ tmpl := x509.CertificateRequest{
+ Subject: pkix.Name{
+ CommonName: altnames[0],
+ },
+ }
+ if len(altnames) > 1 {
+ tmpl.DNSNames = altnames
+ }
+ return x509.CreateCertificateRequest(rand.Reader, &tmpl, key)
+}