blob: 2db1b19dfab71baefee70826991d45695f554e5d [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Serge Bazanski9411f7c2021-03-10 13:12:53 +01002// SPDX-License-Identifier: Apache-2.0
Serge Bazanski9411f7c2021-03-10 13:12:53 +01003
4package pki
5
6import (
7 "crypto"
8 "crypto/sha1"
9 "crypto/x509"
10 "crypto/x509/pkix"
11 "encoding/asn1"
12 "time"
13)
14
15var (
16 // From RFC 5280 Section 4.1.2.5
Serge Bazanskid7d6e022021-09-01 15:03:06 +020017 UnknownNotAfter = time.Unix(253402300799, 0)
Serge Bazanski9411f7c2021-03-10 13:12:53 +010018)
19
Serge Bazanski216fe7b2021-05-21 18:36:16 +020020// Workaround for https://github.com/golang/go/issues/26676 in Go's
21// crypto/x509. Specifically Go violates Section 4.2.1.2 of RFC 5280 without
22// this. Fixed for 1.15 in https://go-review.googlesource.com/c/go/+/227098/.
Serge Bazanski9411f7c2021-03-10 13:12:53 +010023//
Serge Bazanski216fe7b2021-05-21 18:36:16 +020024// Taken from https://github.com/FiloSottile/mkcert/blob/master/cert.go#L295
25// Written by one of Go's crypto engineers
Serge Bazanski9411f7c2021-03-10 13:12:53 +010026//
27// TODO(lorenz): remove this once we migrate to Go 1.15.
28func calculateSKID(pubKey crypto.PublicKey) ([]byte, error) {
29 spkiASN1, err := x509.MarshalPKIXPublicKey(pubKey)
30 if err != nil {
31 return nil, err
32 }
33
34 var spki struct {
35 Algorithm pkix.AlgorithmIdentifier
36 SubjectPublicKey asn1.BitString
37 }
38 _, err = asn1.Unmarshal(spkiASN1, &spki)
39 if err != nil {
40 return nil, err
41 }
42 skid := sha1.Sum(spki.SubjectPublicKey.Bytes)
43 return skid[:], nil
44}