Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions ca/certificate-authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func dupeNames(names []string) bool {
return false
}

func (ca *CertificateAuthorityImpl) RevokeCertificate(serial string) (err error) {
func (ca *CertificateAuthorityImpl) RevokeCertificate(serial string, reasonCode int) (err error) {
certDER, err := ca.SA.GetCertificate(serial)
if err != nil {
return err
Expand All @@ -186,21 +186,17 @@ func (ca *CertificateAuthorityImpl) RevokeCertificate(serial string) (err error)
return err
}

// Per https://tools.ietf.org/html/rfc5280, CRLReason 0 is "unspecified."
// TODO: Add support for specifying reason.
reason := 0

signRequest := ocsp.SignRequest{
Certificate: cert,
Status: string(core.OCSPStatusRevoked),
Reason: reason,
Reason: reasonCode,
RevokedAt: time.Now(),
}
ocspResponse, err := ca.OCSPSigner.Sign(signRequest)
if err != nil {
return err
}
err = ca.SA.MarkCertificateRevoked(serial, ocspResponse, reason)
err = ca.SA.MarkCertificateRevoked(serial, ocspResponse, reasonCode)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion ca/certificate-authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func TestRevoke(t *testing.T) {
cert, err := x509.ParseCertificate(certObj.DER)
test.AssertNotError(t, err, "Certificate failed to parse")
serialString := core.SerialToString(cert.SerialNumber)
err = ca.RevokeCertificate(serialString)
err = ca.RevokeCertificate(serialString, 0)
test.AssertNotError(t, err, "Revocation failed")

status, err := storageAuthority.GetCertificateStatus(serialString)
Expand Down
242 changes: 242 additions & 0 deletions cmd/admin-revoker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// Copyright 2015 ISRG. All rights reserved
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package main

import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"

"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/codegangsta/cli"

// Load both drivers to allow configuring either
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"

"github.com/letsencrypt/boulder/cmd"
"github.com/letsencrypt/boulder/core"
blog "github.com/letsencrypt/boulder/log"
"github.com/letsencrypt/boulder/rpc"
"github.com/letsencrypt/boulder/sa"

gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
)

var reasons map[int]string = map[int]string{
0: "unspecified",
1: "keyCompromise",
2: "cACompromise",
3: "affiliationChanged",
4: "superseded",
5: "cessationOfOperation",
6: "certificateHold",
// 7 is unused
8: "removeFromCRL", // needed?
9: "privilegeWithdrawn",
10: "aAcompromise",
}

func loadConfig(c *cli.Context) (config cmd.Config, err error) {
configFileName := c.GlobalString("config")
configJSON, err := ioutil.ReadFile(configFileName)
if err != nil {
return
}

err = json.Unmarshal(configJSON, &config)
return
}

func setupContext(context *cli.Context) (rpc.CertificateAuthorityClient, *blog.AuditLogger, *gorp.DbMap) {
c, err := loadConfig(context)
cmd.FailOnError(err, "Failed to load Boulder configuration")

stats, err := statsd.NewClient(c.Statsd.Server, c.Statsd.Prefix)
cmd.FailOnError(err, "Couldn't connect to statsd")

auditlogger, err := blog.Dial(c.Syslog.Network, c.Syslog.Server, c.Syslog.Tag, stats)
cmd.FailOnError(err, "Could not connect to Syslog")
blog.SetAuditLogger(auditlogger)

ch := cmd.AmqpChannel(c.AMQP.Server)

cac, err := rpc.NewCertificateAuthorityClient(c.AMQP.CA.Client, c.AMQP.CA.Server, ch)
cmd.FailOnError(err, "Unable to create CA client")

dbMap, err := sa.NewDbMap(c.Revoker.DBDriver, c.Revoker.DBName)
cmd.FailOnError(err, "Couldn't setup database connection")

return cac, auditlogger, dbMap
}

func addDeniedNames(tx *gorp.Transaction, names []string) (err error) {
sort.Strings(names)
deniedCSR := &core.DeniedCsr{Names: strings.ToLower(strings.Join(names, ","))}

err = tx.Insert(deniedCSR)
return
}

func revokeBySerial(serial string, reasonCode int, deny bool, cac rpc.CertificateAuthorityClient, auditlogger *blog.AuditLogger, tx *gorp.Transaction) (err error) {
if reasonCode < 0 || reasonCode == 7 || reasonCode > 10 {
panic(fmt.Sprintf("Invalid reason code: %d", reasonCode))
}

if deny {
// Retrieve DNS names associated with serial
var certificate core.Certificate
err = tx.SelectOne(&certificate, "SELECT * FROM certificates WHERE serial = :serial",
map[string]interface{}{"serial": serial})
if err != nil {
return
}
var cert *x509.Certificate
cert, err = x509.ParseCertificate(certificate.DER)
if err != nil {
return
}
err = addDeniedNames(tx, append(cert.DNSNames, cert.Subject.CommonName))
if err != nil {
return
}
}

err = cac.RevokeCertificate(serial, reasonCode)
if err != nil {
return
}

auditlogger.Info(fmt.Sprintf("Revoked certificate %s with reason '%s'", serial, reasons[reasonCode]))
return
}

func revokeByReg(regID int, reasonCode int, deny bool, cac rpc.CertificateAuthorityClient, auditlogger *blog.AuditLogger, tx *gorp.Transaction) (err error) {
_, err = tx.Get(core.Registration{}, regID)
if err != nil {
return
}

var certs []core.Certificate
_, err = tx.Select(certs, "SELECT serial FROM certificates WHERE registrationID = :regID", map[string]interface{}{"regID": regID})
if err != nil {
return
}

for _, cert := range certs {
err = revokeBySerial(cert.Serial, reasonCode, deny, cac, auditlogger, tx)
if err != nil {
return
}
}

return
}

var version string = "0.0.1"

func main() {
app := cli.NewApp()
app.Name = "admin-revoker"
app.Version = version

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Value: "config.json",
EnvVar: "BOULDER_CONFIG",
Usage: "Path to Boulder JSON configuration file",
},
cli.BoolFlag{
Name: "deny-future",
Usage: "Add certificate DNS names to the denied list",
},
}
app.Commands = []cli.Command{
{
Name: "serial-revoke",
Usage: "Revoke a single certificate by the hex serial number",
Action: func(c *cli.Context) {
// 1: serial, 2: reasonCode (3: deny flag)
serial := c.Args().First()
reasonCode, err := strconv.Atoi(c.Args().Get(1))
cmd.FailOnError(err, "Reason code argument must be a integer")
deny := c.GlobalBool("deny")

cac, auditlogger, dbMap := setupContext(c)

tx, err := dbMap.Begin()
if err != nil {
tx.Rollback()
}
cmd.FailOnError(err, "Couldn't begin transaction")

err = revokeBySerial(serial, reasonCode, deny, cac, auditlogger, tx)
if err != nil {
tx.Rollback()
}
cmd.FailOnError(err, "Couldn't revoke certificate")

err = tx.Commit()
cmd.FailOnError(err, "Couldn't cleanly close transaction")
},
},
{
Name: "reg-revoke",
Usage: "Revoke all certificates associated with a registration ID",
Action: func(c *cli.Context) {
// 1: registration ID, 2: reasonCode (3: deny flag)
regID, err := strconv.Atoi(c.Args().First())
cmd.FailOnError(err, "Registration ID argument must be a integer")
reasonCode, err := strconv.Atoi(c.Args().Get(1))
cmd.FailOnError(err, "Reason code argument must be a integer")
deny := c.GlobalBool("deny")

cac, auditlogger, dbMap := setupContext(c)
// AUDIT[ Error Conditions ] 9cc4d537-8534-4970-8665-4b382abe82f3
defer auditlogger.AuditPanic()

tx, err := dbMap.Begin()
if err != nil {
tx.Rollback()
}
cmd.FailOnError(err, "Couldn't begin transaction")

err = revokeByReg(regID, reasonCode, deny, cac, auditlogger, tx)
if err != nil {
tx.Rollback()
}
cmd.FailOnError(err, "Couldn't revoke certificate")

err = tx.Commit()
cmd.FailOnError(err, "Couldn't cleanly close transaction")
},
},
{
Name: "list-reasons",
Usage: "List all revocation reason codes",
Action: func(c *cli.Context) {
var codes []int
for k, _ := range reasons {
codes = append(codes, k)
}
sort.Ints(codes)
fmt.Printf("Revocation reason codes\n-----------------------\n\n")
for _, k := range codes {
fmt.Printf("%d: %s\n", k, reasons[k])
}
},
},
}

err := app.Run(os.Args)
cmd.FailOnError(err, "Failed to run application")
}
5 changes: 5 additions & 0 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ type Config struct {
Tag string
}

Revoker struct {
DBDriver string
DBName string
}

Mail struct {
Server string
Port string
Expand Down
4 changes: 1 addition & 3 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type ValidationAuthority interface {
type CertificateAuthority interface {
// [RegistrationAuthority]
IssueCertificate(x509.CertificateRequest, int64) (Certificate, error)
RevokeCertificate(serial string) error
RevokeCertificate(string, int) error
}

type PolicyAuthority interface {
Expand Down Expand Up @@ -107,8 +107,6 @@ type StorageAdder interface {
MarkCertificateRevoked(serial string, ocspResponse []byte, reasonCode int) error

AddCertificate([]byte, int64) (string, error)

AddDeniedCSR([]string) error
}

// StorageAuthority interface represents a simple key/value
Expand Down
2 changes: 1 addition & 1 deletion ra/registration-authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (ra *RegistrationAuthorityImpl) UpdateAuthorization(base core.Authorization

func (ra *RegistrationAuthorityImpl) RevokeCertificate(cert x509.Certificate) error {
serialString := core.SerialToString(cert.SerialNumber)
err := ra.CA.RevokeCertificate(serialString)
err := ra.CA.RevokeCertificate(serialString, 0)

// AUDIT[ Revocation Requests ] 4e85d791-09c0-4ab3-a837-d3d67e945134
if err != nil {
Expand Down
Loading