|
| 1 | +package certdehydrate |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "crypto/x509/pkix" |
| 7 | + "encoding/base64" |
| 8 | + "encoding/binary" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "math/big" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +import "github.com/namecoin/ncdns/x509" |
| 16 | + |
| 17 | +// TODO: add a version field |
| 18 | +type DehydratedCertificate struct { |
| 19 | + PubkeyB64 string |
| 20 | + NotBeforeScaled int64 |
| 21 | + NotAfterScaled int64 |
| 22 | + SignatureAlgorithm int64 |
| 23 | + SignatureB64 string |
| 24 | +} |
| 25 | + |
| 26 | +func (dehydrated DehydratedCertificate) SerialNumber(name string) ([]byte, error){ |
| 27 | + |
| 28 | + nameHash := sha256.Sum256([]byte(name)) |
| 29 | + |
| 30 | + pubkeyBytes, err := base64.StdEncoding.DecodeString(dehydrated.PubkeyB64) |
| 31 | + if err != nil { |
| 32 | + return nil, fmt.Errorf("Dehydrated cert pubkey is not valid base64: %s", err) |
| 33 | + } |
| 34 | + pubkeyHash := sha256.Sum256(pubkeyBytes) |
| 35 | + |
| 36 | + notBeforeScaledBuf := new(bytes.Buffer) |
| 37 | + err = binary.Write(notBeforeScaledBuf, binary.BigEndian, dehydrated.NotBeforeScaled) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("binary.Write of notBefore failed: %s", err) |
| 40 | + } |
| 41 | + notBeforeHash := sha256.Sum256(notBeforeScaledBuf.Bytes()) |
| 42 | + |
| 43 | + notAfterScaledBuf := new(bytes.Buffer) |
| 44 | + err = binary.Write(notAfterScaledBuf, binary.BigEndian, dehydrated.NotAfterScaled) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("binary.Write of notAfter failed: %s", err) |
| 47 | + } |
| 48 | + notAfterHash := sha256.Sum256(notAfterScaledBuf.Bytes()) |
| 49 | + |
| 50 | + serialHash := sha256.New() |
| 51 | + serialHash.Write(nameHash[:]) |
| 52 | + serialHash.Write(pubkeyHash[:]) |
| 53 | + serialHash.Write(notBeforeHash[:]) |
| 54 | + serialHash.Write(notAfterHash[:]) |
| 55 | + |
| 56 | + // 19 bytes will be less than 2^159, see https://crypto.stackexchange.com/a/260 |
| 57 | + return serialHash.Sum(nil)[0:19], nil |
| 58 | +} |
| 59 | + |
| 60 | +func (dehydrated DehydratedCertificate) String() string { |
| 61 | + output := []interface{}{1, dehydrated.PubkeyB64, dehydrated.NotBeforeScaled, dehydrated.NotAfterScaled, dehydrated.SignatureAlgorithm, dehydrated.SignatureB64} |
| 62 | + binOutput, _ := json.Marshal(output) |
| 63 | + return string(binOutput) |
| 64 | +} |
| 65 | + |
| 66 | +func ParseDehydratedCert(data interface{}) (*DehydratedCertificate, error) { |
| 67 | + dehydrated, ok := data.([]interface{}) |
| 68 | + if !ok { |
| 69 | + return nil, fmt.Errorf("Dehydrated cert is not a list") |
| 70 | + } |
| 71 | + |
| 72 | + if len(dehydrated) < 1 { |
| 73 | + return nil, fmt.Errorf("Dehydrated cert must have a version field") |
| 74 | + } |
| 75 | + |
| 76 | + version, ok := dehydrated[0].(float64) |
| 77 | + if !ok { |
| 78 | + return nil, fmt.Errorf("Dehydrated cert version must be an integer") |
| 79 | + } |
| 80 | + |
| 81 | + if version != 1 { |
| 82 | + return nil, fmt.Errorf("Dehydrated cert has an unrecognized version") |
| 83 | + } |
| 84 | + |
| 85 | + if len(dehydrated) < 6 { |
| 86 | + return nil, fmt.Errorf("Dehydrated cert must have 6 items") |
| 87 | + } |
| 88 | + |
| 89 | + pubkeyB64, ok := dehydrated[1].(string) |
| 90 | + if !ok { |
| 91 | + return nil, fmt.Errorf("Dehydrated cert pubkey must be a string") |
| 92 | + } |
| 93 | + |
| 94 | + notBeforeScaled, ok := dehydrated[2].(float64) |
| 95 | + if !ok { |
| 96 | + return nil, fmt.Errorf("Dehydrated cert notBefore must be an integer") |
| 97 | + } |
| 98 | + |
| 99 | + notAfterScaled, ok := dehydrated[3].(float64) |
| 100 | + if !ok { |
| 101 | + return nil, fmt.Errorf("Dehydrated cert notAfter must be an integer") |
| 102 | + } |
| 103 | + |
| 104 | + signatureAlgorithm, ok := dehydrated[4].(float64) |
| 105 | + if !ok { |
| 106 | + return nil, fmt.Errorf("Dehydrated cert signature algorithm must be an integer") |
| 107 | + } |
| 108 | + |
| 109 | + signatureB64, ok := dehydrated[5].(string) |
| 110 | + if !ok { |
| 111 | + return nil, fmt.Errorf("Dehydrated cert signature must be a string") |
| 112 | + } |
| 113 | + |
| 114 | + result := DehydratedCertificate { |
| 115 | + PubkeyB64: pubkeyB64, |
| 116 | + NotBeforeScaled: int64(notBeforeScaled), |
| 117 | + NotAfterScaled: int64(notAfterScaled), |
| 118 | + SignatureAlgorithm: int64(signatureAlgorithm), |
| 119 | + SignatureB64: signatureB64, |
| 120 | + } |
| 121 | + |
| 122 | + return &result, nil |
| 123 | +} |
| 124 | + |
| 125 | +func DehydrateCert(cert *x509.Certificate) (*DehydratedCertificate, error) { |
| 126 | + |
| 127 | + pubkeyBytes, err := x509.MarshalPKIXPublicKey(cert.PublicKey) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("failed to marshal parsed public key: %s", err) |
| 130 | + } |
| 131 | + |
| 132 | + pubkeyB64 := base64.StdEncoding.EncodeToString(pubkeyBytes) |
| 133 | + |
| 134 | + notBeforeInt := cert.NotBefore.Unix() |
| 135 | + notAfterInt := cert.NotAfter.Unix() |
| 136 | + |
| 137 | + timestampPrecision := int64(5 * 60) // 5 minute precision |
| 138 | + |
| 139 | + notBeforeScaled := notBeforeInt / timestampPrecision |
| 140 | + notAfterScaled := notAfterInt / timestampPrecision |
| 141 | + |
| 142 | + signatureAlgorithm := int64(cert.SignatureAlgorithm) |
| 143 | + signatureBytes := cert.Signature |
| 144 | + signatureB64 := base64.StdEncoding.EncodeToString(signatureBytes) |
| 145 | + |
| 146 | + result := DehydratedCertificate{ |
| 147 | + PubkeyB64: pubkeyB64, |
| 148 | + NotBeforeScaled: notBeforeScaled, |
| 149 | + NotAfterScaled: notAfterScaled, |
| 150 | + SignatureAlgorithm: signatureAlgorithm, |
| 151 | + SignatureB64: signatureB64, |
| 152 | + } |
| 153 | + |
| 154 | + return &result, nil |
| 155 | +} |
| 156 | + |
| 157 | +// Accepts as input the bare minimum data needed to produce a valid cert. |
| 158 | +// The input is untrusted. |
| 159 | +// The output is safe. |
| 160 | +// The timestamps are in 5-minute increments. |
| 161 | +func RehydrateCert(dehydrated *DehydratedCertificate) (*x509.Certificate, error) { |
| 162 | + |
| 163 | + pubkeyBin, err := base64.StdEncoding.DecodeString(dehydrated.PubkeyB64) |
| 164 | + if err != nil { |
| 165 | + return nil, fmt.Errorf("Dehydrated cert pubkey must be valid base64: %s", err) |
| 166 | + } |
| 167 | + |
| 168 | + pubkey, err := x509.ParsePKIXPublicKey(pubkeyBin) |
| 169 | + if err != nil { |
| 170 | + return nil, fmt.Errorf("Dehydrated cert pubkey is invalid: %s", err) |
| 171 | + } |
| 172 | + |
| 173 | + timestampPrecision := int64(5 * 60) // 5 minute precision |
| 174 | + |
| 175 | + notBeforeInt := dehydrated.NotBeforeScaled * timestampPrecision |
| 176 | + notAfterInt := dehydrated.NotAfterScaled * timestampPrecision |
| 177 | + |
| 178 | + notBefore := time.Unix(int64(notBeforeInt), 0) |
| 179 | + notAfter := time.Unix(int64(notAfterInt), 0) |
| 180 | + |
| 181 | + signatureAlgorithm := x509.SignatureAlgorithm(dehydrated.SignatureAlgorithm) |
| 182 | + |
| 183 | + signature, err := base64.StdEncoding.DecodeString(dehydrated.SignatureB64) |
| 184 | + if err != nil { |
| 185 | + return nil, fmt.Errorf("Dehydrated cert signature must be valid base64: %s", err) |
| 186 | + } |
| 187 | + |
| 188 | + template := x509.Certificate{ |
| 189 | + SerialNumber: big.NewInt(1), |
| 190 | + NotBefore: notBefore, |
| 191 | + NotAfter: notAfter, |
| 192 | + |
| 193 | + // x509.KeyUsageKeyEncipherment is used for RSA key exchange, but not DHE/ECDHE key exchange. Since everyone should be using ECDHE (due to forward secrecy), we disallow x509.KeyUsageKeyEncipherment in our template. |
| 194 | + //KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| 195 | + KeyUsage: x509.KeyUsageDigitalSignature, |
| 196 | + |
| 197 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 198 | + BasicConstraintsValid: true, |
| 199 | + |
| 200 | + SignatureAlgorithm: signatureAlgorithm, |
| 201 | + PublicKey: pubkey, |
| 202 | + Signature: signature, |
| 203 | + } |
| 204 | + |
| 205 | + return &template, nil |
| 206 | +} |
| 207 | + |
| 208 | +func FillRehydratedCertTemplate(template x509.Certificate, name string) ([]byte, error) { |
| 209 | + |
| 210 | + template.Subject = pkix.Name{ |
| 211 | + CommonName: name, |
| 212 | + SerialNumber: "Namecoin TLS Certificate", |
| 213 | + } |
| 214 | + |
| 215 | + // DNS name |
| 216 | + template.DNSNames = append(template.DNSNames, name) |
| 217 | + |
| 218 | + // Serial number |
| 219 | + dehydrated, err := DehydrateCert(&template) |
| 220 | + if err != nil { |
| 221 | + return nil, fmt.Errorf("Error dehydrating filled cert template: %s", err) |
| 222 | + } |
| 223 | + serialNumberBytes, err := dehydrated.SerialNumber(name) |
| 224 | + if err != nil { |
| 225 | + return nil, fmt.Errorf("Error calculating serial number: %s", err) |
| 226 | + } |
| 227 | + template.SerialNumber.SetBytes(serialNumberBytes) |
| 228 | + |
| 229 | + derBytes, err := x509.CreateCertificateWithSplicedSignature(&template, &template) |
| 230 | + if err != nil { |
| 231 | + return nil, fmt.Errorf("Error splicing signature: %s", err) |
| 232 | + } |
| 233 | + |
| 234 | + return derBytes, nil |
| 235 | + |
| 236 | +} |
0 commit comments