Skip to content

Commit 057402b

Browse files
committed
feat: add ca endpoints
See merge request https://ref.ci/fsrvcorp/pki/ocspcrl/-/merge_requests/6
2 parents 403ce69 + 45d4be3 commit 057402b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
OCSPCRL is a minimal implementation of both a OCSP and CRL server in Golang. It provides the following http endpoints:
44

5-
- `/ocsp` - OCSP responder
6-
- `/crl` - CRL responder
5+
| Endpoint | Description |
6+
|------------|----------------------------------------------------------|
7+
| `/ocsp` | OCSP responder supporting both `GET` and `POST` requests |
8+
| `/crl` | CRL responder in DER format |
9+
| `/crl.pem` | CRL responder in PEM format |
10+
| `/ca` | Issuer CA certificate in DER format |
11+
| `/ca.pem` | Issuer CA certificate in PEM format |
712

813
All what you need is to provide a CRL file, the root certificate and cert/key with extendedKeyUsage `OCSPSigning` to allow the OCSP server to sign the OCSP responses.
914
When using OCSP, the certificate is checked against the CRL for validity.

main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"encoding/pem"
8+
"errors"
89
"fmt"
910
"log"
1011
"net/http"
@@ -164,31 +165,39 @@ func main() {
164165
w.Header().Set("Content-Type", "application/pkix-crl")
165166
pem.Encode(w, &pem.Block{Type: "X509 CRL", Bytes: crl.Raw})
166167
})
168+
applicationRouter.HandleFunc("/ca", func(w http.ResponseWriter, r *http.Request) {
169+
w.Header().Set("Content-Type", "application/pkix-cert")
170+
w.Write(caCertificate.Raw)
171+
})
172+
applicationRouter.HandleFunc("/ca.pem", func(w http.ResponseWriter, r *http.Request) {
173+
w.Header().Set("Content-Type", "application/x-x509-ca-cert")
174+
pem.Encode(w, &pem.Block{Type: "CERTIFICATE", Bytes: caCertificate.Raw})
175+
})
167176

168177
applicationServer := &http.Server{Addr: config.applicationListenAddress, Handler: metrics.Middleware(applicationRouter)}
169-
metricsSever := &http.Server{Addr: config.metricsListenAddress, Handler: promhttp.Handler()}
178+
metricsServer := &http.Server{Addr: config.metricsListenAddress, Handler: promhttp.Handler()}
170179

171180
applicationServerClosed := make(chan any)
172181
metricsServerClosed := make(chan any)
173182
go func() {
174183
log.Printf("starting application server on %+q", config.applicationListenAddress)
175-
if listenError := applicationServer.ListenAndServe(); listenError != nil {
184+
if listenError := applicationServer.ListenAndServe(); !errors.Is(listenError, http.ErrServerClosed) {
176185
log.Printf("application error: %v", listenError)
177186
}
178187
close(applicationServerClosed)
179188
}()
180189
go func() {
181190
log.Printf("starting metrics server on %+q", config.metricsListenAddress)
182-
if listenError := metricsSever.ListenAndServe(); listenError != nil {
183-
log.Printf("metrics error: %v", listenError)
191+
if listenError := metricsServer.ListenAndServe(); !errors.Is(listenError, http.ErrServerClosed) {
192+
log.Printf("metrics server error: %v", listenError)
184193
}
185194
close(metricsServerClosed)
186195
}()
187196

188197
<-signalChan
189198
close(hupChan)
190199
applicationServer.Shutdown(nil)
191-
metricsSever.Shutdown(nil)
200+
metricsServer.Shutdown(nil)
192201
<-applicationServerClosed
193202
<-metricsServerClosed
194203
}

0 commit comments

Comments
 (0)