mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
feat(acme): support EAB credentials (#39511)
ACME can now register accounts with CAs that require External Account Binding (EAB). Set `ACME_EAB_KID` and `ACME_EAB_HMAC` in `[server]`, or `ACME_EAB_HMAC_URI` to read the MAC key from a file. Retries for a custom `ACME_URL` no longer go to Let's Encrypt staging. Docs: https://gitea.com/gitea/docs/pulls/669 Fixes: https://github.com/go-gitea/gitea/issues/35549 Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: techknowlogick <164197+techknowlogick@users.noreply.github.com> Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
6 files changed
+164
-42
No files matched your search
+49
-37
@@ -21,6 +21,7 @@ import (
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
"github.com/mholt/acmez/v3/acme"
|
||||
)
|
||||
|
||||
func getCARoot(path string) (*x509.CertPool, error) {
|
||||
@@ -41,6 +42,53 @@ func getCARoot(path string) (*x509.CertPool, error) {
|
||||
return certPool, nil
|
||||
}
|
||||
|
||||
func newACMEConfig(enableHTTPChallenge, enableTLSALPNChallenge bool, altHTTPPort, altTLSALPNPort int) (*certmagic.Config, *certmagic.ACMEIssuer) {
|
||||
// Try to use private CA root if provided, otherwise defaults to system's trust
|
||||
var certPool *x509.CertPool
|
||||
if setting.AcmeCARoot != "" {
|
||||
var err error
|
||||
certPool, err = getCARoot(setting.AcmeCARoot)
|
||||
if err != nil {
|
||||
log.Warn("Failed to parse CA Root certificate, using default CA trust: %v", err)
|
||||
}
|
||||
}
|
||||
var externalAccount *acme.EAB
|
||||
if setting.AcmeEABHMAC != "" {
|
||||
externalAccount = &acme.EAB{KeyID: setting.AcmeEABKID, MACKey: setting.AcmeEABHMAC}
|
||||
}
|
||||
// FIXME: this path is not right, it uses "AppWorkPath" incorrectly, and writes the data into "AppWorkPath/https"
|
||||
// Ideally it should migrate to AppDataPath write to "AppDataPath/https"
|
||||
// And one more thing, no idea why we should set the global default variables here
|
||||
// But it seems that the current ACME code needs these global variables to make renew work.
|
||||
// Otherwise, "renew" will use incorrect storage path
|
||||
oldDefaultACME := certmagic.DefaultACME
|
||||
ca := util.IfZero(setting.AcmeURL, oldDefaultACME.CA)
|
||||
certmagic.Default.Storage = &certmagic.FileStorage{Path: setting.AcmeLiveDirectory}
|
||||
certmagic.DefaultACME = certmagic.ACMEIssuer{
|
||||
// try to use the default values provided by DefaultACME
|
||||
CA: ca,
|
||||
TestCA: util.Iif(ca == oldDefaultACME.CA, oldDefaultACME.TestCA, ""), // a custom CA must not retry against Let's Encrypt staging
|
||||
Logger: oldDefaultACME.Logger,
|
||||
HTTPProxy: oldDefaultACME.HTTPProxy,
|
||||
|
||||
TrustedRoots: certPool,
|
||||
Email: setting.AcmeEmail,
|
||||
Agreed: setting.AcmeTOS,
|
||||
Profile: setting.AcmeProfile,
|
||||
ExternalAccount: externalAccount,
|
||||
DisableHTTPChallenge: !enableHTTPChallenge,
|
||||
DisableTLSALPNChallenge: !enableTLSALPNChallenge,
|
||||
ListenHost: setting.HTTPAddr,
|
||||
AltTLSALPNPort: altTLSALPNPort,
|
||||
AltHTTPPort: altHTTPPort,
|
||||
}
|
||||
|
||||
magic := certmagic.NewDefault()
|
||||
myACME := certmagic.NewACMEIssuer(magic, certmagic.DefaultACME)
|
||||
magic.Issuers = []certmagic.Issuer{myACME}
|
||||
return magic, myACME
|
||||
}
|
||||
|
||||
func runACME(listenAddr string, m http.Handler) error {
|
||||
// If HTTP Challenge enabled, needs to be serving on port 80. For TLSALPN needs 443.
|
||||
// Due to docker port mapping this can't be checked programmatically
|
||||
@@ -57,43 +105,7 @@ func runACME(listenAddr string, m http.Handler) error {
|
||||
altTLSALPNPort = p
|
||||
}
|
||||
|
||||
// Try to use private CA root if provided, otherwise defaults to system's trust
|
||||
var certPool *x509.CertPool
|
||||
if setting.AcmeCARoot != "" {
|
||||
var err error
|
||||
certPool, err = getCARoot(setting.AcmeCARoot)
|
||||
if err != nil {
|
||||
log.Warn("Failed to parse CA Root certificate, using default CA trust: %v", err)
|
||||
}
|
||||
}
|
||||
// FIXME: this path is not right, it uses "AppWorkPath" incorrectly, and writes the data into "AppWorkPath/https"
|
||||
// Ideally it should migrate to AppDataPath write to "AppDataPath/https"
|
||||
// And one more thing, no idea why we should set the global default variables here
|
||||
// But it seems that the current ACME code needs these global variables to make renew work.
|
||||
// Otherwise, "renew" will use incorrect storage path
|
||||
oldDefaultACME := certmagic.DefaultACME
|
||||
certmagic.Default.Storage = &certmagic.FileStorage{Path: setting.AcmeLiveDirectory}
|
||||
certmagic.DefaultACME = certmagic.ACMEIssuer{
|
||||
// try to use the default values provided by DefaultACME
|
||||
CA: util.IfZero(setting.AcmeURL, oldDefaultACME.CA),
|
||||
TestCA: oldDefaultACME.TestCA,
|
||||
Logger: oldDefaultACME.Logger,
|
||||
HTTPProxy: oldDefaultACME.HTTPProxy,
|
||||
|
||||
TrustedRoots: certPool,
|
||||
Email: setting.AcmeEmail,
|
||||
Agreed: setting.AcmeTOS,
|
||||
Profile: setting.AcmeProfile,
|
||||
DisableHTTPChallenge: !enableHTTPChallenge,
|
||||
DisableTLSALPNChallenge: !enableTLSALPNChallenge,
|
||||
ListenHost: setting.HTTPAddr,
|
||||
AltTLSALPNPort: altTLSALPNPort,
|
||||
AltHTTPPort: altHTTPPort,
|
||||
}
|
||||
|
||||
magic := certmagic.NewDefault()
|
||||
myACME := certmagic.NewACMEIssuer(magic, certmagic.DefaultACME)
|
||||
magic.Issuers = []certmagic.Issuer{myACME}
|
||||
magic, myACME := newACMEConfig(enableHTTPChallenge, enableTLSALPNChallenge, altHTTPPort, altTLSALPNPort)
|
||||
|
||||
// Obtain certificates or renew them if necessary. ManageSync fails closed on
|
||||
// renewal errors even when a still-valid certificate is already on disk, which
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"io"
|
||||
stdlog "log"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
"github.com/letsencrypt/pebble/v2/ca"
|
||||
"github.com/letsencrypt/pebble/v2/db"
|
||||
"github.com/letsencrypt/pebble/v2/va"
|
||||
"github.com/letsencrypt/pebble/v2/wfe"
|
||||
"github.com/mholt/acmez/v3/acme"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type noopDNS01Solver struct{}
|
||||
|
||||
func (noopDNS01Solver) Present(_ctx context.Context, _challenge acme.Challenge) error { return nil }
|
||||
|
||||
func (noopDNS01Solver) CleanUp(_ctx context.Context, _challenge acme.Challenge) error { return nil }
|
||||
|
||||
func TestACMEIssuer(t *testing.T) {
|
||||
const keyID, macKey = "kid-1", "zWNDZM6eQGHWpSRTPal5eIUYFTu7EajVIoguysqZ9wG44nMEtx3MUAsUDkMTQ12W"
|
||||
t.Cleanup(test.MockVariableValue(&certmagic.DefaultACME))
|
||||
t.Cleanup(test.MockVariableValue(&certmagic.Default))
|
||||
t.Cleanup(test.MockVariableValue(&setting.AcmeURL, ""))
|
||||
t.Cleanup(test.MockVariableValue(&setting.AcmeCARoot, ""))
|
||||
t.Cleanup(test.MockVariableValue(&setting.AcmeEABKID, ""))
|
||||
t.Cleanup(test.MockVariableValue(&setting.AcmeEABHMAC, ""))
|
||||
t.Cleanup(test.MockVariableValue(&setting.AcmeLiveDirectory, t.TempDir()))
|
||||
|
||||
_, issuer := newACMEConfig(false, false, 0, 0)
|
||||
assert.Equal(t, certmagic.LetsEncryptProductionCA, issuer.CA)
|
||||
assert.Equal(t, certmagic.LetsEncryptStagingCA, issuer.TestCA)
|
||||
assert.Nil(t, issuer.ExternalAccount)
|
||||
|
||||
setting.AcmeURL = certmagic.LetsEncryptProductionCA
|
||||
_, issuer = newACMEConfig(false, false, 0, 0)
|
||||
assert.Equal(t, certmagic.LetsEncryptStagingCA, issuer.TestCA)
|
||||
|
||||
t.Setenv("PEBBLE_VA_ALWAYS_VALID", "1")
|
||||
t.Setenv("PEBBLE_VA_NOSLEEP", "1")
|
||||
t.Setenv("PEBBLE_WFE_NONCEREJECT", "0")
|
||||
logger := stdlog.New(io.Discard, "", 0)
|
||||
store := db.NewMemoryStore()
|
||||
require.NoError(t, store.AddExternalAccountKeyByID(keyID, macKey))
|
||||
pebbleCA := ca.New(logger, store, "", "ecdsa", 0, 1, map[string]ca.Profile{"default": {}})
|
||||
pebbleVA := va.New(logger, 0, 0, false, "", store)
|
||||
pebbleWFE := wfe.New(logger, store, pebbleVA, pebbleCA, nil, false, true, 0, 0)
|
||||
server := httptest.NewTLSServer(pebbleWFE.Handler())
|
||||
t.Cleanup(server.Close)
|
||||
setting.AcmeURL = server.URL + wfe.DirectoryPath
|
||||
setting.AcmeCARoot = filepath.Join(setting.AcmeLiveDirectory, "ca.pem")
|
||||
require.NoError(t, os.WriteFile(setting.AcmeCARoot, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: server.Certificate().Raw}), 0o600))
|
||||
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
csrDER, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{DNSNames: []string{"gitea.example.com"}}, key)
|
||||
require.NoError(t, err)
|
||||
csr, err := x509.ParseCertificateRequest(csrDER)
|
||||
require.NoError(t, err)
|
||||
|
||||
setting.AcmeEABKID, setting.AcmeEABHMAC = keyID, "b10lLJs8l1GPIzsLP0s6pMt8O0XVGnfTaCeROxQM0BIt2XrJMDHJZBM5NuQmQJQH"
|
||||
_, issuer = newACMEConfig(false, false, 0, 0)
|
||||
assert.Empty(t, issuer.TestCA)
|
||||
_, err = issuer.Issue(t.Context(), csr)
|
||||
assert.ErrorContains(t, err, "external account binding JWS verification error")
|
||||
|
||||
setting.AcmeEABHMAC = macKey
|
||||
_, issuer = newACMEConfig(false, false, 0, 0)
|
||||
issuer.DNS01Solver = noopDNS01Solver{}
|
||||
_, err = issuer.Issue(t.Context(), csr)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
@@ -264,6 +264,15 @@
|
||||
;; ACME profile to request from the CA (e.g. "shortlived" for raw-IP certificates)
|
||||
;ACME_PROFILE =
|
||||
;;
|
||||
;; External Account Binding (EAB) key ID, required by some CAs. Only used when registering a new ACME account.
|
||||
;ACME_EAB_KID =
|
||||
;;
|
||||
;; EAB MAC key in base64url encoding. Must be set together with ACME_EAB_KID.
|
||||
;ACME_EAB_HMAC =
|
||||
;;
|
||||
;; Alternative location to specify the EAB MAC key. You cannot specify both this and ACME_EAB_HMAC, and must pick one
|
||||
;ACME_EAB_HMAC_URI = file:/etc/gitea/acme_eab_hmac
|
||||
;;
|
||||
;; ACME live directory (not to be confused with ACME directory URL: ACME_URL)
|
||||
;; (Refer to caddy's ACME manager https://github.com/caddyserver/certmagic)
|
||||
;ACME_DIRECTORY = https
|
||||
|
||||
@@ -55,11 +55,13 @@ require (
|
||||
github.com/jhillyerd/enmime/v2 v2.5.0
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
|
||||
github.com/klauspost/compress v1.20.5
|
||||
github.com/letsencrypt/pebble/v2 v2.10.3
|
||||
github.com/lib/pq v1.12.3
|
||||
github.com/markbates/goth v1.82.0
|
||||
github.com/mattn/go-isatty v0.0.24
|
||||
github.com/mattn/go-sqlite3 v1.14.53
|
||||
github.com/meilisearch/meilisearch-go v0.37.0
|
||||
github.com/mholt/acmez/v3 v3.1.7
|
||||
github.com/mholt/archives v0.1.5
|
||||
github.com/microcosm-cc/bluemonday v1.0.27
|
||||
github.com/microsoft/go-mssqldb v1.12.0
|
||||
@@ -149,6 +151,7 @@ require (
|
||||
github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1 // indirect
|
||||
github.com/go-asn1-ber/asn1-ber v1.5.8 // indirect
|
||||
github.com/go-enry/go-oniguruma v1.2.1 // indirect
|
||||
github.com/go-jose/go-jose/v4 v4.1.8 // indirect
|
||||
github.com/go-openapi/jsonpointer v0.23.1 // indirect
|
||||
github.com/go-openapi/swag/jsonname v0.26.1 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
|
||||
@@ -171,11 +174,11 @@ require (
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||
github.com/klauspost/pgzip v1.2.6 // indirect
|
||||
github.com/letsencrypt/challtestsrv v1.4.2 // indirect
|
||||
github.com/libdns/libdns v1.1.1 // indirect
|
||||
github.com/markbates/going v1.0.3 // indirect
|
||||
github.com/mattn/go-colorable v0.1.15 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.24 // indirect
|
||||
github.com/mholt/acmez/v3 v3.1.7 // indirect
|
||||
github.com/miekg/dns v1.1.73 // indirect
|
||||
github.com/mikelolasagasti/xz v1.0.1 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
|
||||
@@ -202,8 +202,8 @@ github.com/go-enry/go-enry/v2 v2.9.6 h1:np63eOtMV56zfYDHnFVgpEVOk8fr2kmylcMnAZUD
|
||||
github.com/go-enry/go-enry/v2 v2.9.6/go.mod h1:9yrj4ES1YrbNb1Wb7/PWYr2bpaCXUGRt0uafN0ISyG8=
|
||||
github.com/go-enry/go-oniguruma v1.2.1 h1:k8aAMuJfMrqm/56SG2lV9Cfti6tC4x8673aHCcBk+eo=
|
||||
github.com/go-enry/go-oniguruma v1.2.1/go.mod h1:bWDhYP+S6xZQgiRL7wlTScFYBe023B6ilRZbCAD5Hf4=
|
||||
github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA=
|
||||
github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
|
||||
github.com/go-jose/go-jose/v4 v4.1.8 h1:k38WifTutpGuqt6Imq9xW1OgCvnWFri0Nz7FnfiACM8=
|
||||
github.com/go-jose/go-jose/v4 v4.1.8/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
|
||||
github.com/go-ldap/ldap/v3 v3.4.15 h1:eRB4f4Ecn1POL1mO86+uUDlecmyWsdiJwh60bAUS4UY=
|
||||
github.com/go-ldap/ldap/v3 v3.4.15/go.mod h1:jeD0c98EdZQCKKKKe4kTWs5aCf6y+j8KuYsYLznZSQk=
|
||||
github.com/go-openapi/jsonpointer v0.23.1 h1:1HBACs7XIwR2RcmItfdSFlALhGbe6S92p0ry4d1GWg4=
|
||||
@@ -367,8 +367,8 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
|
||||
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
|
||||
github.com/letsencrypt/challtestsrv v1.4.2 h1:0ON3ldMhZyWlfVNYYpFuWRTmZNnyfiL9Hh5YzC3JVwU=
|
||||
github.com/letsencrypt/challtestsrv v1.4.2/go.mod h1:GhqMqcSoeGpYd5zX5TgwA6er/1MbWzx/o7yuuVya+Wk=
|
||||
github.com/letsencrypt/pebble/v2 v2.10.1 h1:oKHx3lgN4e5Nno2LKTMrVx+b+NkDptkO9aDireiBDGE=
|
||||
github.com/letsencrypt/pebble/v2 v2.10.1/go.mod h1:KtYhQ4YTjT5MtoCZ6RTCXlbrrz6cKyXROCuTpIUDJFY=
|
||||
github.com/letsencrypt/pebble/v2 v2.10.3 h1:Zg/Rq9T8FvNMvfLvi3k+dXmrwgDnG9neRpeaJY2hwVM=
|
||||
github.com/letsencrypt/pebble/v2 v2.10.3/go.mod h1:t86OY665ATaOG3amRg83FSFMp7/L8MWjvlzBFmXANYc=
|
||||
github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ=
|
||||
github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
|
||||
github.com/libdns/libdns v1.1.1 h1:wPrHrXILoSHKWJKGd0EiAVmiJbFShguILTg9leS/P/U=
|
||||
|
||||
@@ -102,6 +102,8 @@ var (
|
||||
AcmeEmail string
|
||||
AcmeURL string
|
||||
AcmeProfile string
|
||||
AcmeEABKID string
|
||||
AcmeEABHMAC string
|
||||
AcmeCARoot string
|
||||
SSLMinimumVersion string
|
||||
SSLMaximumVersion string
|
||||
@@ -166,6 +168,11 @@ func loadServerFrom(rootCfg ConfigProvider) {
|
||||
if EnableAcme {
|
||||
AcmeURL = sec.Key("ACME_URL").MustString("")
|
||||
AcmeProfile = sec.Key("ACME_PROFILE").MustString("")
|
||||
AcmeEABKID = sec.Key("ACME_EAB_KID").String()
|
||||
AcmeEABHMAC = strings.TrimRight(loadSecret(sec, "ACME_EAB_HMAC_URI", "ACME_EAB_HMAC"), "=")
|
||||
if (AcmeEABKID == "") != (AcmeEABHMAC == "") {
|
||||
log.Fatal("ACME_EAB_KID and ACME_EAB_HMAC (or ACME_EAB_HMAC_URI) must be set together")
|
||||
}
|
||||
AcmeCARoot = sec.Key("ACME_CA_ROOT").MustString("")
|
||||
|
||||
AcmeTOS = sec.Key("ACME_ACCEPTTOS").MustBool(false)
|
||||
|
||||
Reference in new issue
Block a user