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.
|
|
|
|
*/
|
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
package openssl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-04-21 18:01:35 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/asn1"
|
2021-08-23 18:53:43 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-04-24 06:03:51 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/x509/revoking"
|
2021-08-23 18:53:43 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
const TimeSpec = "060102030405Z"
|
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
type indexStatus string
|
|
|
|
|
|
|
|
const (
|
2022-04-21 18:01:35 +00:00
|
|
|
certificateValid indexStatus = "V"
|
|
|
|
certificateRevoked indexStatus = "R"
|
|
|
|
certificateExpired indexStatus = "E"
|
2021-08-23 18:53:43 +00:00
|
|
|
)
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
const (
|
|
|
|
posStatus int = iota
|
|
|
|
posExpiry
|
|
|
|
posRevocation
|
|
|
|
posSerial
|
|
|
|
posFilename
|
|
|
|
posSubject
|
|
|
|
)
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
// An indexEntry represents a line in an openssl ca compatible index.txt file
|
|
|
|
// a format specification is available at https://pki-tutorial.readthedocs.io/en/latest/cadb.html
|
2021-08-23 18:53:43 +00:00
|
|
|
type indexEntry struct {
|
|
|
|
statusFlag indexStatus
|
|
|
|
expiresAt time.Time
|
2022-04-21 18:01:35 +00:00
|
|
|
revokedAt *time.Time
|
2022-04-23 17:37:42 +00:00
|
|
|
revocationReason revoking.CRLReason
|
2021-08-23 18:53:43 +00:00
|
|
|
serialNumber *big.Int
|
|
|
|
fileName string
|
|
|
|
certificateSubjectDN string
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
func (ie *indexEntry) String() string {
|
|
|
|
var revoked, fileName string
|
|
|
|
|
|
|
|
if ie.revokedAt != nil {
|
|
|
|
revoked = fmt.Sprintf("%s,%s", ie.revokedAt.Format(TimeSpec), ie.revocationReason)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ie.fileName == "" {
|
|
|
|
fileName = "unknown"
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
2022-04-21 18:01:35 +00:00
|
|
|
|
|
|
|
return strings.Join([]string{
|
|
|
|
string(ie.statusFlag),
|
|
|
|
ie.expiresAt.Format(TimeSpec),
|
|
|
|
revoked,
|
|
|
|
strings.ToUpper(ie.serialNumber.Text(16)),
|
|
|
|
fileName,
|
|
|
|
ie.certificateSubjectDN, // this is not 100% compatible with openssl that uses a non-RFC syntax
|
|
|
|
}, "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Repository stores information about signed and revoked certificates
|
|
|
|
// in an openssl index.txt compatible file.
|
|
|
|
//
|
|
|
|
// A reference for the file format can be found at
|
|
|
|
// https://pki-tutorial.readthedocs.io/en/latest/cadb.html.
|
|
|
|
type Repository struct {
|
|
|
|
indexFileName string
|
|
|
|
lock sync.Locker
|
|
|
|
entries []indexEntry
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
func (ie *indexEntry) markRevoked(revocationTime time.Time, reason revoking.CRLReason) {
|
2022-04-21 18:01:35 +00:00
|
|
|
if ie.statusFlag == certificateValid {
|
|
|
|
ie.statusFlag = certificateRevoked
|
|
|
|
ie.revokedAt = &revocationTime
|
|
|
|
ie.revocationReason = reason
|
|
|
|
}
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
func (r *Repository) findEntry(number *big.Int) (*indexEntry, error) {
|
|
|
|
if number == nil {
|
|
|
|
return nil, errors.New("serial number must not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range r.entries {
|
|
|
|
if entry.serialNumber.Cmp(number) == 0 {
|
|
|
|
return &entry, nil
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-21 18:01:35 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CannotRevokeUnknown struct {
|
|
|
|
Serial *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CannotRevokeUnknown) Error() string {
|
|
|
|
return fmt.Sprintf("cannot revoke unknown certificate with serial number %s", c.Serial)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StoreRevocation records information about a revoked certificate.
|
2022-04-21 18:01:35 +00:00
|
|
|
func (r *Repository) StoreRevocation(revoked *pkix.RevokedCertificate) error {
|
2021-08-23 18:53:43 +00:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
err := r.loadIndex()
|
2021-08-23 18:53:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
entry, err := r.findEntry(revoked.SerialNumber)
|
2021-08-23 18:53:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
if entry == nil {
|
|
|
|
return CannotRevokeUnknown{Serial: revoked.SerialNumber}
|
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
reason := revoking.CRLReasonUnspecified
|
2022-04-21 18:01:35 +00:00
|
|
|
|
|
|
|
for _, ext := range revoked.Extensions {
|
2022-04-23 17:37:42 +00:00
|
|
|
if ext.Id.Equal(revoking.OidCRLReason) {
|
2022-04-21 18:01:35 +00:00
|
|
|
_, err := asn1.Unmarshal(ext.Value, &reason)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.markRevoked(revoked.RevocationTime, reason)
|
|
|
|
|
|
|
|
err = r.writeIndex()
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreCertificate records information about a signed certificate.
|
2022-04-21 18:01:35 +00:00
|
|
|
func (r *Repository) StoreCertificate(signed *x509.Certificate) error {
|
|
|
|
var err error
|
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
err = r.loadIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := r.findEntry(signed.SerialNumber)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry != nil {
|
|
|
|
return fmt.Errorf("certificate with serial %s is already in the index", signed.SerialNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
status := certificateValid
|
|
|
|
|
|
|
|
if signed.NotAfter.Before(time.Now().UTC()) {
|
|
|
|
status = certificateExpired
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.addIndexEntry(&indexEntry{
|
|
|
|
statusFlag: status,
|
|
|
|
expiresAt: signed.NotAfter,
|
|
|
|
serialNumber: signed.SerialNumber,
|
|
|
|
certificateSubjectDN: signed.Subject.String(), // not openssl compatible
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
func (r *Repository) RevokedCertificates() ([]pkix.RevokedCertificate, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
|
|
|
err = r.loadIndex()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]pkix.RevokedCertificate, 0)
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-23 17:37:42 +00:00
|
|
|
for _, entry := range r.entries {
|
|
|
|
if entry.revokedAt != nil {
|
|
|
|
result = append(result, pkix.RevokedCertificate{
|
|
|
|
SerialNumber: entry.serialNumber,
|
|
|
|
RevocationTime: *entry.revokedAt,
|
|
|
|
Extensions: []pkix.Extension{entry.revocationReason.BuildExtension()},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
func (r *Repository) loadIndex() error {
|
2022-04-24 07:25:04 +00:00
|
|
|
const reservedEntries = 100 // use 100 as base capacity to avoid unnecessary array resizing
|
|
|
|
|
|
|
|
entries := make([]indexEntry, 0, reservedEntries)
|
2022-04-21 18:01:35 +00:00
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
f, err := os.Open(r.indexFileName)
|
2022-04-21 18:01:35 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("could not load index from %s: %w", r.indexFileName, err)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
if f == nil {
|
|
|
|
r.entries = entries
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = f.Close() }()
|
2021-08-23 18:53:43 +00:00
|
|
|
|
|
|
|
indexScanner := bufio.NewScanner(f)
|
|
|
|
for indexScanner.Scan() {
|
2022-04-21 18:01:35 +00:00
|
|
|
indexEntry, err := r.newIndexEntryFromLine(indexScanner.Text())
|
2021-08-23 18:53:43 +00:00
|
|
|
if err != nil {
|
2022-04-21 18:01:35 +00:00
|
|
|
return err
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
entries = append(entries, *indexEntry)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2021-08-23 18:53:43 +00:00
|
|
|
if err := indexScanner.Err(); err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return fmt.Errorf("could not scan index file: %w", err)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
r.entries = entries
|
|
|
|
|
|
|
|
return nil
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
func (r *Repository) writeIndex() error {
|
|
|
|
f, err := os.OpenFile(r.indexFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create index file %s: %w", r.indexFileName, err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
defer func(f *os.File) {
|
|
|
|
_ = f.Close()
|
|
|
|
}(f)
|
|
|
|
|
|
|
|
w := bufio.NewWriter(f)
|
|
|
|
|
|
|
|
for i, entry := range r.entries {
|
|
|
|
_, err = w.WriteString(entry.String())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write entry for serial %s: %w", entry.serialNumber, err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
if i < len(r.entries)-1 {
|
|
|
|
_, err = w.WriteString("\n")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write linebreak: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
err = w.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write to %s: %w", r.indexFileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) addIndexEntry(ie *indexEntry) error {
|
|
|
|
r.entries = append(r.entries, *ie)
|
|
|
|
|
|
|
|
err := r.writeIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
func (r *Repository) newIndexEntryFromLine(text string) (*indexEntry, error) {
|
2022-04-24 07:25:04 +00:00
|
|
|
const expectedFieldNumber = 6
|
2022-04-21 18:01:35 +00:00
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
var err error
|
2021-08-23 18:53:43 +00:00
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
fields := strings.SplitN(text, "\t", expectedFieldNumber)
|
2021-08-23 18:53:43 +00:00
|
|
|
if len(fields) != expectedFieldNumber {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"unexpected number of fields %d instead of %d",
|
|
|
|
len(fields),
|
|
|
|
expectedFieldNumber,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
expirationParsed, err := time.Parse(TimeSpec, fields[posExpiry])
|
2021-08-23 18:53:43 +00:00
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not parse expiration time %s: %w", fields[posExpiry], err)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 07:25:04 +00:00
|
|
|
var (
|
|
|
|
revocationTimeParsed time.Time
|
|
|
|
revocationReason revoking.CRLReason
|
|
|
|
)
|
|
|
|
|
|
|
|
if fields[posRevocation] != "" {
|
|
|
|
timeString, reasonText, found := strings.Cut(fields[revocationReason], ",")
|
|
|
|
if found {
|
|
|
|
revocationReason = revoking.ParseReason(reasonText)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
revocationTimeParsed, err = time.Parse(TimeSpec, timeString)
|
2021-08-23 18:53:43 +00:00
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not parse revocation time %s: %w", timeString, err)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serialParsed := new(big.Int)
|
2022-04-24 07:25:04 +00:00
|
|
|
if _, ok := serialParsed.SetString(fields[posSerial], 16); !ok {
|
2021-08-23 18:53:43 +00:00
|
|
|
return nil, fmt.Errorf("could not parse serial number %s", fields[3])
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNameParsed := "unknown"
|
2022-04-24 07:25:04 +00:00
|
|
|
|
|
|
|
if fields[posFilename] != "" {
|
|
|
|
certificateFile := path.Join(path.Dir(r.indexFileName), fields[posFilename])
|
|
|
|
|
|
|
|
_, err = os.Stat(certificateFile)
|
2022-04-21 18:01:35 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not check certificate file %s: %w", certificateFile, err)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
|
|
|
fileNameParsed = fields[posFilename]
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &indexEntry{
|
2022-04-24 07:25:04 +00:00
|
|
|
statusFlag: indexStatus(fields[posStatus]),
|
2021-08-23 18:53:43 +00:00
|
|
|
expiresAt: expirationParsed,
|
2022-04-21 18:01:35 +00:00
|
|
|
revokedAt: &revocationTimeParsed,
|
2021-08-23 18:53:43 +00:00
|
|
|
revocationReason: revocationReason,
|
|
|
|
serialNumber: serialParsed,
|
|
|
|
fileName: fileNameParsed,
|
2022-04-24 07:25:04 +00:00
|
|
|
certificateSubjectDN: fields[posSubject],
|
2021-08-23 18:53:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
func NewFileRepository(baseDirectory string) (*Repository, error) {
|
2021-08-23 18:53:43 +00:00
|
|
|
err := os.Chdir(baseDirectory)
|
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return nil, fmt.Errorf("could not change to base directory %s: %w", baseDirectory, err)
|
2021-08-23 18:53:43 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-21 18:01:35 +00:00
|
|
|
return &Repository{
|
2021-08-23 18:53:43 +00:00
|
|
|
indexFileName: path.Join(baseDirectory, "index.txt"),
|
|
|
|
lock: &sync.Mutex{},
|
|
|
|
}, nil
|
|
|
|
}
|