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

@ -298,7 +298,11 @@ const formatQuotaSize = (v: number) => {
}
const index = async () => {
const [acc, storageUsed, storageLimit, suppressions] = await client.Account()
const [[acc, storageUsed, storageLimit, suppressions], tlspubkeys0] = await Promise.all([
client.Account(),
client.TLSPublicKeys(),
])
const tlspubkeys = tlspubkeys0 || []
let fullNameForm: HTMLFormElement
let fullNameFieldset: HTMLFieldSetElement
@ -601,7 +605,7 @@ const index = async () => {
dom.div(
dom.h2('Parameters'),
dom.div(
style({marginBottom: '.5ex'}),
style({marginBottom: '.5ex'}),
dom.label(
'Event',
dom.div(
@ -872,6 +876,199 @@ const index = async () => {
),
dom.br(),
dom.h2('TLS public keys'),
dom.p('For TLS client authentication with certificates, for IMAP and/or submission (SMTP). Only the public key of the certificate is used during TLS authentication, to identify this account. Names, expiration or constraints are not verified.'),
(() => {
let elem = dom.div()
const preauthHelp = 'New IMAP immediate TLS connections authenticated with a client certificate are automatically switched to "authenticated" state with an untagged IMAP "preauth" message by default. IMAP connections have a state machine specifying when commands are allowed. Authenticating is not allowed while in the "authenticated" state. Enable this option to work around clients that would try to authenticated anyway.'
const render = () => {
const e = dom.div(
dom.table(
dom.thead(
dom.tr(
dom.th('Login address'),
dom.th('Name'),
dom.th('Type'),
dom.th('No IMAP "preauth"', attr.title(preauthHelp)),
dom.th('Fingerprint'),
dom.th('Update'),
dom.th('Remove'),
),
),
dom.tbody(
tlspubkeys.length === 0 ? dom.tr(dom.td(attr.colspan('7'), 'None')) : [],
tlspubkeys.map((tpk, index) => {
let loginAddress: HTMLInputElement
let name: HTMLInputElement
let noIMAPPreauth: HTMLInputElement
let update: HTMLButtonElement
const formID = 'tlk-'+index
const row = dom.tr(
dom.td(
dom.form(
attr.id(formID),
async function submit(e: SubmitEvent) {
e.stopPropagation()
e.preventDefault()
const ntpk: api.TLSPublicKey = {...tpk}
ntpk.LoginAddress = loginAddress.value
ntpk.Name = name.value
ntpk.NoIMAPPreauth = noIMAPPreauth.checked
await check(update, client.TLSPublicKeyUpdate(ntpk))
tpk.LoginAddress = ntpk.LoginAddress
tpk.Name = ntpk.Name
tpk.NoIMAPPreauth = ntpk.NoIMAPPreauth
},
loginAddress=dom.input(attr.type('email'), attr.value(tpk.LoginAddress), attr.required('')),
),
),
dom.td(name=dom.input(attr.form(formID), attr.value(tpk.Name), attr.required(''))),
dom.td(tpk.Type),
dom.td(dom.label(noIMAPPreauth=dom.input(attr.form(formID), attr.type('checkbox'), tpk.NoIMAPPreauth ? attr.checked('') : []), ' No IMAP "preauth"', attr.title(preauthHelp))),
dom.td(tpk.Fingerprint),
dom.td(update=dom.submitbutton(attr.form(formID), 'Update')),
dom.td(
dom.form(
async function submit(e: SubmitEvent & {target: {disabled: boolean}}) {
e.stopPropagation()
e.preventDefault()
await check(e.target, client.TLSPublicKeyRemove(tpk.Fingerprint))
tlspubkeys.splice(tlspubkeys.indexOf(tpk), 1)
render()
},
dom.submitbutton('Remove'),
),
),
)
return row
}),
),
),
dom.clickbutton('Add', style({marginTop: '1ex'}), function click() {
let address: HTMLInputElement
let name: HTMLInputElement
let noIMAPPreauth: HTMLInputElement
let file: HTMLInputElement
const close = popup(
dom.div(
style({maxWidth: '45em'}),
dom.h1('Add TLS public key'),
dom.form(
async function submit(e: SubmitEvent & {target: {disabled: boolean}}) {
e.preventDefault()
e.stopPropagation()
if (file.files?.length !== 1) {
throw new Error('exactly 1 certificate required') // xxx
}
const certPEM = await new Promise<string>((resolve, reject) => {
const fr = new window.FileReader()
fr.addEventListener('load', () => {
resolve(fr.result as string)
})
fr.addEventListener('error', () => {
reject(fr.error)
})
fr.readAsText(file.files![0])
})
const ntpk = await check(e.target, client.TLSPublicKeyAdd(address.value, name.value, noIMAPPreauth.checked, certPEM))
tlspubkeys.push(ntpk)
render()
close()
},
dom.label(
style({display: 'block', marginBottom: '1ex'}),
dom.div(dom.b('Login address')),
address=dom.input(attr.type('email'), attr.value(localStorageGet('webaccountaddress') || ''), attr.required('')),
dom.div(style({fontStyle: 'italic', marginTop: '.5ex'}), 'Login address used for sessions using this key.'),
),
dom.label(
style({display: 'block', marginBottom: '1ex'}),
noIMAPPreauth=dom.input(attr.type('checkbox')),
' No IMAP "preauth"',
attr.title(preauthHelp),
),
dom.div(
style({display: 'block', marginBottom: '1ex'}),
dom.label(
dom.div(dom.b('Certificate')),
file=dom.input(attr.type('file'), attr.required('')),
),
dom.p(
style({fontStyle: 'italic', margin: '1ex 0'}),
'Upload a PEM file containing a certificate, not a private key. Only the public key of the certificate is used during TLS authentication, to identify this account. Names, expiration, and constraints are not verified. ',
dom.a('Show suggested commands', attr.href(''), function click(e: MouseEvent) {
e.preventDefault()
popup(
dom.h1('Generate a private key and certificate'),
dom.pre(
dom._class('literal'),
`export keyname=... # Used for file names, certificate "common name" and as name of tls public key.
# Suggestion: Use an application name and/or email address.
export passphrase=... # Protects the private key in the PEM and p12 files.
# Generate an ECDSA P-256 private key and a long-lived, unsigned, basic certificate
# for the corresponding public key.
openssl req \\
-config /dev/null \\
-x509 \\
-newkey ec \\
-pkeyopt ec_paramgen_curve:P-256 \\
-passout env:passphrase \\
-keyout "$keyname.ecdsa-p256.privatekey.pkcs8.pem" \\
-out "$keyname.ecdsa-p256.certificate.pem" \\
-days 36500 \\
-subj "/CN=$keyname"
# Generate a p12 file containing both certificate and private key, for
# applications/operating systems that cannot read PEM files with
# certificates/private keys.
openssl pkcs12 \\
-export \\
-in "$keyname.ecdsa-p256.certificate.pem" \\
-inkey "$keyname.ecdsa-p256.privatekey.pkcs8.pem" \\
-name "$keyname" \\
-passin env:passphrase \\
-passout env:passphrase \\
-out "$keyname.ecdsa-p256-privatekey-certificate.p12"
# If the p12 file cannot be imported in the destination OS or email application,
# try adding -legacy to the "openssl pkcs12" command.
`
),
)
}),
' for generating a private key and certificate.',
),
),
dom.label(
style({display: 'block', marginBottom: '1ex'}),
dom.div(dom.b('Name')),
name=dom.input(),
dom.div(style({fontStyle: 'italic', marginTop: '.5ex'}), 'Optional. If empty, the "subject common name" from the certificate is used.'),
),
dom.br(),
dom.submitbutton('Add'),
),
),
)
})
)
if (elem) {
elem.replaceWith(e)
}
elem = e
}
render()
return elem
})(),
dom.br(),
dom.h2('Disk usage'),
dom.p('Storage used is ', dom.b(formatQuotaSize(Math.floor(storageUsed/(1024*1024))*1024*1024)),
storageLimit > 0 ? [