Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package pki |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "crypto/sha1" |
| 22 | "crypto/x509" |
| 23 | "crypto/x509/pkix" |
| 24 | "encoding/asn1" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | // From RFC 5280 Section 4.1.2.5 |
| 30 | unknownNotAfter = time.Unix(253402300799, 0) |
| 31 | ) |
| 32 | |
| 33 | // Workaround for https://github.com/golang/go/issues/26676 in Go's crypto/x509. Specifically Go |
| 34 | // violates Section 4.2.1.2 of RFC 5280 without this. |
| 35 | // Fixed for 1.15 in https://go-review.googlesource.com/c/go/+/227098/. |
| 36 | // |
| 37 | // Taken from https://github.com/FiloSottile/mkcert/blob/master/cert.go#L295 written by one of Go's |
| 38 | // crypto engineers |
| 39 | // |
| 40 | // TODO(lorenz): remove this once we migrate to Go 1.15. |
| 41 | func calculateSKID(pubKey crypto.PublicKey) ([]byte, error) { |
| 42 | spkiASN1, err := x509.MarshalPKIXPublicKey(pubKey) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | |
| 47 | var spki struct { |
| 48 | Algorithm pkix.AlgorithmIdentifier |
| 49 | SubjectPublicKey asn1.BitString |
| 50 | } |
| 51 | _, err = asn1.Unmarshal(spkiASN1, &spki) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | skid := sha1.Sum(spki.SubjectPublicKey.Bytes) |
| 56 | return skid[:], nil |
| 57 | } |