func main() { flag.Parse() if len(*host) == 0 { log.Fatalf("Missing required -host parameter") } var err error var notBefore time.Time if len(*validFrom) == 0 { notBefore = util.Now() } else { notBefore, err = time.Parse("Jan 2 15:04:05 2006", *validFrom) if err != nil { fmt.Fprintf(os.Stderr, "Failed to parse creation date: %v\n", err) os.Exit(1) } } err = util.GenerateCert(*certFile, *keyFile, *host, *organization, *rsaBits, *isCA, notBefore, *validFor) if err == nil { fmt.Printf("Created %s and %s\n", *certFile, *keyFile) } else { fmt.Fprintln(os.Stderr, "Failed to create cert and key") fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
// Checks that certFile and keyFile exist and are files, and if not, // returns a slice of errors indicating status. // If neither certFile nor keyFile exist, they are automatically created // for host func CreateCertIfNotExists(host, certFile, keyFile string) []error { // check that cert/key both exist, or dont exist, errs := certKeyXor(certFile, keyFile) // Automatically create a new cert if neither files exist if !exist && len(errs) == 0 { logger.Info("Creating certificate %s", certFile) logger.Info("Creating key %s", keyFile) err := util.GenerateCert(certFile, keyFile, host, "Skycoind", 2048, false, util.Now(), 365*24*time.Hour) if err == nil { logger.Info("Created certificate %s for host %s", certFile, host) logger.Info("Created key %s for host %s", keyFile, host) } else { errs = append(errs, err) } } return errs }