mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
gemini: ask the sysop before making a certificate
The BBS no longer makes a Gemini certificate by itself at startup. bbs gemini cert asks for the host names (and addresses), organization and years, shows what it will make and where, and makes it only on a yes. It will not replace a certificate without -force, and keeps the old one as .old. Without one, bbs serve stops and says to run it, and bbs check says there is none yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRSz48BzvGbSP2U45F8
This commit is contained in:
12 files changed
+332
-65
No files matched your search
+5
-4
@@ -870,11 +870,12 @@ address = 127.0.0.1:2513
|
||||
; no login, so anyone sees them. Port 1965 needs no privilege
|
||||
;[gemini]
|
||||
;address = :1965
|
||||
; the name a certificate made here is for
|
||||
; the host name bbs gemini cert offers for a new certificate
|
||||
;hostname = bbs.example.org
|
||||
; the certificate and key (PEM); with neither, a self-signed one is made
|
||||
; beside the database (gemini-cert.pem, gemini-key.pem) at the first start
|
||||
; and kept: clients trust it on first use and warn if it ever changes
|
||||
; the certificate and key (PEM); with neither, gemini-cert.pem and
|
||||
; gemini-key.pem beside the database, which bbs gemini cert makes (it asks
|
||||
; for the host names, organization and years first). Clients trust it on
|
||||
; first use and warn if it ever changes
|
||||
;cert_file = /etc/bbs/gemini-cert.pem
|
||||
;key_file = /etc/bbs/gemini-key.pem
|
||||
; message areas shown (a wildmat of area names; "": none)
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/gemini"
|
||||
"github.com/D-Modem/bbs/internal/server"
|
||||
)
|
||||
|
||||
// geminiCmd is bbs gemini: cert makes the Gemini server's certificate,
|
||||
// asking the sysop what goes in it.
|
||||
func geminiCmd(cfg *server.Config, args []string) {
|
||||
if len(args) == 0 || args[0] != "cert" {
|
||||
usage()
|
||||
}
|
||||
fs := flag.NewFlagSet("gemini cert", flag.ExitOnError)
|
||||
force := fs.Bool("force", false, "replace a certificate there already (clients will warn)")
|
||||
fs.Parse(args[1:])
|
||||
g := cfg.Gemini
|
||||
if g == nil {
|
||||
fatal(errors.New("no [gemini] section in the configuration"))
|
||||
}
|
||||
if err := geminiCert(g, cfg.Settings.Name, *force, os.Stdin, os.Stderr); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// geminiCert asks on in (prompts to out) for the host names, organization
|
||||
// and years of a new certificate, shows what it will be, and makes it
|
||||
// once the sysop says yes.
|
||||
func geminiCert(g *gemini.Settings, org string, force bool, in io.Reader, out io.Writer) error {
|
||||
_, cerr := os.Stat(g.CertFile)
|
||||
_, kerr := os.Stat(g.KeyFile)
|
||||
there := cerr == nil || kerr == nil
|
||||
if there && !force {
|
||||
return fmt.Errorf("there is a certificate already (%s: %s); -force to replace it, and clients that know it will warn",
|
||||
g.CertFile, gemini.CertInfo(g.CertFile))
|
||||
}
|
||||
r := bufio.NewReader(in)
|
||||
ask := func(q, def string) (string, error) {
|
||||
if def != "" {
|
||||
fmt.Fprintf(out, "%s [%s]: ", q, def)
|
||||
} else {
|
||||
fmt.Fprintf(out, "%s: ", q)
|
||||
}
|
||||
line, err := r.ReadString('\n')
|
||||
if err != nil && line == "" {
|
||||
return "", errors.New("no answer")
|
||||
}
|
||||
if line = strings.TrimSpace(line); line != "" {
|
||||
return line, nil
|
||||
}
|
||||
return def, nil
|
||||
}
|
||||
fmt.Fprintln(out, "A certificate for the Gemini server, made here and signed by itself.")
|
||||
fmt.Fprintln(out, "Clients trust it the first time they connect and warn if it ever changes:")
|
||||
fmt.Fprintln(out, "make it once, and keep it (and its key) with your backups.")
|
||||
var req gemini.CertRequest
|
||||
for {
|
||||
names, err := ask("Host names callers use, by commas", g.Host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Names = nil
|
||||
for _, n := range strings.Split(names, ",") {
|
||||
if n = strings.TrimSpace(n); n != "" {
|
||||
req.Names = append(req.Names, strings.ToLower(n))
|
||||
}
|
||||
}
|
||||
if req.Org, err = ask("Organization (- for none)", org); err != nil {
|
||||
return err
|
||||
}
|
||||
if req.Org == "-" {
|
||||
req.Org = ""
|
||||
}
|
||||
years, err := ask("Years it is good for", "20")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if req.Years, err = strconv.Atoi(years); err != nil {
|
||||
req.Years = 0
|
||||
}
|
||||
if err := req.Check(); err != nil {
|
||||
fmt.Fprintln(out, err)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
fmt.Fprintf(out, "\nFor: %s\n", strings.Join(req.Names, ", "))
|
||||
if req.Org != "" {
|
||||
fmt.Fprintf(out, "Org: %s\n", req.Org)
|
||||
}
|
||||
fmt.Fprintf(out, "Until: %s\n", time.Now().AddDate(req.Years, 0, 0).Format("2006-01-02"))
|
||||
fmt.Fprintf(out, "Files: %s, %s\n", g.CertFile, g.KeyFile)
|
||||
if there {
|
||||
fmt.Fprintln(out, "Replaces: "+gemini.CertInfo(g.CertFile)+" (kept as .old)")
|
||||
}
|
||||
yes, err := ask("Make it? (yes/no)", "no")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if y := strings.ToLower(yes); y != "y" && y != "yes" {
|
||||
return errors.New("not made")
|
||||
}
|
||||
if there {
|
||||
for _, f := range []string{g.CertFile, g.KeyFile} {
|
||||
if err := os.Rename(f, f+".old"); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := gemini.MakeCert(g.CertFile, g.KeyFile, req); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(out, "Made: "+gemini.CertInfo(g.CertFile))
|
||||
fmt.Fprintln(out, "The BBS serves it from its next start.")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/gemini"
|
||||
)
|
||||
|
||||
// TestGeminiCert: bbs gemini cert asks, shows what it will make and makes
|
||||
// it only on a yes; asks again after a bad answer; refuses to replace one
|
||||
// without -force, and with it keeps the old one as .old.
|
||||
func TestGeminiCert(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
g := &gemini.Settings{Host: "bbs.example.org", CertFile: filepath.Join(dir, "c.pem"), KeyFile: filepath.Join(dir, "k.pem")}
|
||||
var out bytes.Buffer
|
||||
if err := geminiCert(g, "Test BBS", false, strings.NewReader("\n\n\nno\n"), &out); err == nil {
|
||||
t.Error("made on a no")
|
||||
}
|
||||
if _, err := os.Stat(g.CertFile); err == nil {
|
||||
t.Error("a file made on a no")
|
||||
}
|
||||
if !strings.Contains(out.String(), "Host names callers use, by commas [bbs.example.org]: ") ||
|
||||
!strings.Contains(out.String(), "Organization (- for none) [Test BBS]: ") || !strings.Contains(out.String(), "For: bbs.example.org\n") {
|
||||
t.Errorf("prompts:\n%s", out.String())
|
||||
}
|
||||
out.Reset()
|
||||
in := "gemini.example.org, 192.0.2.7\n-\n0\nGemini.Example.org\nMy BBS\n3\nyes\n"
|
||||
if err := geminiCert(g, "Test BBS", false, strings.NewReader(in), &out); err != nil {
|
||||
t.Fatalf("%v\n%s", err, out.String())
|
||||
}
|
||||
if !strings.Contains(out.String(), "years: 1 to 100") || !strings.Contains(out.String(), "Made: gemini.example.org until ") {
|
||||
t.Errorf("output:\n%s", out.String())
|
||||
}
|
||||
first, _ := os.ReadFile(g.CertFile)
|
||||
if err := geminiCert(g, "Test BBS", false, strings.NewReader("\n\n\nyes\n"), &out); err == nil ||
|
||||
!strings.Contains(err.Error(), "-force") {
|
||||
t.Errorf("replaced without -force: %v", err)
|
||||
}
|
||||
if err := geminiCert(g, "Test BBS", true, strings.NewReader("\n\n\nyes\n"), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if old, _ := os.ReadFile(g.CertFile + ".old"); !bytes.Equal(old, first) {
|
||||
t.Error("old certificate not kept")
|
||||
}
|
||||
if now, _ := os.ReadFile(g.CertFile); bytes.Equal(now, first) {
|
||||
t.Error("not replaced")
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -163,6 +163,8 @@ func usage() {
|
||||
bbs [-config FILE] chan board list | del DIR del takes its posts and images too
|
||||
bbs [-config FILE] chan ban add ADDRESS DAYS [BOARD|-] [REASON...] (0 days: for ever)
|
||||
bbs [-config FILE] chan ban list | del ID
|
||||
bbs [-config FILE] gemini cert [-force] make the Gemini server's certificate (it asks
|
||||
for host names, organization and years)
|
||||
`)
|
||||
os.Exit(2)
|
||||
}
|
||||
@@ -317,7 +319,7 @@ func main() {
|
||||
}
|
||||
if g := cfg.Gemini; g != nil {
|
||||
fmt.Printf("gemini: %s, areas %q, files %v, users %v\n", g.Address, g.Areas, g.Files, g.Users)
|
||||
fmt.Printf("gemini certificate %s: %s\n", g.CertFile, gemini.CertInfo(g.CertFile, g.MakeCert))
|
||||
fmt.Printf("gemini certificate %s: %s\n", g.CertFile, gemini.CertInfo(g.CertFile))
|
||||
}
|
||||
for _, d := range cfg.Doors {
|
||||
if r := d.Remote; r != nil {
|
||||
@@ -376,6 +378,8 @@ func main() {
|
||||
timeCmd(cfg, args)
|
||||
case "chan":
|
||||
chanCmd(cfg, args)
|
||||
case "gemini":
|
||||
geminiCmd(cfg, args)
|
||||
default:
|
||||
usage()
|
||||
}
|
||||
|
||||
@@ -662,9 +662,9 @@ gemini: gopher's capsule over Gemini (gemini://, TLS): about the system, who is
|
||||
| key | default | live | what it does |
|
||||
|---|---|---|---|
|
||||
| `address` | | | See the section above. Examples: `:1965`. |
|
||||
| `hostname` | | | the name a certificate made here is for. Examples: `bbs.example.org`. |
|
||||
| `cert_file` | | | the certificate and key (PEM); with neither, a self-signed one is made beside the database (gemini-cert.pem, gemini-key.pem) at the first start and kept: clients trust it on first use and warn if it ever changes. Examples: `/etc/bbs/gemini-cert.pem`. |
|
||||
| `key_file` | | | the certificate and key (PEM); with neither, a self-signed one is made beside the database (gemini-cert.pem, gemini-key.pem) at the first start and kept: clients trust it on first use and warn if it ever changes. Examples: `/etc/bbs/gemini-key.pem`. |
|
||||
| `hostname` | | | the host name bbs gemini cert offers for a new certificate. Examples: `bbs.example.org`. |
|
||||
| `cert_file` | | | the certificate and key (PEM); with neither, gemini-cert.pem and gemini-key.pem beside the database, which bbs gemini cert makes (it asks for the host names, organization and years first). Clients trust it on first use and warn if it ever changes. Examples: `/etc/bbs/gemini-cert.pem`. |
|
||||
| `key_file` | | | the certificate and key (PEM); with neither, gemini-cert.pem and gemini-key.pem beside the database, which bbs gemini cert makes (it asks for the host names, organization and years first). Clients trust it on first use and warn if it ever changes. Examples: `/etc/bbs/gemini-key.pem`. |
|
||||
| `areas` | | | message areas shown (a wildmat of area names; "": none) Examples: `general,announcements`. |
|
||||
| `files` | | | file areas listed and downloadable. Examples: `no`. |
|
||||
| `users` | | | /users: look up a user, as finger tells of them. Examples: `yes`. |
|
||||
|
||||
@@ -139,7 +139,7 @@ and answering, the page command (and that it runs no shell); two callers
|
||||
chatting and paging on the text BBS; a 3270 (s3270) chatting with a text
|
||||
caller and getting a node message on its message line. Finger,
|
||||
gopher and Gemini: their answers and refusals, with the finger program and
|
||||
curl as clients, and Gemini's certificate made once and kept. Customization: finding display files by terminal, SAUCE,
|
||||
curl as clients, and `bbs gemini cert` asking before it makes a certificate. Customization: finding display files by terminal, SAUCE,
|
||||
@-codes and widths, colour tags; menus and strings in the configuration
|
||||
and what it refuses; a text caller through an ANSI logon (CP437 as it
|
||||
is), the strings, a welcome screen, the sysop's menus and submenus, a
|
||||
|
||||
@@ -77,8 +77,8 @@ $ lynx gopher://bbs.example.org
|
||||
```
|
||||
[gemini]
|
||||
address = :1965
|
||||
hostname = bbs.example.org ; the name a certificate made here is for
|
||||
;cert_file = /etc/bbs/gemini-cert.pem ; or neither, for one made here
|
||||
hostname = bbs.example.org ; offered by bbs gemini cert
|
||||
;cert_file = /etc/bbs/gemini-cert.pem ; or neither, for bbs gemini cert's
|
||||
;key_file = /etc/bbs/gemini-key.pem
|
||||
areas = general,announcements ; as gopher's
|
||||
files = no
|
||||
@@ -99,12 +99,38 @@ a link or a heading. A file goes as it is, typed by its name
|
||||
|
||||
Gemini clients trust a capsule's certificate the first time they see it
|
||||
and warn if it ever changes, so self-signed certificates are the usual
|
||||
thing. With no `cert_file` and `key_file`, the BBS makes one for
|
||||
`hostname` at its first start, beside the database (`gemini-cert.pem`,
|
||||
`gemini-key.pem`), and keeps it; `bbs check` shows its SHA-256
|
||||
fingerprint. Keep those two files with your backups: a new certificate
|
||||
makes every client that has been before warn. A certificate from a
|
||||
certificate authority (Let's Encrypt's, say) works too.
|
||||
thing. The BBS never makes one by itself: with no `cert_file` and
|
||||
`key_file`, make one with `bbs gemini cert` before starting it. It asks
|
||||
what goes in the certificate, shows it, and makes it only when you say
|
||||
yes:
|
||||
|
||||
```
|
||||
$ bbs -config /etc/bbs/bbs.conf gemini cert
|
||||
A certificate for the Gemini server, made here and signed by itself.
|
||||
Clients trust it the first time they connect and warn if it ever changes:
|
||||
make it once, and keep it (and its key) with your backups.
|
||||
Host names callers use, by commas [bbs.example.org]: bbs.example.org, gemini.example.org
|
||||
Organization (- for none) [D-Modem BBS]:
|
||||
Years it is good for [20]:
|
||||
|
||||
For: bbs.example.org, gemini.example.org
|
||||
Org: D-Modem BBS
|
||||
Until: 2046-11-09
|
||||
Files: /var/lib/bbs/gemini-cert.pem, /var/lib/bbs/gemini-key.pem
|
||||
Make it? (yes/no) [no]: yes
|
||||
Made: bbs.example.org, gemini.example.org until 2046-11-09, SHA-256 3F2A...
|
||||
```
|
||||
|
||||
The first host name is the certificate's common name; addresses (IPv4 or
|
||||
IPv6) can be listed too. The files go beside the database
|
||||
(`gemini-cert.pem`, `gemini-key.pem`). Keep them with your backups: a new
|
||||
certificate makes every client that has been before warn, which is why
|
||||
`bbs gemini cert` will not replace one without `-force` (and then keeps
|
||||
the old one as `.old`). `bbs check` shows the certificate's names, expiry
|
||||
and SHA-256 fingerprint, or that there is none yet; without one, `bbs
|
||||
serve` stops and says to make it. A certificate from a certificate
|
||||
authority (Let's Encrypt's, say) works too: set `cert_file` and
|
||||
`key_file`.
|
||||
|
||||
What gopher shows anyone, Gemini shows anyone: no areas and no files by
|
||||
default, `bbs check` warns when `areas` or `files` is set, and file areas
|
||||
|
||||
+66
-27
@@ -14,31 +14,53 @@ import (
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LoadCert loads the certificate and key; if mk is set and neither file
|
||||
// is there yet, it first makes them: a self-signed certificate for host,
|
||||
// good for 50 years. Gemini clients trust a capsule's certificate on first
|
||||
// use, so it is made once and kept, never made again behind the sysop's
|
||||
// back: a changed certificate is what clients warn of.
|
||||
func LoadCert(certFile, keyFile, host string, mk bool) (tls.Certificate, error) {
|
||||
_, cerr := os.Stat(certFile)
|
||||
_, kerr := os.Stat(keyFile)
|
||||
if mk && errors.Is(cerr, os.ErrNotExist) && errors.Is(kerr, os.ErrNotExist) {
|
||||
if err := MakeCert(certFile, keyFile, host); err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
}
|
||||
// LoadCert loads the certificate and its key. Nothing is made here: a
|
||||
// sysop makes a certificate with bbs gemini cert, saying what is in it.
|
||||
func LoadCert(certFile, keyFile string) (tls.Certificate, error) {
|
||||
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return tls.Certificate{}, fmt.Errorf("no certificate (%s): make one with bbs gemini cert, or set cert_file and key_file", certFile)
|
||||
}
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("certificate: %w", err)
|
||||
}
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
// MakeCert writes a new self-signed certificate for host and its key.
|
||||
func MakeCert(certFile, keyFile, host string) error {
|
||||
// CertRequest is what a sysop asks to be in a new certificate.
|
||||
type CertRequest struct {
|
||||
Names []string // host names and addresses; the first is its common name
|
||||
Org string // organization ("": none)
|
||||
Years int // how long it is good for
|
||||
}
|
||||
|
||||
// Check says what is wrong with a request, if anything.
|
||||
func (r CertRequest) Check() error {
|
||||
if len(r.Names) == 0 {
|
||||
return errors.New("a certificate needs a host name")
|
||||
}
|
||||
for _, n := range r.Names {
|
||||
if n == "" || strings.ContainsAny(n, " /:@") && net.ParseIP(n) == nil {
|
||||
return fmt.Errorf("%q is not a host name", n)
|
||||
}
|
||||
}
|
||||
if r.Years < 1 || r.Years > 100 {
|
||||
return errors.New("years: 1 to 100")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeCert writes a new self-signed certificate (ECDSA P-256) and its key.
|
||||
// Gemini clients trust a capsule's certificate the first time they see
|
||||
// it and warn if it changes, so neither file may be there already.
|
||||
func MakeCert(certFile, keyFile string, r CertRequest) error {
|
||||
if err := r.Check(); err != nil {
|
||||
return err
|
||||
}
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -50,16 +72,21 @@ func MakeCert(certFile, keyFile, host string) error {
|
||||
now := time.Now()
|
||||
tmpl := &x509.Certificate{
|
||||
SerialNumber: serial,
|
||||
Subject: pkix.Name{CommonName: host},
|
||||
Subject: pkix.Name{CommonName: r.Names[0]},
|
||||
NotBefore: now.Add(-time.Hour),
|
||||
NotAfter: now.AddDate(50, 0, 0),
|
||||
NotAfter: now.AddDate(r.Years, 0, 0),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
}
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
tmpl.IPAddresses = []net.IP{ip}
|
||||
} else {
|
||||
tmpl.DNSNames = []string{host}
|
||||
if r.Org != "" {
|
||||
tmpl.Subject.Organization = []string{r.Org}
|
||||
}
|
||||
for _, n := range r.Names {
|
||||
if ip := net.ParseIP(n); ip != nil {
|
||||
tmpl.IPAddresses = append(tmpl.IPAddresses, ip)
|
||||
} else {
|
||||
tmpl.DNSNames = append(tmpl.DNSNames, n)
|
||||
}
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
@@ -96,12 +123,12 @@ func write(name, kind string, b []byte, mode os.FileMode) error {
|
||||
}
|
||||
|
||||
// CertInfo describes the certificate for bbs check: whom it is for, until
|
||||
// when, and its SHA-256 fingerprint (what TOFU clients pin), or that it
|
||||
// will be made.
|
||||
func CertInfo(certFile string, mk bool) string {
|
||||
// when, and its SHA-256 fingerprint (what TOFU clients pin), or that there
|
||||
// is none yet.
|
||||
func CertInfo(certFile string) string {
|
||||
b, err := os.ReadFile(certFile)
|
||||
if errors.Is(err, os.ErrNotExist) && mk {
|
||||
return "none yet (made at the first start)"
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return "none yet: make one with bbs gemini cert"
|
||||
}
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
@@ -114,5 +141,17 @@ func CertInfo(certFile string, mk bool) string {
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return fmt.Sprintf("%s until %s, SHA-256 %X", c.Subject.CommonName, c.NotAfter.Format("2006-01-02"), sha256.Sum256(c.Raw))
|
||||
names := append([]string{}, c.DNSNames...)
|
||||
for _, ip := range c.IPAddresses {
|
||||
names = append(names, ip.String())
|
||||
}
|
||||
if len(names) == 0 {
|
||||
names = []string{c.Subject.CommonName}
|
||||
}
|
||||
return fmt.Sprintf("%s until %s, SHA-256 %s", strings.Join(names, ", "), c.NotAfter.Format("2006-01-02"), Fingerprint(c.Raw))
|
||||
}
|
||||
|
||||
// Fingerprint is a certificate's SHA-256, as clients show it.
|
||||
func Fingerprint(der []byte) string {
|
||||
return fmt.Sprintf("%X", sha256.Sum256(der))
|
||||
}
|
||||
@@ -29,10 +29,9 @@ import (
|
||||
// Settings are the [gemini] section's.
|
||||
type Settings struct {
|
||||
Address string
|
||||
Host string // the name the certificate is made for
|
||||
Host string // the name bbs gemini cert offers for a certificate
|
||||
CertFile string // the certificate and its key (PEM)
|
||||
KeyFile string
|
||||
MakeCert bool // made, self-signed, if not there (no cert_file given)
|
||||
Areas string // wildmat of the message areas shown ("": none)
|
||||
Files bool // file areas listed and downloadable
|
||||
Users bool // /users: finger's answer for a user
|
||||
@@ -65,7 +64,7 @@ const MaxRequest = 1024
|
||||
|
||||
// Run listens until ctx is done.
|
||||
func (s *Server) Run(ctx context.Context) error {
|
||||
cert, err := LoadCert(s.S.CertFile, s.S.KeyFile, s.S.Host, s.S.MakeCert)
|
||||
cert, err := LoadCert(s.S.CertFile, s.S.KeyFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("gemini: %w", err)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/bbs"
|
||||
"github.com/D-Modem/bbs/internal/files"
|
||||
@@ -58,7 +60,11 @@ func setup(t *testing.T, areas string, withFiles bool) *site {
|
||||
if s.file, err = up.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cert, err := LoadCert(filepath.Join(dir, "c.pem"), filepath.Join(dir, "k.pem"), "gemini.test", true)
|
||||
cf, kf := filepath.Join(dir, "c.pem"), filepath.Join(dir, "k.pem")
|
||||
if err := MakeCert(cf, kf, CertRequest{Names: []string{"gemini.test"}, Years: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cert, err := LoadCert(cf, kf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -179,33 +185,48 @@ func TestGemini(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCert: a certificate is made once and then kept; with mk unset, none
|
||||
// is made; a key alone is not overwritten.
|
||||
// TestCert: nothing is made by loading; a certificate has the names,
|
||||
// organization and years asked for; one there is never overwritten; bad
|
||||
// requests are refused.
|
||||
func TestCert(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
c, k := filepath.Join(dir, "c.pem"), filepath.Join(dir, "k.pem")
|
||||
if _, err := LoadCert(c, k, "bbs.example.org", false); err == nil {
|
||||
t.Error("loaded nothing")
|
||||
if _, err := LoadCert(c, k); err == nil || !strings.Contains(err.Error(), "bbs gemini cert") {
|
||||
t.Errorf("no certificate: %v", err)
|
||||
}
|
||||
if got := CertInfo(c, true); !strings.Contains(got, "none yet") {
|
||||
if got := CertInfo(c); !strings.Contains(got, "bbs gemini cert") {
|
||||
t.Errorf("info %q", got)
|
||||
}
|
||||
a, err := LoadCert(c, k, "bbs.example.org", true)
|
||||
req := CertRequest{Names: []string{"bbs.example.org", "gemini.example.org", "192.0.2.7"}, Org: "Test BBS", Years: 5}
|
||||
if err := MakeCert(c, k, req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cert, err := LoadCert(c, k)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := LoadCert(c, k, "bbs.example.org", true)
|
||||
if err != nil || !bytes.Equal(a.Certificate[0], b.Certificate[0]) {
|
||||
t.Errorf("made again: %v", err)
|
||||
x, _ := x509.ParseCertificate(cert.Certificate[0])
|
||||
if x.Subject.CommonName != "bbs.example.org" || strings.Join(x.DNSNames, ",") != "bbs.example.org,gemini.example.org" ||
|
||||
len(x.IPAddresses) != 1 || x.Subject.Organization[0] != "Test BBS" || x.NotAfter.Year() != time.Now().Year()+5 {
|
||||
t.Errorf("certificate %v %v %v %v %v", x.Subject, x.DNSNames, x.IPAddresses, x.Subject.Organization, x.NotAfter)
|
||||
}
|
||||
if got := CertInfo(c, true); !strings.HasPrefix(got, "bbs.example.org until ") || !strings.Contains(got, "SHA-256 ") {
|
||||
if got := CertInfo(c); !strings.HasPrefix(got, "bbs.example.org, gemini.example.org, 192.0.2.7 until ") ||
|
||||
!strings.Contains(got, "SHA-256 "+Fingerprint(x.Raw)) {
|
||||
t.Errorf("info %q", got)
|
||||
}
|
||||
if fi, _ := os.Stat(k); fi.Mode().Perm() != 0600 {
|
||||
t.Errorf("key mode %v", fi.Mode())
|
||||
}
|
||||
os.Remove(c)
|
||||
if _, err := LoadCert(c, k, "bbs.example.org", true); err == nil {
|
||||
t.Error("made a certificate for a key left alone")
|
||||
if err := MakeCert(c, k, req); err == nil {
|
||||
t.Error("made over one there")
|
||||
}
|
||||
if again, _ := LoadCert(c, k); !bytes.Equal(again.Certificate[0], cert.Certificate[0]) {
|
||||
t.Error("changed")
|
||||
}
|
||||
for _, bad := range []CertRequest{{Years: 1}, {Names: []string{"a b"}, Years: 1}, {Names: []string{"x"}, Years: 0},
|
||||
{Names: []string{"x"}, Years: 101}} {
|
||||
if err := MakeCert(filepath.Join(dir, "c2"), filepath.Join(dir, "k2"), bad); err == nil {
|
||||
t.Errorf("%+v made", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,9 +75,8 @@ func (c *Config) infoSections(cf *config.File) error {
|
||||
case g.CertFile == "" && g.KeyFile == "":
|
||||
g.CertFile = filepath.Join(c.DataDir(), "gemini-cert.pem")
|
||||
g.KeyFile = filepath.Join(c.DataDir(), "gemini-key.pem")
|
||||
g.MakeCert = true
|
||||
case g.CertFile == "" || g.KeyFile == "":
|
||||
return errors.New("[gemini] cert_file and key_file go together (or neither, for a certificate made here)")
|
||||
return errors.New("[gemini] cert_file and key_file go together (or neither, for one made by bbs gemini cert)")
|
||||
}
|
||||
g.Areas = s.String("areas", "")
|
||||
if g.Files, err = s.Bool("files", false); err != nil {
|
||||
|
||||
@@ -47,7 +47,7 @@ files = yes
|
||||
t.Errorf("defaults %+v %v", c.Gopher, err)
|
||||
}
|
||||
c, err = load("[general]\ndatabase = /var/lib/bbs/bbs.db\n[gemini]\nareas = *\n")
|
||||
if g := c.Gemini; err != nil || g.Address != ":1965" || !g.MakeCert || !g.Users || g.Areas != "*" ||
|
||||
if g := c.Gemini; err != nil || g.Address != ":1965" || !g.Users || g.Areas != "*" ||
|
||||
g.CertFile != "/var/lib/bbs/gemini-cert.pem" || g.KeyFile != "/var/lib/bbs/gemini-key.pem" {
|
||||
t.Errorf("gemini %+v %v", g, err)
|
||||
}
|
||||
@@ -55,7 +55,7 @@ files = yes
|
||||
t.Errorf("no gemini warning: %v", c.Warnings)
|
||||
}
|
||||
c, err = load("[gemini]\naddress = :1966\ncert_file = /etc/c.pem\nkey_file = /etc/k.pem\nusers = no\n")
|
||||
if g := c.Gemini; err != nil || g.MakeCert || g.Users || g.CertFile != "/etc/c.pem" || len(c.Warnings) != 0 {
|
||||
if g := c.Gemini; err != nil || g.Users || g.CertFile != "/etc/c.pem" || len(c.Warnings) != 0 {
|
||||
t.Errorf("gemini %+v %v %v", g, err, c.Warnings)
|
||||
}
|
||||
for body, want := range map[string]string{
|
||||
|
||||
Reference in new issue
Block a user