parent:
af60bd0141acb3f5ffa6ea8e93d9b351b41c9420
nmyk <nick@nmyk.io>
2026-11-14T18:58:14-05:00
add authorship
diff --git a/cmd/government/author_test.go b/cmd/government/author_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..3966c1c20be09491caca8b904ec4f1857ab52491
--- /dev/null
+++ b/cmd/government/author_test.go
@@ -0,0 +1,118 @@
+package main
+
+import (
+ "crypto/ed25519"
+ "crypto/x509"
+ "encoding/pem"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func writeKey(t *testing.T, dir, name string, seed byte) string {
+ t.Helper()
+ b := make([]byte, 32)
+ for i := range b {
+ b[i] = seed
+ }
+ der, err := x509.MarshalPKCS8PrivateKey(ed25519.NewKeyFromSeed(b))
+ if err != nil {
+ t.Fatal(err)
+ }
+ path := filepath.Join(dir, name+".pem")
+ os.WriteFile(path, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}), 0o600)
+ return path
+}
+
+// TestAuthorsPinTheirOwn is the plan's check: each author's build verifies
+// with its own -anchor pin, and neither with the other's.
+func TestAuthorsPinTheirOwn(t *testing.T) {
+ dir := t.TempDir()
+ src := writeSource(t, "law", "(+ 1 (* 2 3))")
+ build := func(who string, seed byte, author string) string {
+ key := writeKey(t, dir, who, seed)
+ gov := filepath.Join(dir, who+".gov")
+ if code, _, stderr := runCmd("compile", src, "-o", gov, "-key", key, "-author", author); code != exitOK {
+ t.Fatalf("%s: %s", who, stderr)
+ }
+ return gov
+ }
+ ada := build("ada", 1, "Ada Lovelace <ada@example.com>")
+ bob := build("bob", 2, "Bob")
+
+ for _, c := range []struct {
+ prog, pin string
+ ok bool
+ }{{ada, ada, true}, {bob, bob, true}, {ada, bob, false}, {bob, ada, false}} {
+ code, _, stderr := runCmd("verify", c.prog, "-anchor", c.pin)
+ if c.ok != (code == exitOK) {
+ t.Errorf("verify %s -anchor %s: exit %d, %s", filepath.Base(c.prog), filepath.Base(c.pin), code, stderr)
+ }
+ if !c.ok && !strings.Contains(stderr, "E-FOREIGN") {
+ t.Errorf("verify %s -anchor %s: %s, want E-FOREIGN", filepath.Base(c.prog), filepath.Base(c.pin), stderr)
+ }
+ }
+
+ _, stdout, _ := runCmd("verify", ada)
+ if !strings.Contains(stdout, "Ratified by Ada Lovelace <ada@example.com>\n") {
+ t.Errorf("certificate:\n%s", stdout)
+ }
+ _, stdout, _ = runCmd("verify", compileTo(t, dir, "anon", "1"))
+ if !strings.Contains(stdout, "nobody in particular (default key)") {
+ t.Errorf("default key not reported:\n%s", stdout)
+ }
+}
+
+func TestCompileDates(t *testing.T) {
+ dir := t.TempDir()
+ src := writeSource(t, "dated", "(+ 1 2)")
+ gov := filepath.Join(dir, "dated.gov")
+ if code, _, stderr := runCmd("compile", src, "-o", gov, "-effective", "2027-01-01", "-expires", "2027-12-31T00:00:00Z"); code != exitOK {
+ t.Fatal(stderr)
+ }
+ for at, want := range map[string]string{
+ "2027-06-01T00:00:00Z": "",
+ "2026-12-31T00:00:00Z": "E-NOT-IN-FORCE: takes effect 2027-01-01T00:00:00Z",
+ "2028-01-01T00:00:00Z": "E-LAPSED: expired 2027-12-31T00:00:00Z",
+ } {
+ code, stdout, stderr := runCmd("run", gov, "-at", at)
+ switch {
+ case want == "" && (code != exitOK || stdout != "3\n"):
+ t.Errorf("at %s: exit %d, %q, %s", at, code, stdout, stderr)
+ case want != "" && !strings.Contains(stderr, want):
+ t.Errorf("at %s: %s, want %s", at, stderr, want)
+ }
+ }
+ code, stdout, stderr := runCmd("verify", gov, "-at", "2027-06-01T00:00:00Z")
+ if code != exitOK || !strings.Contains(stdout, "In force 2027-01-01T00:00:00Z to 2027-12-31T00:00:00Z") {
+ t.Errorf("verify in the window: exit %d, %s%s", code, stdout, stderr)
+ }
+}
+
+func TestCompileSigningFlags(t *testing.T) {
+ dir := t.TempDir()
+ src := writeSource(t, "p", "1")
+ key := writeKey(t, dir, "k", 5)
+ seedHex := strings.Repeat("05", 32)
+ a, b := filepath.Join(dir, "a.gov"), filepath.Join(dir, "b.gov")
+ runCmd("compile", src, "-o", a, "-key", key)
+ runCmd("compile", src, "-o", b, "-seed", seedHex)
+ x, _ := os.ReadFile(a)
+ y, _ := os.ReadFile(b)
+ if len(x) == 0 || string(x) != string(y) {
+ t.Error("-key and -seed for the same key gave different programs")
+ }
+ for _, args := range [][]string{
+ {"compile", src, "-key", key, "-seed", seedHex},
+ {"compile", src, "-seed", "abc"},
+ {"compile", src, "-key", filepath.Join(dir, "missing.pem")},
+ {"compile", src, "-effective", "next week"},
+ {"compile", src, "-effective", "2028-01-01", "-expires", "2027-01-01"},
+ {"compile", src, "-author", "Ada <broken"},
+ } {
+ if code, _, _ := runCmd(args...); code != exitInvalid {
+ t.Errorf("%v: exit %d, want %d", args[2:], code, exitInvalid)
+ }
+ }
+}
diff --git a/cmd/government/compile.go b/cmd/government/compile.go
index 185d2cf3a97e29c742e67a0c90665e65f94ed0b8..fa5b6e03f55d7dbf9be9446d8d469827270ebe7e 100644
--- a/cmd/government/compile.go
+++ b/cmd/government/compile.go
@@ -1,6 +1,7 @@
package main
import (
+ "encoding/hex"
"errors"
"flag"
"fmt"
@@ -8,6 +9,7 @@ "io"
"os"
"path/filepath"
"strings"
+ "time"
"government/lisp2gov"
"government/sexpr"
@@ -19,8 +21,15 @@ fs.SetOutput(stderr)
out := fs.String("o", "", "write the program to `file` instead of standard output")
name := fs.String("name", "", "program name for the certs' O= field (default: the source file's name without .gov.lisp)")
withSource := fs.Bool("source", false, "copy the Lisp source into the file's header")
+ keyFile := fs.String("key", "", "sign with the Ed25519 PKCS#8 private key in `file` (make one with: openssl genpkey -algorithm ed25519)")
+ seedHex := fs.String("seed", "", "sign with keys derived from this 32-byte `hex` seed instead of a key file")
+ author := fs.String("author", "", "name the author in the anchor, as `\"Name <email>\"`")
+ var effective, expires dateFlag
+ fs.Var(&effective, "effective", "the program takes effect at this `time` (RFC 3339 or YYYY-MM-DD; default 2000-01-01)")
+ fs.Var(&expires, "expires", "the program expires at this `time` (RFC 3339 or YYYY-MM-DD; default 9999-12-31T23:59:59Z)")
fs.Usage = func() {
- fmt.Fprintln(stderr, "usage: government compile prog.gov.lisp [-o prog.gov] [-name name] [-source]")
+ fmt.Fprintln(stderr, "usage: government compile prog.gov.lisp [-o prog.gov] [-name name] [-source]\n"+
+ " [-key file | -seed hex] [-author \"Name <email>\"] [-effective time] [-expires time]")
fs.PrintDefaults()
}
positional, err := parseInterspersed(fs, args)
@@ -45,7 +54,12 @@ if *name == base {
*name = strings.TrimSuffix(base, filepath.Ext(base))
}
}
- program, err := lisp2gov.Compile(string(src), lisp2gov.Options{Name: *name, IncludeSource: *withSource})
+ opts := lisp2gov.Options{Name: *name, IncludeSource: *withSource, Author: *author, Effective: effective.t, Expires: expires.t}
+ if opts.Seed, err = signingSeed(*keyFile, *seedHex); err != nil {
+ fmt.Fprintln(stderr, err)
+ return exitInvalid
+ }
+ program, err := lisp2gov.Compile(string(src), opts)
if err != nil {
var se *sexpr.SyntaxError
if errors.As(err, &se) {
@@ -85,3 +99,53 @@ positional = append(positional, args[0])
args = args[1:]
}
}
+
+// signingSeed reads -key or -seed. Neither means the default seed, which
+// is public: anyone can rebuild, amend or repeal such a program.
+func signingSeed(keyFile, seedHex string) (*[32]byte, error) {
+ switch {
+ case keyFile != "" && seedHex != "":
+ return nil, errors.New("give -key or -seed, not both")
+ case keyFile != "":
+ data, err := os.ReadFile(keyFile)
+ if err != nil {
+ return nil, err
+ }
+ seed, err := lisp2gov.LoadKey(data)
+ if err != nil {
+ return nil, fmt.Errorf("%s: %v", keyFile, err)
+ }
+ return seed, nil
+ case seedHex != "":
+ b, err := hex.DecodeString(seedHex)
+ if err != nil || len(b) != 32 {
+ return nil, errors.New("-seed wants 64 hex digits (32 bytes)")
+ }
+ seed := [32]byte(b)
+ return &seed, nil
+ }
+ return nil, nil
+}
+
+// dateFlag is -effective or -expires: an RFC 3339 time, or a date meaning
+// midnight UTC at its start.
+type dateFlag struct {
+ t time.Time
+}
+
+func (d *dateFlag) String() string {
+ if d == nil || d.t.IsZero() {
+ return ""
+ }
+ return d.t.Format(time.RFC3339)
+}
+
+func (d *dateFlag) Set(s string) error {
+ for _, layout := range []string{time.RFC3339, time.DateOnly} {
+ if t, err := time.Parse(layout, s); err == nil {
+ d.t = t.UTC()
+ return nil
+ }
+ }
+ return errors.New("want an RFC 3339 time or a date, such as 2027-01-01")
+}
diff --git a/lisp2gov/author_test.go b/lisp2gov/author_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..4b4866db8805b43a56e01dee25e6d07a73682e03
--- /dev/null
+++ b/lisp2gov/author_test.go
@@ -0,0 +1,149 @@
+package lisp2gov
+
+import (
+ "bytes"
+ "crypto/ecdsa"
+ "crypto/ed25519"
+ "crypto/elliptic"
+ "crypto/rand"
+ "crypto/sha256"
+ "crypto/x509"
+ "encoding/pem"
+ "reflect"
+ "testing"
+ "time"
+)
+
+// keyPEM writes an Ed25519 key the way openssl genpkey does.
+func keyPEM(t *testing.T, seed byte) []byte {
+ t.Helper()
+ key := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{seed}, 32))
+ der, err := x509.MarshalPKCS8PrivateKey(key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der})
+}
+
+func compileAs(t *testing.T, src string, keySeed byte, author string) []*x509.Certificate {
+ t.Helper()
+ seed, err := LoadKey(keyPEM(t, keySeed))
+ if err != nil {
+ t.Fatal(err)
+ }
+ out, err := Compile(src, Options{Name: "law", Seed: seed, Author: author})
+ if err != nil {
+ t.Fatal(err)
+ }
+ return parsePEM(t, out)
+}
+
+// TestTwoAuthors is the plan's check: two authors' builds of one source
+// differ only in keys and names.
+func TestTwoAuthors(t *testing.T) {
+ src := "; the total\n(+ 1 (* 2 3))"
+ ada := compileAs(t, src, 1, "Ada Lovelace <ada@example.com>")
+ bob := compileAs(t, src, 2, "Bob")
+ if len(ada) != len(bob) {
+ t.Fatalf("%d certs and %d certs", len(ada), len(bob))
+ }
+ for i := range ada {
+ a, b := ada[i], bob[i]
+ same := []struct {
+ what string
+ x, y any
+ }{
+ {"policies", a.Policies, b.Policies},
+ {"mappings", a.PolicyMappings, b.PolicyMappings},
+ {"serial", a.SerialNumber, b.SerialNumber},
+ {"window", [2]time.Time{a.NotBefore, a.NotAfter}, [2]time.Time{b.NotBefore, b.NotAfter}},
+ {"key usage", a.KeyUsage, b.KeyUsage},
+ {"CA flag", a.IsCA, b.IsCA},
+ {"organization", a.Subject.Organization, b.Subject.Organization},
+ }
+ for _, s := range same {
+ if !reflect.DeepEqual(s.x, s.y) {
+ t.Errorf("cert %d: %s differs", i, s.what)
+ }
+ }
+ if bytes.Equal(a.RawSubjectPublicKeyInfo, b.RawSubjectPublicKeyInfo) {
+ t.Errorf("cert %d: both authors' builds have the same key", i)
+ }
+ }
+ if ada[0].Subject.CommonName != "Ada Lovelace" || len(ada[0].EmailAddresses) != 1 || ada[0].EmailAddresses[0] != "ada@example.com" {
+ t.Errorf("Ada's anchor: CN %q, emails %v", ada[0].Subject.CommonName, ada[0].EmailAddresses)
+ }
+ if bob[0].Subject.CommonName != "Bob" || len(bob[0].EmailAddresses) != 0 {
+ t.Errorf("Bob's anchor: CN %q, emails %v", bob[0].Subject.CommonName, bob[0].EmailAddresses)
+ }
+}
+
+func TestAnchorIsSignedWithTheAuthorKey(t *testing.T) {
+ certs := compileAs(t, "1", 1, "Ada")
+ want := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{1}, 32)).Public()
+ if !want.(ed25519.PublicKey).Equal(certs[0].PublicKey) {
+ t.Error("the anchor's key is not the author's key")
+ }
+}
+
+func TestKeyDerivation(t *testing.T) {
+ // The design's formula: seed for depth i = SHA-256(seed || "GOVERNMENT" || i).
+ seed := [32]byte{9}
+ h := sha256.New()
+ h.Write(seed[:])
+ h.Write([]byte("GOVERNMENT3"))
+ want := ed25519.NewKeyFromSeed(h.Sum(nil))
+ if !keyForDepth(seed, 3).Equal(want) {
+ t.Error("depth 3's key is not SHA-256(seed || \"GOVERNMENT\" || \"3\")")
+ }
+}
+
+func TestParseAuthor(t *testing.T) {
+ tests := []struct{ in, name, email string }{
+ {"Ada Lovelace <ada@example.com>", "Ada Lovelace", "ada@example.com"},
+ {"Ada Lovelace", "Ada Lovelace", ""},
+ {"ada@example.com", "ada@example.com", "ada@example.com"},
+ {" ", "", ""},
+ }
+ for _, tt := range tests {
+ name, email, err := parseAuthor(tt.in)
+ if err != nil || name != tt.name || email != tt.email {
+ t.Errorf("parseAuthor(%q) = %q, %q, %v", tt.in, name, email, err)
+ }
+ }
+ if _, _, err := parseAuthor("Ada <not an email"); err == nil {
+ t.Error("a broken address was accepted")
+ }
+}
+
+func TestLoadKeyRejects(t *testing.T) {
+ ec, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
+ der, _ := x509.MarshalPKCS8PrivateKey(ec)
+ for name, data := range map[string][]byte{
+ "not PEM": []byte("hello"),
+ "public key": pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: []byte{1}}),
+ "bad PKCS#8": pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: []byte{1, 2, 3}}),
+ "an ECDSA key": pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}),
+ } {
+ if _, err := LoadKey(data); err == nil {
+ t.Errorf("%s: accepted", name)
+ }
+ }
+}
+
+func TestDates(t *testing.T) {
+ from := time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC)
+ to := time.Date(2027, 12, 31, 0, 0, 0, 0, time.UTC)
+ out, err := Compile("(+ 1 2)", Options{Effective: from, Expires: to})
+ if err != nil {
+ t.Fatal(err)
+ }
+ for i, c := range parsePEM(t, out) {
+ if !c.NotBefore.Equal(from) || !c.NotAfter.Equal(to) {
+ t.Errorf("cert %d: %s to %s", i, c.NotBefore, c.NotAfter)
+ }
+ }
+ if _, err := Compile("1", Options{Effective: to, Expires: from}); err == nil {
+ t.Error("a program that expires before it takes effect was compiled")
+ }
+}
diff --git a/lisp2gov/cert.go b/lisp2gov/cert.go
index d2ea25f5db075992b6c4d2067681b5d1cd7ee0ad..874dde93d71641e81f3b9afcac7c3d719257e327 100644
--- a/lisp2gov/cert.go
+++ b/lisp2gov/cert.go
@@ -26,21 +26,39 @@ cert *x509.Certificate
key ed25519.PrivateKey
}
-// newAnchor makes the self-signed trust anchor. It carries no policies:
-// the policy tree starts below it, at cert 1.
-func newAnchor(seed [32]byte, program string) (signer, error) {
- key := keyForDepth(seed, 0)
+// profile is what every cert of one program shares: who signs it, what it
+// is called, and when it is in force.
+type profile struct {
+ seed [32]byte
+ program string
+ author, email string
+ notBefore, notAfter time.Time
+}
+
+// newAnchor makes the self-signed trust anchor. It is signed with the
+// author's own key and names the author, so it records who ratified the
+// program. It carries no policies: the policy tree starts below it, at
+// cert 1.
+func newAnchor(p profile) (signer, error) {
+ key := keyForDepth(p.seed, 0)
pub := key.Public().(ed25519.PublicKey)
+ cn := p.author
+ if cn == "" {
+ cn = "GOVERNMENT Anchor"
+ }
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
- Subject: pkix.Name{CommonName: "GOVERNMENT Anchor", Organization: []string{program}},
- NotBefore: defaultEffective,
- NotAfter: defaultExpires,
+ Subject: pkix.Name{CommonName: cn, Organization: []string{p.program}},
+ NotBefore: p.notBefore,
+ NotAfter: p.notAfter,
BasicConstraintsValid: true,
IsCA: true,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
SubjectKeyId: keyID(pub),
AuthorityKeyId: keyID(pub),
+ }
+ if p.email != "" {
+ tmpl.EmailAddresses = []string{p.email}
}
return create(tmpl, tmpl, pub, key, key)
}
@@ -48,14 +66,14 @@
// newCert makes the cert at depth i, issued by parent. Every cert but the
// last is a CA, because each one signs the next; the last one signs
// nothing.
-func newCert(seed [32]byte, program string, i int, last bool, policies []policyInfo, mappings []x509.PolicyMapping, parent signer) (signer, error) {
- key := keyForDepth(seed, i)
+func newCert(p profile, i int, last bool, policies []policyInfo, mappings []x509.PolicyMapping, parent signer) (signer, error) {
+ key := keyForDepth(p.seed, i)
pub := key.Public().(ed25519.PublicKey)
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(int64(i + 1)),
- Subject: pkix.Name{CommonName: fmt.Sprintf("GOVERNMENT Depth %d", i), Organization: []string{program}},
- NotBefore: defaultEffective,
- NotAfter: defaultExpires,
+ Subject: pkix.Name{CommonName: fmt.Sprintf("GOVERNMENT Depth %d", i), Organization: []string{p.program}},
+ NotBefore: p.notBefore,
+ NotAfter: p.notAfter,
BasicConstraintsValid: true,
IsCA: !last,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
diff --git a/lisp2gov/compile.go b/lisp2gov/compile.go
index 97128c767a5fd1d39e6862b32f4bc4acd149ecc5..b824a4a9361fbb22c8b122c1c693f47249b2988f 100644
--- a/lisp2gov/compile.go
+++ b/lisp2gov/compile.go
@@ -3,7 +3,9 @@
import (
"crypto/x509"
"fmt"
+ "net/mail"
"strings"
+ "time"
"government/ast"
"government/sexpr"
@@ -20,6 +22,15 @@ // IncludeSource copies the Lisp source into the file's header. It is
// off by default: the certs alone are the program, and the source
// may hold comments or code the author does not want to publish.
IncludeSource bool
+ // Author is who ratifies the program, as "Name <email>", "Name" or
+ // "email". The name goes in the anchor's subject and the email in its
+ // subjectAltName. It is only a claim unless Seed is the author's own
+ // key: anyone can type a name, but only the key holder can sign.
+ Author string
+ // Effective and Expires bound when the program is in force, on every
+ // cert. Zero values mean 2000-01-01 and 9999-12-31 23:59:59, RFC
+ // 5280's "no well-defined expiration".
+ Effective, Expires time.Time
}
// Compile turns Lisp source into the bytes of a .gov file. The same source
@@ -40,11 +51,46 @@ }
return o.Name
}
-func (o Options) seed() [32]byte {
- if o.Seed == nil {
- return DefaultSeed
+func (o Options) profile() (profile, error) {
+ p := profile{seed: DefaultSeed, program: o.name(), notBefore: defaultEffective, notAfter: defaultExpires}
+ if o.Seed != nil {
+ p.seed = *o.Seed
+ }
+ if !o.Effective.IsZero() {
+ p.notBefore = o.Effective.UTC()
+ }
+ if !o.Expires.IsZero() {
+ p.notAfter = o.Expires.UTC()
+ }
+ if !p.notBefore.Before(p.notAfter) {
+ return profile{}, fmt.Errorf("the program would expire (%s) before it takes effect (%s)",
+ p.notAfter.Format(time.RFC3339), p.notBefore.Format(time.RFC3339))
+ }
+ var err error
+ if p.author, p.email, err = parseAuthor(o.Author); err != nil {
+ return profile{}, err
+ }
+ return p, nil
+}
+
+// parseAuthor splits "Ada Lovelace <ada@example.com>" into its name and
+// email. A bare name or a bare email is allowed too.
+func parseAuthor(s string) (name, email string, err error) {
+ s = strings.TrimSpace(s)
+ if s == "" {
+ return "", "", nil
+ }
+ if !strings.ContainsAny(s, "<@") {
+ return s, "", nil
+ }
+ addr, err := mail.ParseAddress(s)
+ if err != nil {
+ return "", "", fmt.Errorf("author %q: want \"Name <email>\", a name or an email", s)
+ }
+ if addr.Name == "" {
+ return addr.Address, addr.Address, nil
}
- return *o.Seed
+ return addr.Name, addr.Address, nil
}
// compileCerts builds the anchor and one cert per depth. Cert 1 lists the
@@ -65,8 +111,11 @@ if err != nil {
return nil, err
}
- name, seed := opts.name(), opts.seed()
- prev, err := newAnchor(seed, name)
+ p, err := opts.profile()
+ if err != nil {
+ return nil, err
+ }
+ prev, err := newAnchor(p)
if err != nil {
return nil, err
}
@@ -81,7 +130,7 @@ mappings, err := mappingsFor(layers.Edges[i-1])
if err != nil {
return nil, err
}
- next, err := newCert(seed, name, i, i == n, policies, mappings, prev)
+ next, err := newCert(p, i, i == n, policies, mappings, prev)
if err != nil {
return nil, fmt.Errorf("cert %d: %w", i, err)
}
diff --git a/lisp2gov/extensions_test.go b/lisp2gov/extensions_test.go
index 9fe0c1de97af579f3fd8d23f9a315d31ef1f359a..fdeee0af9d8aebacc308015f1f9a71cec9342abd 100644
--- a/lisp2gov/extensions_test.go
+++ b/lisp2gov/extensions_test.go
@@ -111,12 +111,16 @@ }
}
func TestMappingsRejectedOnLastCert(t *testing.T) {
- anchor, err := newAnchor(DefaultSeed, "x")
+ p, err := Options{Name: "x"}.profile()
+ if err != nil {
+ t.Fatal(err)
+ }
+ anchor, err := newAnchor(p)
if err != nil {
t.Fatal(err)
}
a, _ := x509.ParseOID("1.2.3")
- _, err = newCert(DefaultSeed, "x", 1, true, nil, []x509.PolicyMapping{{IssuerDomainPolicy: a, SubjectDomainPolicy: a}}, anchor)
+ _, err = newCert(p, 1, true, nil, []x509.PolicyMapping{{IssuerDomainPolicy: a, SubjectDomainPolicy: a}}, anchor)
if err == nil {
t.Error("last cert accepted mappings")
}
diff --git a/lisp2gov/keys.go b/lisp2gov/keys.go
index fae567c8e0f2ca4b76d24b6cd435bda5514298de..2779a7a39ae770b1696a4fe1578006cd5d2a4f11 100644
--- a/lisp2gov/keys.go
+++ b/lisp2gov/keys.go
@@ -3,6 +3,10 @@
import (
"crypto/ed25519"
"crypto/sha256"
+ "crypto/x509"
+ "encoding/pem"
+ "errors"
+ "fmt"
"strconv"
)
@@ -31,3 +35,24 @@ // particular.
func DefaultAnchorKey() ed25519.PublicKey {
return keyForDepth(DefaultSeed, 0).Public().(ed25519.PublicKey)
}
+
+// LoadKey reads an author's Ed25519 private key, a PKCS#8 "PRIVATE KEY" PEM
+// block such as `openssl genpkey -algorithm ed25519` writes, and returns
+// its seed. The seed is the key: Ed25519 derives the whole key pair from
+// these 32 bytes, so it is also what keyForDepth derives each depth from.
+func LoadKey(data []byte) (*[32]byte, error) {
+ block, _ := pem.Decode(data)
+ if block == nil || block.Type != "PRIVATE KEY" {
+ return nil, errors.New("want a PKCS#8 \"PRIVATE KEY\" PEM block")
+ }
+ key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
+ if err != nil {
+ return nil, err
+ }
+ ed, ok := key.(ed25519.PrivateKey)
+ if !ok {
+ return nil, fmt.Errorf("the key is a %T; GOVERNMENT signs with Ed25519", key)
+ }
+ seed := [32]byte(ed.Seed())
+ return &seed, nil
+}