Initial QSfera import
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
load("@rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "cert",
|
||||
srcs = [
|
||||
"cert.go",
|
||||
"chain.go",
|
||||
],
|
||||
importpath = "github.com/lestrrat-go/jwx/v3/cert",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//internal/base64",
|
||||
"//internal/tokens",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "cert_test",
|
||||
srcs = [
|
||||
"cert_test.go",
|
||||
"chain_test.go",
|
||||
],
|
||||
deps = [
|
||||
":cert",
|
||||
"//internal/jwxtest",
|
||||
"@com_github_stretchr_testify//require",
|
||||
],
|
||||
)
|
||||
|
||||
alias(
|
||||
name = "go_default_library",
|
||||
actual = ":cert",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/x509"
|
||||
stdlibb64 "encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v3/internal/base64"
|
||||
)
|
||||
|
||||
// Create is a wrapper around x509.CreateCertificate, but it additionally
|
||||
// encodes it in base64 so that it can be easily added to `x5c` fields
|
||||
func Create(rand io.Reader, template, parent *x509.Certificate, pub, priv any) ([]byte, error) {
|
||||
der, err := x509.CreateCertificate(rand, template, parent, pub, priv)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`failed to create x509 certificate: %w`, err)
|
||||
}
|
||||
return EncodeBase64(der)
|
||||
}
|
||||
|
||||
// EncodeBase64 is a utility function to encode ASN.1 DER certificates
|
||||
// using base64 encoding. This operation is normally done by `pem.Encode`
|
||||
// but since PEM would include the markers (`-----BEGIN`, and the like)
|
||||
// while `x5c` fields do not need this, this function can be used to
|
||||
// shave off a few lines
|
||||
func EncodeBase64(der []byte) ([]byte, error) {
|
||||
enc := stdlibb64.StdEncoding
|
||||
dst := make([]byte, enc.EncodedLen(len(der)))
|
||||
enc.Encode(dst, der)
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// Parse is a utility function to decode a base64 encoded
|
||||
// ASN.1 DER format certificate, and to parse the byte sequence.
|
||||
// The certificate must be in PKIX format, and it must not contain PEM markers
|
||||
func Parse(src []byte) (*x509.Certificate, error) {
|
||||
dst, err := base64.Decode(src)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`failed to base64 decode the certificate: %w`, err)
|
||||
}
|
||||
|
||||
cert, err := x509.ParseCertificate(dst)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`failed to parse x509 certificate: %w`, err)
|
||||
}
|
||||
return cert, nil
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v3/internal/tokens"
|
||||
)
|
||||
|
||||
// Chain represents a certificate chain as used in the `x5c` field of
|
||||
// various objects within JOSE.
|
||||
//
|
||||
// It stores the certificates as a list of base64 encoded []byte
|
||||
// sequence. By definition these values must PKIX encoded.
|
||||
type Chain struct {
|
||||
certificates [][]byte
|
||||
}
|
||||
|
||||
func (cc Chain) MarshalJSON() ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteByte(tokens.OpenSquareBracket)
|
||||
for i, cert := range cc.certificates {
|
||||
if i > 0 {
|
||||
buf.WriteByte(tokens.Comma)
|
||||
}
|
||||
buf.WriteByte('"')
|
||||
buf.Write(cert)
|
||||
buf.WriteByte('"')
|
||||
}
|
||||
buf.WriteByte(tokens.CloseSquareBracket)
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (cc *Chain) UnmarshalJSON(data []byte) error {
|
||||
var tmp []string
|
||||
if err := json.Unmarshal(data, &tmp); err != nil {
|
||||
return fmt.Errorf(`failed to unmarshal certificate chain: %w`, err)
|
||||
}
|
||||
|
||||
certs := make([][]byte, len(tmp))
|
||||
for i, cert := range tmp {
|
||||
certs[i] = []byte(cert)
|
||||
}
|
||||
cc.certificates = certs
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get returns the n-th ASN.1 DER + base64 encoded certificate
|
||||
// stored. `false` will be returned in the second argument if
|
||||
// the corresponding index is out of range.
|
||||
func (cc *Chain) Get(index int) ([]byte, bool) {
|
||||
if index < 0 || index >= len(cc.certificates) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return cc.certificates[index], true
|
||||
}
|
||||
|
||||
// Len returns the number of certificates stored in this Chain
|
||||
func (cc *Chain) Len() int {
|
||||
return len(cc.certificates)
|
||||
}
|
||||
|
||||
var pemStart = []byte("----- BEGIN CERTIFICATE -----")
|
||||
var pemEnd = []byte("----- END CERTIFICATE -----")
|
||||
|
||||
func (cc *Chain) AddString(der string) error {
|
||||
return cc.Add([]byte(der))
|
||||
}
|
||||
|
||||
func (cc *Chain) Add(der []byte) error {
|
||||
// We're going to be nice and remove marker lines if they
|
||||
// give it to us
|
||||
der = bytes.TrimPrefix(der, pemStart)
|
||||
der = bytes.TrimSuffix(der, pemEnd)
|
||||
der = bytes.TrimSpace(der)
|
||||
cc.certificates = append(cc.certificates, der)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user