github.com/nais/outtune@v0.0.0-20230327072907-ef48d1263aba/pkg/cert/cert.go (about)

     1  package cert
     2  
     3  import (
     4  	"context"
     5  	"crypto/rsa"
     6  	"crypto/x509"
     7  	"encoding/pem"
     8  	"fmt"
     9  	"strings"
    10  )
    11  
    12  type CA interface {
    13  	MakeCert(ctx context.Context, serial string, keyPem []byte) (string, error)
    14  }
    15  
    16  func PublicKeytoPem(key *rsa.PublicKey) ([]byte, error) {
    17  	publicKeyBytes, err := x509.MarshalPKIXPublicKey(key)
    18  	if err != nil {
    19  		return nil, fmt.Errorf("error when dumping publickey: %w", err)
    20  	}
    21  
    22  	publicKeyBlock := &pem.Block{
    23  		Type:  "PUBLIC KEY",
    24  		Bytes: publicKeyBytes,
    25  	}
    26  
    27  	s := strings.Builder{}
    28  	err = pem.Encode(&s, publicKeyBlock)
    29  	if err != nil {
    30  		return nil, fmt.Errorf("pem encode: %w", err)
    31  	}
    32  
    33  	return []byte(s.String()), nil
    34  }