cert.go RawHistoryFolder

1package quic
2
3import (
4	"crypto/ed25519"
5	"crypto/rand"
6	"crypto/sha256"
7	"crypto/tls"
8	"crypto/x509"
9	"crypto/x509/pkix"
10	"encoding/pem"
11	"math/big"
12	"time"
13)
14
15// Ephemeral certificates are minted per bootstrap listener and never
16// persisted: the SHA-256 pin travels through the SSH bootstrap channel,
17// and the key dies with the process.
18
19// GenerateEphemeralCert mints one self-signed Ed25519 certificate valid
20// for 24 hours. The returned fingerprint is the SHA-256 of the raw DER
21// certificate the dialer pins.
22func GenerateEphemeralCert() (tls.Certificate, []byte, error) {
23	public, private, err := ed25519.GenerateKey(rand.Reader)
24	if err != nil {
25		return tls.Certificate{}, nil, err
26	}
27	serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
28	if err != nil {
29		return tls.Certificate{}, nil, err
30	}
31	now := time.Now()
32	template := &x509.Certificate{
33		SerialNumber: serial,
34		Subject:      pkix.Name{CommonName: "vev-ephemeral"},
35		NotBefore:    now.Add(-time.Minute),
36		NotAfter:     now.Add(24 * time.Hour),
37		KeyUsage:     x509.KeyUsageDigitalSignature,
38		ExtKeyUsage:  []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
39		DNSNames:     []string{"vev-bootstrap"},
40	}
41	der, err := x509.CreateCertificate(rand.Reader, template, template, public, private)
42	if err != nil {
43		return tls.Certificate{}, nil, err
44	}
45	cert, err := tls.X509KeyPair(
46		pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}),
47		pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: mustMarshalPKCS8(private)}),
48	)
49	if err != nil {
50		return tls.Certificate{}, nil, err
51	}
52	sum := sha256.Sum256(der)
53	return cert, sum[:], nil
54}
55
56// mustMarshalPKCS8 marshals an Ed25519 private key; Ed25519 keys always
57// marshal, so a failure panics as a programming error.
58func mustMarshalPKCS8(private ed25519.PrivateKey) []byte {
59	der, err := x509.MarshalPKCS8PrivateKey(private)
60	if err != nil {
61		panic(err)
62	}
63	return der
64}
65
66// FingerprintDER returns the SHA-256 pin of one DER certificate.
67func FingerprintDER(der []byte) []byte {
68	sum := sha256.Sum256(der)
69	return sum[:]
70}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 12

Tags 11

↑↓ moveenter openesc close