2022-04-24 07:25:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021-2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package revoking takes care of handling certificate revocation requests.
|
2021-08-23 18:53:43 +00:00
|
|
|
package revoking
|
|
|
|
|
|
|
|
import (
|
2022-04-23 17:37:42 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/asn1"
|
|
|
|
"fmt"
|
2021-08-23 18:53:43 +00:00
|
|
|
"math/big"
|
2022-04-23 17:37:42 +00:00
|
|
|
"strings"
|
2021-08-23 18:53:43 +00:00
|
|
|
"time"
|
2022-11-30 17:47:18 +00:00
|
|
|
|
|
|
|
"github.com/balacode/go-delta"
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-08-23 18:53:43 +00:00
|
|
|
)
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
var OidCRLReason = asn1.ObjectIdentifier{2, 5, 29, 21}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
const defaultCRLValidity = 24 * time.Hour
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
type CRLReason int
|
|
|
|
|
|
|
|
// CRL reason codes as defined in RFC 5280 section 5.3.1
|
|
|
|
const (
|
|
|
|
CRLReasonUnspecified CRLReason = 0
|
|
|
|
CRLReasonKeyCompromise CRLReason = 1
|
|
|
|
CRLReasonCACompromise CRLReason = 2
|
|
|
|
CRLReasonAffiliationChanged CRLReason = 3
|
|
|
|
CRLReasonSuperseded CRLReason = 4
|
|
|
|
CRLReasonCessationOfOperation CRLReason = 5
|
|
|
|
CRLReasonCertificateHold CRLReason = 6
|
|
|
|
CRLReasonRemoveFromCRL CRLReason = 8
|
|
|
|
CRLReasonPrivilegeWithdrawn CRLReason = 9
|
|
|
|
CRLReasonAACompromise CRLReason = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
var crlReasonNames = map[CRLReason]string{
|
|
|
|
CRLReasonUnspecified: "unspecified",
|
|
|
|
CRLReasonKeyCompromise: "keyCompromise",
|
|
|
|
CRLReasonCACompromise: "CACompromise",
|
|
|
|
CRLReasonAffiliationChanged: "affiliationChanged",
|
|
|
|
CRLReasonSuperseded: "superseded",
|
|
|
|
CRLReasonCessationOfOperation: "cessationOfOperation",
|
|
|
|
CRLReasonCertificateHold: "certificateHold",
|
|
|
|
CRLReasonRemoveFromCRL: "removeFromCRL",
|
|
|
|
CRLReasonPrivilegeWithdrawn: "privilegeWithdrawn",
|
|
|
|
CRLReasonAACompromise: "AACompromise",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r CRLReason) String() string {
|
|
|
|
if reason, ok := crlReasonNames[r]; ok {
|
|
|
|
return reason
|
|
|
|
}
|
|
|
|
|
|
|
|
return crlReasonNames[CRLReasonUnspecified]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r CRLReason) BuildExtension() pkix.Extension {
|
2022-04-24 10:45:22 +00:00
|
|
|
extBytes, _ := asn1.Marshal(r)
|
2022-04-23 17:37:42 +00:00
|
|
|
|
|
|
|
return pkix.Extension{Id: OidCRLReason, Value: extBytes}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseReason takes a reason string and performs a case-insensitive match to a reason code
|
|
|
|
func ParseReason(rs string) CRLReason {
|
|
|
|
for key, name := range crlReasonNames {
|
|
|
|
if strings.EqualFold(name, rs) {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CRLReasonUnspecified
|
|
|
|
}
|
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
type X509Revoking struct {
|
2022-04-23 17:37:42 +00:00
|
|
|
repository Repository
|
|
|
|
crlAlgorithm x509.SignatureAlgorithm
|
|
|
|
crlIssuer *x509.Certificate
|
|
|
|
signer crypto.Signer
|
2022-11-30 17:47:18 +00:00
|
|
|
logger *logrus.Logger
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RevokeCertificate struct {
|
|
|
|
serialNumber *big.Int
|
2022-04-23 17:37:42 +00:00
|
|
|
reason CRLReason
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 10:45:22 +00:00
|
|
|
func NewRevokeCertificate(serialNumber *big.Int, reason CRLReason) *RevokeCertificate {
|
|
|
|
return &RevokeCertificate{
|
|
|
|
serialNumber: serialNumber,
|
|
|
|
reason: reason,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
type CRLInformation struct {
|
2022-11-30 17:47:18 +00:00
|
|
|
CRL []byte // DER encoded CRL
|
|
|
|
Number *big.Int
|
|
|
|
NextUpdate time.Time
|
2022-04-23 17:37:42 +00:00
|
|
|
}
|
2021-08-23 18:53:43 +00:00
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
func (r *X509Revoking) Revoke(revokeCertificate *RevokeCertificate) (*pkix.RevokedCertificate, error) {
|
|
|
|
revoked := &pkix.RevokedCertificate{
|
|
|
|
SerialNumber: revokeCertificate.serialNumber,
|
|
|
|
RevocationTime: time.Now(),
|
|
|
|
Extensions: []pkix.Extension{revokeCertificate.reason.BuildExtension()},
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.repository.StoreRevocation(revoked); err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not store revocation %w", err)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return revoked, nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
func (r *X509Revoking) createCRL() (*CRLInformation, error) {
|
2022-04-23 17:37:42 +00:00
|
|
|
revoked, err := r.repository.RevokedCertificates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get revocation information: %w", err)
|
|
|
|
}
|
2021-08-23 18:53:43 +00:00
|
|
|
|
2022-04-24 10:45:22 +00:00
|
|
|
nextNumber, err := r.repository.NextCRLNumber()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get next CRL number: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
nextUpdate := time.Now().UTC().Add(defaultCRLValidity)
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
list, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{
|
|
|
|
SignatureAlgorithm: r.crlAlgorithm,
|
|
|
|
RevokedCertificates: revoked,
|
2022-04-24 10:45:22 +00:00
|
|
|
Number: nextNumber,
|
2022-11-30 17:47:18 +00:00
|
|
|
ThisUpdate: time.Now().UTC(),
|
|
|
|
NextUpdate: nextUpdate,
|
2022-04-23 17:37:42 +00:00
|
|
|
}, r.crlIssuer, r.signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not sign revocation list: %w", err)
|
|
|
|
}
|
2021-08-23 18:53:43 +00:00
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
err = r.repository.StoreCRL(nextNumber, list)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not store new CRL: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &CRLInformation{CRL: list, Number: nextNumber, NextUpdate: nextUpdate}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *X509Revoking) GetCurrentCRL() (*CRLInformation, error) {
|
|
|
|
return r.GetCRL(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
const maximumCRLLifetime = 6 * time.Hour
|
|
|
|
|
|
|
|
func (r *X509Revoking) GetCRL(number *big.Int) (*CRLInformation, error) {
|
|
|
|
var (
|
|
|
|
crl []byte
|
|
|
|
err error
|
|
|
|
list *x509.RevocationList
|
|
|
|
)
|
|
|
|
|
|
|
|
crl, err = r.repository.LoadCRL(number)
|
|
|
|
if err != nil {
|
|
|
|
r.logger.WithError(err).Warn("could not load CRL")
|
|
|
|
|
|
|
|
if number != nil {
|
|
|
|
return nil, fmt.Errorf("could not load crl number 0x%0x", number)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if crl != nil {
|
|
|
|
list, err = x509.ParseRevocationList(crl)
|
|
|
|
if err != nil {
|
|
|
|
r.logger.WithError(err).Warn("could not parse CRL")
|
|
|
|
}
|
|
|
|
|
|
|
|
stillValid := list.ThisUpdate.Add(maximumCRLLifetime).After(time.Now().UTC())
|
|
|
|
|
|
|
|
if number != nil || stillValid {
|
|
|
|
return &CRLInformation{CRL: crl, Number: list.Number, NextUpdate: list.NextUpdate}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.createCRL()
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
func NewX509Revoking(
|
|
|
|
repo Repository,
|
|
|
|
crlAlgorithm x509.SignatureAlgorithm,
|
|
|
|
issuer *x509.Certificate,
|
|
|
|
signer crypto.Signer,
|
2022-11-30 17:47:18 +00:00
|
|
|
logger *logrus.Logger,
|
2022-04-23 17:37:42 +00:00
|
|
|
) *X509Revoking {
|
2022-11-30 17:47:18 +00:00
|
|
|
return &X509Revoking{
|
|
|
|
repository: repo,
|
|
|
|
crlAlgorithm: crlAlgorithm,
|
|
|
|
crlIssuer: issuer,
|
|
|
|
signer: signer,
|
|
|
|
logger: logger,
|
|
|
|
}
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
type Result struct {
|
2022-11-28 16:10:46 +00:00
|
|
|
CRLData []byte
|
2022-11-30 17:47:18 +00:00
|
|
|
IsDelta bool
|
2022-11-28 16:10:46 +00:00
|
|
|
Number *big.Int
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FetchCRLHandler struct {
|
|
|
|
repositories map[string]*X509Revoking
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFetchCRLHandler(repositories map[string]*X509Revoking) *FetchCRLHandler {
|
|
|
|
return &FetchCRLHandler{repositories: repositories}
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
func (h *FetchCRLHandler) FetchCRL(issuerID string, crlNumber *big.Int) (*Result, error) {
|
|
|
|
var (
|
|
|
|
currentCRL, oldCRL *CRLInformation
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
repo, ok := h.repositories[issuerID]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown issuer ID %s", issuerID)
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
if repo.signer == nil {
|
|
|
|
return nil, fmt.Errorf("key for issuer ID %s is not available", issuerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentCRL, err = repo.GetCurrentCRL()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get CRL for issuer ID %s: %w", issuerID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if crlNumber == nil {
|
|
|
|
return &Result{CRLData: currentCRL.CRL, Number: currentCRL.Number, IsDelta: false}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if crlNumber.Cmp(currentCRL.Number) == 0 {
|
|
|
|
return &Result{CRLData: nil, Number: currentCRL.Number, IsDelta: false}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
oldCRL, err = repo.GetCRL(crlNumber)
|
2022-11-20 17:59:37 +00:00
|
|
|
if err != nil {
|
2022-11-30 17:47:18 +00:00
|
|
|
return &Result{CRLData: currentCRL.CRL, Number: currentCRL.Number, IsDelta: false}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := delta.Make(oldCRL.CRL, currentCRL.CRL)
|
|
|
|
patchBytes := diff.Bytes()
|
|
|
|
|
|
|
|
if len(patchBytes) > len(currentCRL.CRL) {
|
|
|
|
return &Result{CRLData: currentCRL.CRL, Number: currentCRL.Number, IsDelta: false}, nil
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
return &Result{CRLData: patchBytes, Number: currentCRL.Number, IsDelta: true}, nil
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|