...

Source file src/edge-infra.dev/pkg/lib/crypto/certs/pem/pem.go

Documentation: edge-infra.dev/pkg/lib/crypto/certs/pem

     1  package pem
     2  
     3  import (
     4  	"crypto"
     5  	"crypto/x509"
     6  	"encoding/pem"
     7  )
     8  
     9  // Encodex509CertAsPem converts an x509 certificate to a PEM encoded block
    10  func Encodex509CertAsPem(x509CertBytes []byte) []byte {
    11  	block := pem.Block{
    12  		Type:  "CERTIFICATE",
    13  		Bytes: x509CertBytes,
    14  	}
    15  	encodedCert := pem.EncodeToMemory(&block)
    16  	return encodedCert
    17  }
    18  
    19  // Utility for obtaining an *x509.Certificate from PEM-encoded bytes
    20  func GetCertFromPemFile(certBytes []byte) (*x509.Certificate, error) {
    21  	cpb, _ := pem.Decode(certBytes)
    22  	cert, err := x509.ParseCertificate(cpb.Bytes)
    23  
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	return cert, nil
    29  }
    30  
    31  // Utility for obtaining a crypto.Signer from a file containing a PEM-encoded private key
    32  func GetKeySignerFromPemFile(keyBytes []byte) (crypto.Signer, error) {
    33  	ckb, _ := pem.Decode(keyBytes)
    34  	key, err := x509.ParsePKCS1PrivateKey(ckb.Bytes)
    35  
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	return key, nil
    41  }
    42  

View as plain text