~emersion/tlsd

a60e6ef0c9adf1edc54d59791f1a2c29f0233ee5 — Simon Ser 2 months ago 035075c
internal: add NewCertificateFromTLS
1 files changed, 30 insertions(+), 0 deletions(-)

M internal/service.go
M internal/service.go => internal/service.go +30 -0
@@ 2,6 2,9 @@ package internal

import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"log"
	"sync"


@@ 21,6 24,33 @@ const (

type Certificate = api.Certificate

func NewCertificateFromTLS(cert *tls.Certificate) (*Certificate, error) {
	var certDER []byte
	for _, b := range cert.Certificate {
		certDER = append(certDER, b...)
	}

	certPEM := pem.EncodeToMemory(&pem.Block{
		Type:  "CERTIFICATE",
		Bytes: certDER,
	})

	privDER, err := x509.MarshalPKCS8PrivateKey(cert.PrivateKey)
	if err != nil {
		return nil, err
	}

	privPEM := pem.EncodeToMemory(&pem.Block{
		Type:  "PRIVATE KEY",
		Bytes: privDER,
	})

	return &Certificate{
		Certificate: string(certPEM),
		PrivateKey:  string(privPEM),
	}, nil
}

type Backend interface {
	Register(domain string) error
	Unregister(domain string) error