aboutsummaryrefslogtreecommitdiff
path: root/internal/x509util/x509util.go
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus@rgdd.se>2023-12-31 09:39:25 +0100
committerRasmus Dahlberg <rasmus@rgdd.se>2024-01-07 20:22:23 +0100
commite18d36ebae30536c77c61cd5da123991e0ca1629 (patch)
treebf4880c0019a6009ab1b671e23ef4a1a4a5e8e08 /internal/x509util/x509util.go
parent54d980afcbd6f0011d6a162e0003587d26a3e311 (diff)
Add drafty prototype
Diffstat (limited to 'internal/x509util/x509util.go')
-rw-r--r--internal/x509util/x509util.go44
1 files changed, 0 insertions, 44 deletions
diff --git a/internal/x509util/x509util.go b/internal/x509util/x509util.go
deleted file mode 100644
index 912d1b4..0000000
--- a/internal/x509util/x509util.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package x509util
-
-import (
- "crypto/x509"
- "encoding/pem"
- "fmt"
-)
-
-// ParseChain parses a certificate chain in PEM format. At least one
-// certificate must be in the chain. The first certificate must be a leaf,
-// whereas all other certificates must CA certificates (intermdiates/roots).
-//
-// Note: it is not checked if the certificate chain's root is trusted or not.
-func ParseChain(b []byte) ([]x509.Certificate, error) {
- var chain []x509.Certificate
-
- for {
- block, rest := pem.Decode(b)
- if block == nil {
- break
- }
- crt, err := x509.ParseCertificate(block.Bytes)
- if err != nil {
- return nil, fmt.Errorf("parse certificate: %v", err)
- }
-
- chain = append(chain, *crt)
- b = rest
- }
-
- if len(chain) == 0 {
- return nil, fmt.Errorf("no certificates in the provided chain")
- }
- if chain[0].IsCA {
- return nil, fmt.Errorf("leaf certificate has the CA bit set")
- }
- for _, crt := range chain[1:] {
- if !crt.IsCA {
- return nil, fmt.Errorf("non-leaf certificate without the CA bit set")
- }
- }
-
- return chain, nil
-}