2019-03-11 15:54:35 +00:00
|
|
|
package cmd // import "github.com/xenolf/lego/cmd"
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"github.com/xenolf/lego/certcrypto"
|
|
|
|
"github.com/xenolf/lego/lego"
|
|
|
|
"github.com/xenolf/lego/log"
|
|
|
|
"github.com/xenolf/lego/registration"
|
|
|
|
)
|
|
|
|
|
|
|
|
const filePerm os.FileMode = 0600
|
|
|
|
|
|
|
|
func setup(ctx *cli.Context, accountsStorage *AccountsStorage) (*Account, *lego.Client) {
|
2019-02-08 18:29:28 +00:00
|
|
|
keyType := getKeyType(ctx)
|
|
|
|
privateKey := accountsStorage.GetPrivateKey(keyType)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
var account *Account
|
|
|
|
if accountsStorage.ExistsAccountFilePath() {
|
|
|
|
account = accountsStorage.LoadAccount(privateKey)
|
|
|
|
} else {
|
|
|
|
account = &Account{Email: accountsStorage.GetUserID(), key: privateKey}
|
|
|
|
}
|
|
|
|
|
2019-02-08 18:29:28 +00:00
|
|
|
client := newClient(ctx, account, keyType)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
return account, client
|
|
|
|
}
|
|
|
|
|
2019-02-08 18:29:28 +00:00
|
|
|
func newClient(ctx *cli.Context, acc registration.User, keyType certcrypto.KeyType) *lego.Client {
|
2018-12-06 21:50:17 +00:00
|
|
|
config := lego.NewConfig(acc)
|
|
|
|
config.CADirURL = ctx.GlobalString("server")
|
2019-01-09 07:29:17 +00:00
|
|
|
|
|
|
|
config.Certificate = lego.CertificateConfig{
|
2019-02-08 18:29:28 +00:00
|
|
|
KeyType: keyType,
|
2019-01-09 07:29:17 +00:00
|
|
|
Timeout: time.Duration(ctx.GlobalInt("cert.timeout")) * time.Second,
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
config.UserAgent = fmt.Sprintf("lego-cli/%s", ctx.App.Version)
|
|
|
|
|
|
|
|
if ctx.GlobalIsSet("http-timeout") {
|
|
|
|
config.HTTPClient.Timeout = time.Duration(ctx.GlobalInt("http-timeout")) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := lego.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not create client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.GetExternalAccountRequired() && !ctx.GlobalIsSet("eab") {
|
|
|
|
log.Fatal("Server requires External Account Binding. Use --eab with --kid and --hmac.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
// getKeyType the type from which private keys should be generated
|
|
|
|
func getKeyType(ctx *cli.Context) certcrypto.KeyType {
|
|
|
|
keyType := ctx.GlobalString("key-type")
|
|
|
|
switch strings.ToUpper(keyType) {
|
|
|
|
case "RSA2048":
|
|
|
|
return certcrypto.RSA2048
|
|
|
|
case "RSA4096":
|
|
|
|
return certcrypto.RSA4096
|
|
|
|
case "RSA8192":
|
|
|
|
return certcrypto.RSA8192
|
|
|
|
case "EC256":
|
|
|
|
return certcrypto.EC256
|
|
|
|
case "EC384":
|
|
|
|
return certcrypto.EC384
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Fatalf("Unsupported KeyType: %s", keyType)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEmail(ctx *cli.Context) string {
|
|
|
|
email := ctx.GlobalString("email")
|
|
|
|
if len(email) == 0 {
|
|
|
|
log.Fatal("You have to pass an account (email address) to the program using --email or -m")
|
|
|
|
}
|
|
|
|
return email
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNonExistingFolder(path string) error {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return os.MkdirAll(path, 0700)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readCSRFile(filename string) (*x509.CertificateRequest, error) {
|
|
|
|
bytes, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
raw := bytes
|
|
|
|
|
|
|
|
// see if we can find a PEM-encoded CSR
|
|
|
|
var p *pem.Block
|
|
|
|
rest := bytes
|
|
|
|
for {
|
|
|
|
// decode a PEM block
|
|
|
|
p, rest = pem.Decode(rest)
|
|
|
|
|
|
|
|
// did we fail?
|
|
|
|
if p == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// did we get a CSR?
|
|
|
|
if p.Type == "CERTIFICATE REQUEST" {
|
|
|
|
raw = p.Bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no PEM-encoded CSR
|
|
|
|
// assume we were given a DER-encoded ASN.1 CSR
|
|
|
|
// (if this assumption is wrong, parsing these bytes will fail)
|
|
|
|
return x509.ParseCertificateRequest(raw)
|
|
|
|
}
|