implement tls client certificate authentication

the imap & smtp servers now allow logging in with tls client authentication and
the "external" sasl authentication mechanism. email clients like thunderbird,
fairemail, k9, macos mail implement it. this seems to be the most secure among
the authentication mechanism commonly implemented by clients. a useful property
is that an account can have a separate tls public key for each device/email
client.  with tls client cert auth, authentication is also bound to the tls
connection. a mitm cannot pass the credentials on to another tls connection,
similar to scram-*-plus. though part of scram-*-plus is that clients verify
that the server knows the client credentials.

for tls client auth with imap, we send a "preauth" untagged message by default.
that puts the connection in authenticated state. given the imap connection
state machine, further authentication commands are not allowed. some clients
don't recognize the preauth message, and try to authenticate anyway, which
fails. a tls public key has a config option to disable preauth, keeping new
connections in unauthenticated state, to work with such email clients.

for smtp (submission), we don't require an explicit auth command.

both for imap and smtp, we allow a client to authenticate with another
mechanism than "external". in that case, credentials are verified, and have to
be for the same account as the tls client auth, but the adress can be another
one than the login address configured with the tls public key.

only the public key is used to identify the account that is authenticating. we
ignore the rest of the certificate. expiration dates, names, constraints, etc
are not verified. no certificate authorities are involved.

users can upload their own (minimal) certificate. the account web interface
shows openssl commands you can run to generate a private key, minimal cert, and
a p12 file (the format that email clients seem to like...) containing both
private key and certificate.

the imapclient & smtpclient packages can now also use tls client auth. and so
does "mox sendmail", either with a pem file with private key and certificate,
or with just an ed25519 private key.

there are new subcommands "mox config tlspubkey ..." for
adding/removing/listing tls public keys from the cli, by the admin.
This commit is contained in:
Mechiel Lukkien
2024-12-05 22:41:49 +01:00
parent 5f7831a7f0
commit 8804d6b60e
38 changed files with 2737 additions and 309 deletions

View File

@ -26,6 +26,7 @@ import (
"github.com/mjl-/mox/mox-"
"github.com/mjl-/mox/mtasts"
"github.com/mjl-/mox/smtp"
"github.com/mjl-/mox/store"
)
var pkglog = mlog.New("admin", nil)
@ -514,6 +515,18 @@ func DomainRemove(ctx context.Context, domain dns.Domain) (rerr error) {
return fmt.Errorf("%w: domain does not exist", ErrRequest)
}
// Check that the domain isn't referenced in a TLS public key.
tlspubkeys, err := store.TLSPublicKeyList(ctx, "")
if err != nil {
return fmt.Errorf("%w: listing tls public keys: %s", ErrRequest, err)
}
atdom := "@" + domain.Name()
for _, tpk := range tlspubkeys {
if strings.HasSuffix(tpk.LoginAddress, atdom) {
return fmt.Errorf("%w: domain is still referenced in tls public key by login address %q of account %q, change or remove it first", ErrRequest, tpk.LoginAddress, tpk.Account)
}
}
// Compose new config without modifying existing data structures. If we fail, we
// leave no trace.
nc := c
@ -720,6 +733,11 @@ func AccountRemove(ctx context.Context, account string) (rerr error) {
return fmt.Errorf("account removed, its data directory moved to %q, but removing failed: %v", odir, err)
}
if err := store.TLSPublicKeyRemoveForAccount(context.Background(), account); err != nil {
log.Errorx("removing tls public keys for removed account", err)
return fmt.Errorf("account removed, but removing tls public keys failed: %v", err)
}
log.Info("account removed", slog.String("account", account))
return nil
}
@ -851,7 +869,7 @@ func AddressRemove(ctx context.Context, address string) (rerr error) {
}
// Also remove matching address from FromIDLoginAddresses, composing a new slice.
var fromIDLoginAddresses []string
// Refuse if address is referenced in a TLS public key.
var dom dns.Domain
var pa smtp.Address // For non-catchall addresses (most).
var err error
@ -867,6 +885,12 @@ func AddressRemove(ctx context.Context, address string) (rerr error) {
}
dom = pa.Domain
}
dc, ok := mox.Conf.Dynamic.Domains[dom.Name()]
if !ok {
return fmt.Errorf("%w: unknown domain in address %q", ErrRequest, address)
}
var fromIDLoginAddresses []string
for i, fa := range a.ParsedFromIDLoginAddresses {
if fa.Domain != dom {
// Keep for different domain.
@ -876,10 +900,6 @@ func AddressRemove(ctx context.Context, address string) (rerr error) {
if strings.HasPrefix(address, "@") {
continue
}
dc, ok := mox.Conf.Dynamic.Domains[dom.Name()]
if !ok {
return fmt.Errorf("%w: unknown domain in fromid login address %q", ErrRequest, fa.Pack(true))
}
flp := mox.CanonicalLocalpart(fa.Localpart, dc)
alp := mox.CanonicalLocalpart(pa.Localpart, dc)
if alp != flp {
@ -889,6 +909,23 @@ func AddressRemove(ctx context.Context, address string) (rerr error) {
}
na.FromIDLoginAddresses = fromIDLoginAddresses
// Refuse if there is still a TLS public key that references this address.
tlspubkeys, err := store.TLSPublicKeyList(ctx, ad.Account)
if err != nil {
return fmt.Errorf("%w: listing tls public keys for account: %v", ErrRequest, err)
}
for _, tpk := range tlspubkeys {
a, err := smtp.ParseAddress(tpk.LoginAddress)
if err != nil {
return fmt.Errorf("%w: parsing address from tls public key: %v", ErrRequest, err)
}
lp := mox.CanonicalLocalpart(a.Localpart, dc)
ca := smtp.NewAddress(lp, a.Domain)
if xad, ok := mox.Conf.AccountDestinationsLocked[ca.String()]; ok && xad.Localpart == ad.Localpart {
return fmt.Errorf("%w: tls public key %q references this address as login address %q, remove the tls public key before removing the address", ErrRequest, tpk.Fingerprint, tpk.LoginAddress)
}
}
// And remove as member from aliases configured in domains.
domains := maps.Clone(mox.Conf.Dynamic.Domains)
for _, aa := range na.Aliases {