|
| 1 | +// Copyright certyaml authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package certyaml |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "crypto/rand" |
| 20 | + "crypto/x509/pkix" |
| 21 | + "encoding/pem" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "time" |
| 25 | +) |
| 26 | + |
| 27 | +// CRL defines properties for generating CRL files. |
| 28 | +type CRL struct { |
| 29 | + // ThisUpdate is the issue date of this CRL. |
| 30 | + // Default value is current time (when value is nil). |
| 31 | + ThisUpdate *time.Time |
| 32 | + |
| 33 | + // NextUpdate indicates the date by which the next CRL will be issued. |
| 34 | + // Default value is ThisUpdate + one week (when value is nil). |
| 35 | + NextUpdate *time.Time |
| 36 | + |
| 37 | + // Revoked is the list of Certificates that will be included in the CRL. |
| 38 | + // All Certificates must be issued by the same Issuer. |
| 39 | + // Self-signed certificates cannot be added. |
| 40 | + Revoked []*Certificate |
| 41 | +} |
| 42 | + |
| 43 | +// Add appends a Certificate to CRL list. |
| 44 | +// All Certificates must be issued by the same Issuer. |
| 45 | +// Self-signed certificates cannot be added. |
| 46 | +// Error is not nil if adding fails. |
| 47 | +func (crl *CRL) Add(cert *Certificate) error { |
| 48 | + if cert.Issuer == nil { |
| 49 | + return fmt.Errorf("cannot revoke self-signed certificate: %s", cert.Subject) |
| 50 | + } |
| 51 | + if len(crl.Revoked) > 0 && (crl.Revoked[0].Issuer != cert.Issuer) { |
| 52 | + return fmt.Errorf("CRL can contain certificates for single issuer only") |
| 53 | + } |
| 54 | + crl.Revoked = append(crl.Revoked, cert) |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// DER returns the CRL as DER buffer. |
| 59 | +// Error is not nil if generation fails. |
| 60 | +func (crl *CRL) DER() (crlBytes []byte, err error) { |
| 61 | + if len(crl.Revoked) == 0 { |
| 62 | + return nil, fmt.Errorf("certificates have not been added to CRL") |
| 63 | + } |
| 64 | + |
| 65 | + effectiveRevocationTime := time.Now() |
| 66 | + if crl.ThisUpdate != nil { |
| 67 | + effectiveRevocationTime = *crl.ThisUpdate |
| 68 | + } |
| 69 | + |
| 70 | + week := 24 * 7 * time.Hour |
| 71 | + effectiveExpiry := effectiveRevocationTime.UTC().Add(week) |
| 72 | + if crl.NextUpdate != nil { |
| 73 | + effectiveExpiry = *crl.NextUpdate |
| 74 | + } |
| 75 | + |
| 76 | + issuer := crl.Revoked[0].Issuer |
| 77 | + |
| 78 | + var revokedCerts []pkix.RevokedCertificate |
| 79 | + for _, c := range crl.Revoked { |
| 80 | + err := c.ensureGenerated() |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + if c.Issuer == nil { |
| 85 | + return nil, fmt.Errorf("cannot revoke self-signed certificate: %s", c.Subject) |
| 86 | + } else if c.Issuer != issuer { |
| 87 | + return nil, fmt.Errorf("CRL can contain certificates for single issuer only") |
| 88 | + } |
| 89 | + revokedCerts = append(revokedCerts, pkix.RevokedCertificate{ |
| 90 | + SerialNumber: c.SerialNumber, |
| 91 | + RevocationTime: effectiveRevocationTime, |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + ca, err := issuer.X509Certificate() |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + privateKey, err := issuer.PrivateKey() |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + return ca.CreateCRL(rand.Reader, privateKey, revokedCerts, effectiveRevocationTime, effectiveExpiry) |
| 106 | +} |
| 107 | + |
| 108 | +// PEM returns the CRL as PEM buffer. |
| 109 | +// Error is not nil if generation fails. |
| 110 | +func (crl *CRL) PEM() (crlBytes []byte, err error) { |
| 111 | + derBytes, err := crl.DER() |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + var buf bytes.Buffer |
| 117 | + err = pem.Encode(&buf, &pem.Block{ |
| 118 | + Type: "X509 CRL", |
| 119 | + Bytes: derBytes, |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + |
| 125 | + crlBytes = append(crlBytes, buf.Bytes()...) // Create copy of underlying buf. |
| 126 | + return |
| 127 | +} |
| 128 | + |
| 129 | +// WritePEM writes the CRL as PEM file. |
| 130 | +// Error is not nil if writing fails. |
| 131 | +func (crl *CRL) WritePEM(crlFile string) error { |
| 132 | + pemBytes, err := crl.PEM() |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + err = os.WriteFile(crlFile, pemBytes, 0600) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
0 commit comments