3ec5be3f51
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
231 lines
6.0 KiB
Go
231 lines
6.0 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package key
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/subtle"
|
|
"fmt"
|
|
|
|
"go4.org/mem"
|
|
"golang.org/x/crypto/curve25519"
|
|
"golang.org/x/crypto/nacl/box"
|
|
"tailscale.com/types/structs"
|
|
)
|
|
|
|
const (
|
|
// discoPublicHexPrefix is the prefix used to identify a
|
|
// hex-encoded disco public key.
|
|
//
|
|
// This prefix is used in the control protocol, so cannot be
|
|
// changed.
|
|
discoPublicHexPrefix = "discokey:"
|
|
|
|
// DiscoPublicRawLen is the length in bytes of a DiscoPublic, when
|
|
// serialized with AppendTo, Raw32 or WriteRawWithoutAllocating.
|
|
DiscoPublicRawLen = 32
|
|
)
|
|
|
|
// DiscoPrivate is a disco key, used for peer-to-peer path discovery.
|
|
type DiscoPrivate struct {
|
|
_ structs.Incomparable // because == isn't constant-time
|
|
k [32]byte
|
|
}
|
|
|
|
// NewDisco creates and returns a new disco private key.
|
|
func NewDisco() DiscoPrivate {
|
|
var ret DiscoPrivate
|
|
rand(ret.k[:])
|
|
// Key used for nacl seal/open, so needs to be clamped.
|
|
clamp25519Private(ret.k[:])
|
|
return ret
|
|
}
|
|
|
|
// IsZero reports whether k is the zero value.
|
|
func (k DiscoPrivate) IsZero() bool {
|
|
return k.Equal(DiscoPrivate{})
|
|
}
|
|
|
|
// Equal reports whether k and other are the same key.
|
|
func (k DiscoPrivate) Equal(other DiscoPrivate) bool {
|
|
return subtle.ConstantTimeCompare(k.k[:], other.k[:]) == 1
|
|
}
|
|
|
|
// Public returns the DiscoPublic for k.
|
|
// Panics if DiscoPrivate is zero.
|
|
func (k DiscoPrivate) Public() DiscoPublic {
|
|
if k.IsZero() {
|
|
panic("can't take the public key of a zero DiscoPrivate")
|
|
}
|
|
var ret DiscoPublic
|
|
curve25519.ScalarBaseMult(&ret.k, &k.k)
|
|
return ret
|
|
}
|
|
|
|
// Shared returns the DiscoShared for communication between k and p.
|
|
func (k DiscoPrivate) Shared(p DiscoPublic) DiscoShared {
|
|
if k.IsZero() || p.IsZero() {
|
|
panic("can't compute shared secret with zero keys")
|
|
}
|
|
var ret DiscoShared
|
|
box.Precompute(&ret.k, &p.k, &k.k)
|
|
return ret
|
|
}
|
|
|
|
// SortedPairOfDiscoPublic is a lexicographically sorted container of two
|
|
// [DiscoPublic] keys.
|
|
type SortedPairOfDiscoPublic struct {
|
|
k [2]DiscoPublic
|
|
}
|
|
|
|
// Get returns the underlying keys.
|
|
func (s SortedPairOfDiscoPublic) Get() [2]DiscoPublic {
|
|
return s.k
|
|
}
|
|
|
|
// NewSortedPairOfDiscoPublic returns a SortedPairOfDiscoPublic from a and b.
|
|
func NewSortedPairOfDiscoPublic(a, b DiscoPublic) SortedPairOfDiscoPublic {
|
|
s := SortedPairOfDiscoPublic{}
|
|
if a.Compare(b) < 0 {
|
|
s.k[0] = a
|
|
s.k[1] = b
|
|
} else {
|
|
s.k[0] = b
|
|
s.k[1] = a
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (s SortedPairOfDiscoPublic) String() string {
|
|
return fmt.Sprintf("%s <=> %s", s.k[0].ShortString(), s.k[1].ShortString())
|
|
}
|
|
|
|
// Equal returns true if s and b are equal, otherwise it returns false.
|
|
func (s SortedPairOfDiscoPublic) Equal(b SortedPairOfDiscoPublic) bool {
|
|
for i := range s.k {
|
|
if s.k[i].Compare(b.k[i]) != 0 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// DiscoPublic is the public portion of a DiscoPrivate.
|
|
type DiscoPublic struct {
|
|
k [32]byte
|
|
}
|
|
|
|
// DiscoPublicFromRaw32 parses a 32-byte raw value as a DiscoPublic.
|
|
//
|
|
// This should be used only when deserializing a DiscoPublic from a
|
|
// binary protocol.
|
|
func DiscoPublicFromRaw32(raw mem.RO) DiscoPublic {
|
|
if raw.Len() != 32 {
|
|
panic("input has wrong size")
|
|
}
|
|
var ret DiscoPublic
|
|
raw.Copy(ret.k[:])
|
|
return ret
|
|
}
|
|
|
|
// IsZero reports whether k is the zero value.
|
|
func (k DiscoPublic) IsZero() bool {
|
|
return k == DiscoPublic{}
|
|
}
|
|
|
|
// Raw32 returns k encoded as 32 raw bytes.
|
|
//
|
|
// Deprecated: only needed for a temporary compat shim in tailcfg, do
|
|
// not add more uses.
|
|
func (k DiscoPublic) Raw32() [32]byte {
|
|
return k.k
|
|
}
|
|
|
|
// ShortString returns the Tailscale conventional debug representation
|
|
// of a disco key.
|
|
func (k DiscoPublic) ShortString() string {
|
|
if k.IsZero() {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("d:%x", k.k[:8])
|
|
}
|
|
|
|
// AppendTo appends k, serialized as a 32-byte binary value, to
|
|
// buf. Returns the new slice.
|
|
func (k DiscoPublic) AppendTo(buf []byte) []byte {
|
|
return append(buf, k.k[:]...)
|
|
}
|
|
|
|
// String returns the output of MarshalText as a string.
|
|
func (k DiscoPublic) String() string {
|
|
bs, err := k.MarshalText()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return string(bs)
|
|
}
|
|
|
|
// Compare returns an integer comparing DiscoPublic k and l lexicographically.
|
|
// The result will be 0 if k == other, -1 if k < other, and +1 if k > other.
|
|
// This is useful for situations requiring only one node in a pair to perform
|
|
// some operation, e.g. probing UDP path lifetime.
|
|
func (k DiscoPublic) Compare(other DiscoPublic) int {
|
|
return bytes.Compare(k.k[:], other.k[:])
|
|
}
|
|
|
|
// AppendText implements encoding.TextAppender.
|
|
func (k DiscoPublic) AppendText(b []byte) ([]byte, error) {
|
|
return appendHexKey(b, discoPublicHexPrefix, k.k[:]), nil
|
|
}
|
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
|
func (k DiscoPublic) MarshalText() ([]byte, error) {
|
|
return k.AppendText(nil)
|
|
}
|
|
|
|
// MarshalText implements encoding.TextUnmarshaler.
|
|
func (k *DiscoPublic) UnmarshalText(b []byte) error {
|
|
return parseHex(k.k[:], mem.B(b), mem.S(discoPublicHexPrefix))
|
|
}
|
|
|
|
type DiscoShared struct {
|
|
_ structs.Incomparable // because == isn't constant-time
|
|
k [32]byte
|
|
}
|
|
|
|
// Equal reports whether k and other are the same key.
|
|
func (k DiscoShared) Equal(other DiscoShared) bool {
|
|
return subtle.ConstantTimeCompare(k.k[:], other.k[:]) == 1
|
|
}
|
|
|
|
func (k DiscoShared) IsZero() bool {
|
|
return k.Equal(DiscoShared{})
|
|
}
|
|
|
|
// Seal wraps cleartext into a NaCl box (see
|
|
// golang.org/x/crypto/nacl), using k as the shared secret and a
|
|
// random nonce.
|
|
func (k DiscoShared) Seal(cleartext []byte) (ciphertext []byte) {
|
|
if k.IsZero() {
|
|
panic("can't seal with zero key")
|
|
}
|
|
var nonce [24]byte
|
|
rand(nonce[:])
|
|
return box.SealAfterPrecomputation(nonce[:], cleartext, &nonce, &k.k)
|
|
}
|
|
|
|
// Open opens the NaCl box ciphertext, which must be a value created
|
|
// by Seal, and returns the inner cleartext if ciphertext is a valid
|
|
// box using shared secret k.
|
|
func (k DiscoShared) Open(ciphertext []byte) (cleartext []byte, ok bool) {
|
|
if k.IsZero() {
|
|
panic("can't open with zero key")
|
|
}
|
|
if len(ciphertext) < 24 {
|
|
return nil, false
|
|
}
|
|
nonce := (*[24]byte)(ciphertext)
|
|
return box.OpenAfterPrecomputation(nil, ciphertext[24:], nonce, &k.k)
|
|
}
|