This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
191 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package rest | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"io/ioutil" | ||
"math/big" | ||
"os" | ||
"time" | ||
) | ||
|
||
type tls struct { | ||
cert, key string | ||
} | ||
|
||
func newtls(certPath, keyPath string) (*tls, error) { | ||
t := &tls{} | ||
if certPath != "" && keyPath != "" { | ||
cert, err := os.Open(certPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = os.Open(keyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rest, err := ioutil.ReadAll(cert) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// test that given keychain is valid | ||
var block *pem.Block | ||
for { | ||
block, rest = pem.Decode(rest) | ||
if block == nil { | ||
return nil, ErrBadCert | ||
} | ||
_, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(rest) == 0 { | ||
break | ||
} | ||
} | ||
t.cert = certPath | ||
t.key = keyPath | ||
} else { | ||
err := generateCert(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return t, nil | ||
} | ||
|
||
func generateCert(t *tls) error { | ||
// good for 1 year | ||
notBefore := time.Now() | ||
notAfter := notBefore.Add(time.Hour * 24 * 365) | ||
|
||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
temp := &x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{"Local Pulse Agent"}, | ||
}, | ||
DNSNames: []string{"localhost"}, | ||
NotBefore: notBefore, | ||
NotAfter: notAfter, | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
priv, err := rsa.GenerateKey(rand.Reader, 1024) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cbytes, err := x509.CreateCertificate(rand.Reader, temp, temp, &priv.PublicKey, priv) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
certPath := os.TempDir() + "/cert.pem" | ||
keyPath := os.TempDir() + "/key.pem" | ||
|
||
cout, err := os.Create(certPath) | ||
if err != nil { | ||
return err | ||
} | ||
pem.Encode(cout, &pem.Block{Type: "CERTIFICATE", Bytes: cbytes}) | ||
cout.Close() | ||
|
||
kout, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
pem.Encode(kout, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}) | ||
kout.Close() | ||
|
||
t.cert = certPath | ||
t.key = keyPath | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters