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
3 changes: 3 additions & 0 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type StorageGetter interface {
GetCertificate(string) ([]byte, error)
GetCertificateByShortSerial(string) ([]byte, error)
GetCertificateStatus(string) (CertificateStatus, error)
AlreadyDeniedCSR([]string) (bool, error)
}

type StorageAdder interface {
Expand All @@ -105,6 +106,8 @@ type StorageAdder interface {
MarkCertificateRevoked(serial string, ocspResponse []byte, reasonCode int) error

AddCertificate([]byte) (string, error)

AddDeniedCSR([]string) error
}

// StorageAuthority interface represents a simple key/value
Expand Down
6 changes: 6 additions & 0 deletions core/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,9 @@ type Crl struct {
// crl: The encoded and signed CRL.
Crl string `db:"crl"`
}

type DeniedCsr struct {
ID int `db:"id"`

Names string `db:"names"`
}
10 changes: 10 additions & 0 deletions ra/registration-authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ func (ra *RegistrationAuthorityImpl) NewCertificate(req core.CertificateRequest,
return emptyCert, err
}

csrPreviousDenied, err := ra.SA.AlreadyDeniedCSR(append(csr.DNSNames, csr.Subject.CommonName))
if err != nil {
return emptyCert, err
}
if csrPreviousDenied {
ra.log.Audit(fmt.Sprintf("CSR for names %v was previously revoked/denied", csr.DNSNames))
err = core.UnauthorizedError("CSR has already been revoked/denied")
return emptyCert, err
}

// Gather authorized domains from the referenced authorizations
authorizedDomains := map[string]bool{}
now := time.Now()
Expand Down
79 changes: 79 additions & 0 deletions rpc/rpc-wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const (
MethodUpdatePendingAuthorization = "UpdatePendingAuthorization" // SA
MethodFinalizeAuthorization = "FinalizeAuthorization" // SA
MethodAddCertificate = "AddCertificate" // SA
MethodAddDeniedCSR = "AddDeniedCSR" // SA
MethodAlreadyDeniedCSR = "AlreadyDeniedCSR" // SA
)

// RegistrationAuthorityClient / Server
Expand Down Expand Up @@ -553,6 +555,42 @@ func NewStorageAuthorityServer(serverQueue string, channel *amqp.Channel, impl c
return nil
})

rpc.Handle(MethodAddDeniedCSR, func(req []byte) []byte {
var csrReq struct {
Names []string
}

err := json.Unmarshal(req, csrReq)
if err != nil {
return nil
}

err = impl.AddDeniedCSR(csrReq.Names)
return nil
})

rpc.Handle(MethodAlreadyDeniedCSR, func(req []byte) []byte {
var csrReq struct {
Names []string
}

err := json.Unmarshal(req, csrReq)
if err != nil {
return nil
}

exists, err := impl.AlreadyDeniedCSR(csrReq.Names)
if err != nil {
return nil
}

if exists {
return []byte{1}
} else {
return []byte{0}
}
})

return rpc
}

Expand Down Expand Up @@ -692,3 +730,44 @@ func (cac StorageAuthorityClient) AddCertificate(cert []byte) (id string, err er
id = string(response)
return
}

func (cac StorageAuthorityClient) AddDeniedCSR(names []string) (err error) {
var sliceReq struct {
Names []string
}
sliceReq.Names = names

data, err := json.Marshal(sliceReq)
if err != nil {
return
}

_, err = cac.rpc.DispatchSync(MethodAddDeniedCSR, data)
return
}

func (cac StorageAuthorityClient) AlreadyDeniedCSR(names []string) (exists bool, err error) {
var sliceReq struct {
Names []string
}
sliceReq.Names = names

data, err := json.Marshal(sliceReq)
if err != nil {
return
}

response, err := cac.rpc.DispatchSync(MethodAlreadyDeniedCSR, data)
if err != nil || len(response) == 0 {
err = errors.New("AddDeniedCSR RPC failed") // XXX
return
}

switch response[0] {
case 0:
exists = false
case 1:
exists = true
}
return
}
55 changes: 54 additions & 1 deletion sa/storage-authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"time"

gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
Expand Down Expand Up @@ -147,6 +149,7 @@ func (ssa *SQLStorageAuthority) InitTables() (err error) {
ssa.dbMap.AddTableWithName(core.CertificateStatus{}, "certificateStatus").SetKeys(false, "Serial").SetVersionCol("LockCol")
ssa.dbMap.AddTableWithName(core.OcspResponse{}, "ocspResponses").SetKeys(true, "ID")
ssa.dbMap.AddTableWithName(core.Crl{}, "crls").SetKeys(false, "Serial")
ssa.dbMap.AddTableWithName(core.DeniedCsr{}, "deniedCsrs").SetKeys(true, "ID").ColMap("Names").SetUnique(true)

err = ssa.dbMap.CreateTablesIfNotExists()
return
Expand Down Expand Up @@ -238,6 +241,17 @@ func (ssa *SQLStorageAuthority) DumpTables() error {
fmt.Printf("%+v\n", c)
}

fmt.Printf("\n----- deniedCsrs -----\n")
var dCsrs []core.DeniedCsr
_, err = tx.Select(&dCsrs, "SELECT * FROM deniedCsrs")
if err != nil {
tx.Rollback()
return err
}
for _, c := range dCsrs {
fmt.Printf("%+v\n", c)
}

err = tx.Commit()
return err
}
Expand Down Expand Up @@ -583,7 +597,7 @@ func (ssa *SQLStorageAuthority) FinalizeAuthorization(authz core.Authorization)
return
}

tx.Commit()
err = tx.Commit()
return
}

Expand Down Expand Up @@ -632,3 +646,42 @@ func (ssa *SQLStorageAuthority) AddCertificate(certDER []byte) (digest string, e
err = tx.Commit()
return
}

func (ssa *SQLStorageAuthority) AddDeniedCSR(names []string) (err error) {
sort.Strings(names)
deniedCSR := &core.DeniedCsr{Names: strings.ToLower(strings.Join(names, ","))}

tx, err := ssa.dbMap.Begin()
if err != nil {
return
}

err = tx.Insert(deniedCSR)
if err != nil {
tx.Rollback()
return
}

err = tx.Commit()
return
}

func (ssa *SQLStorageAuthority) AlreadyDeniedCSR(names []string) (already bool, err error) {
sort.Strings(names)

var denied int64
err = ssa.dbMap.SelectOne(
&denied,
"SELECT count(*) FROM deniedCsrs WHERE names = :names",
map[string]interface{} {"names": strings.ToLower(strings.Join(names, ","))},
)
if err != nil {
return
}
if denied > 0 {
already = true
}

return
}

26 changes: 26 additions & 0 deletions sa/storage-authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
package sa

import (
"crypto/x509"
"crypto/x509/pkix"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"net/url"
"time"
Expand Down Expand Up @@ -165,3 +169,25 @@ func TestGetCertificateByShortSerial(t *testing.T) {
_, err = sa.GetCertificateByShortSerial("01020304050607080102030405060708")
test.AssertError(t, err, "Should've failed on too-long serial")
}

func TestDeniedCSR(t *testing.T) {
key, _ := rsa.GenerateKey(rand.Reader, 512)
template := &x509.CertificateRequest{
Subject: pkix.Name{CommonName: "google.com"},
DNSNames: []string{"badguys.com", "reallybad.com"},
}
csrBytes, _ := x509.CreateCertificateRequest(rand.Reader, template, key)
csr, _ := x509.ParseCertificateRequest(csrBytes)

sa := initSA(t)
exists, err := sa.AlreadyDeniedCSR(append(csr.DNSNames, csr.Subject.CommonName))
test.AssertNotError(t, err, "AlreadyDeniedCSR failed")
test.Assert(t, !exists, "Found non-existent CSR")

err = sa.AddDeniedCSR(append(csr.DNSNames, csr.Subject.CommonName))
test.AssertNotError(t, err, "Couldn't add the denied CSR to the DB")

exists, err = sa.AlreadyDeniedCSR(append(csr.DNSNames, csr.Subject.CommonName))
test.AssertNotError(t, err, "AlreadyDeniedCSR failed")
test.Assert(t, exists, "Couldn't find denied CSR in DB")
}