File: crl.go

package info (click to toggle)
golang-github-cloudflare-cfssl 1.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,220 kB
  • sloc: asm: 1,936; javascript: 652; makefile: 94; sql: 89; sh: 64; python: 11
file content (105 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Package crl implements the crl command
package crl

import (
	"os"

	"github.com/cloudflare/cfssl/certdb/dbconf"
	certsql "github.com/cloudflare/cfssl/certdb/sql"
	"github.com/cloudflare/cfssl/cli"
	"github.com/cloudflare/cfssl/crl"
	cferr "github.com/cloudflare/cfssl/errors"
	"github.com/cloudflare/cfssl/helpers"
	"github.com/cloudflare/cfssl/log"

	"github.com/jmoiron/sqlx"
)

var crlUsageText = `cfssl crl -- generate a new Certificate Revocation List from Database

Usage of crl:
        cfssl crl

Flags:
`
var crlFlags = []string{"db-config", "ca", "ca-key", "expiry"}

func generateCRL(c cli.Config) (crlBytes []byte, err error) {
	if c.CAFile == "" {
		log.Error("need CA certificate (provide one with -ca)")
		return
	}

	if c.CAKeyFile == "" {
		log.Error("need CA key (provide one with -ca-key)")
		return
	}

	var db *sqlx.DB
	if c.DBConfigFile != "" {
		db, err = dbconf.DBFromConfig(c.DBConfigFile)
		if err != nil {
			return nil, err
		}
	} else {
		log.Error("no Database specified!")
		return nil, err
	}

	dbAccessor := certsql.NewAccessor(db)

	log.Debug("loading CA: ", c.CAFile)
	ca, err := helpers.ReadBytes(c.CAFile)
	if err != nil {
		return nil, err
	}
	log.Debug("loading CA key: ", c.CAKeyFile)
	cakey, err := helpers.ReadBytes(c.CAKeyFile)
	if err != nil {
		return nil, cferr.Wrap(cferr.CertificateError, cferr.ReadFailed, err)
	}

	// Parse the PEM encoded certificate
	issuerCert, err := helpers.ParseCertificatePEM(ca)
	if err != nil {
		return nil, err
	}

	strPassword := os.Getenv("CFSSL_CA_PK_PASSWORD")
	password := []byte(strPassword)
	if strPassword == "" {
		password = nil
	}

	// Parse the key given
	key, err := helpers.ParsePrivateKeyPEMWithPassword(cakey, password)
	if err != nil {
		log.Debugf("malformed private key %v", err)
		return nil, err
	}

	certs, err := dbAccessor.GetRevokedAndUnexpiredCertificates()
	if err != nil {
		return nil, err
	}

	req, err := crl.NewCRLFromDB(certs, issuerCert, key, c.CRLExpiration)
	if err != nil {
		return nil, err
	}

	return req, nil
}

func crlMain(args []string, c cli.Config) (err error) {
	req, err := generateCRL(c)
	if err != nil {
		return err
	}

	cli.PrintCRL(req)
	return
}

// Command assembles the definition of Command 'crl'
var Command = &cli.Command{UsageText: crlUsageText, Flags: crlFlags, Main: crlMain}