Skip to content

Commit

Permalink
make dependencies as interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zLukas committed Sep 28, 2023
1 parent 0b4cf41 commit bd9c6fc
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
50 changes: 50 additions & 0 deletions src/cert-generator/pkg/interfaces/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package interfaces

import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io"
)

type Irsa interface {
GenerateKey(random io.Reader, bits int) (*rsa.PrivateKey, error)
}

type Ix509 interface {
CreateCertificate(rand io.Reader, template *x509.Certificate, parent *x509.Certificate, pub any, priv any) ([]byte, error)
ParseCertificate(der []byte) (*x509.Certificate, error)
}

type Ipem interface {
Decode(data []byte) (*pem.Block, []byte)
Encode(out io.Writer, b *pem.Block) error
}

type Rsa struct{}

func (r *Rsa) GenerateKey(random io.Reader, bits int) (*rsa.PrivateKey, error) {
return rsa.GenerateKey(random, bits)
}

type Pem struct{}

func (p *Pem) Decode(data []byte) (*pem.Block, []byte) {
return pem.Decode(data)
}

func (p *Pem) Encode(out io.Writer, b *pem.Block) error {
return pem.Encode(out, b)
}

type X509 struct{}

func (x *X509) CreateCertificate(rand io.Reader, template *x509.Certificate, parent *x509.Certificate, pub any, priv any) ([]byte, error) {
return x509.CreateCertificate(rand, template, parent, pub, priv)
}

func (x *X509) ParseCertificate(der []byte) (*x509.Certificate, error) {
return x509.ParseCertificate(der)
}

//x509.MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte
12 changes: 9 additions & 3 deletions src/cert-generator/pkg/key/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
"encoding/pem"
"fmt"
"os"

"github.com/zLukas/CloudTools/src/cert-generator/pkg/interfaces"
)

var Irsa interfaces.Irsa = &interfaces.Rsa{}
var Ipem interfaces.Ipem = &interfaces.Pem{}
var Ix509 interfaces.Ix509 = &interfaces.X509{}

func CreateRSAPrivateKey(n int) (*rsa.PrivateKey, error) {
return rsa.GenerateKey(rand.Reader, n)
return Irsa.GenerateKey(rand.Reader, n)
}

func RSAPrivateKeyToPEM(privateKey *rsa.PrivateKey) *pem.Block {
Expand All @@ -29,7 +35,7 @@ func CreateRSAPrivateKeyAndSave(path string, n int) error {
if err != nil {
return err
}
if err := pem.Encode(f, RSAPrivateKeyToPEM(privateKey)); err != nil {
if err := Ipem.Encode(f, RSAPrivateKeyToPEM(privateKey)); err != nil {
return err
}
if err := f.Close(); err != nil {
Expand All @@ -42,7 +48,7 @@ func PrivateKeyPemToRSA(input []byte) (*rsa.PrivateKey, error) {
var parsedKey *rsa.PrivateKey
var err error

privPem, _ := pem.Decode(input)
privPem, _ := Ipem.Decode(input)

if privPem.Type != "RSA PRIVATE KEY" {
return nil, fmt.Errorf("RSA private key is of the wrong type: %s", privPem.Type)
Expand Down
5 changes: 2 additions & 3 deletions src/cert-generator/pkg/tls/pem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package tls

import (
"crypto/x509"
"encoding/pem"
"fmt"
)

func PemToX509(input []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(input)
block, _ := Ipem.Decode(input)
if block == nil {
return nil, fmt.Errorf("failed to parse certificate PEM")
}
return x509.ParseCertificate(block.Bytes)
return Ix509.ParseCertificate(block.Bytes)
}
6 changes: 6 additions & 0 deletions src/cert-generator/pkg/tls/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tls

import (
"math/big"

"github.com/zLukas/CloudTools/src/cert-generator/pkg/interfaces"
)

type CACert struct {
Expand All @@ -26,3 +28,7 @@ type CertSubject struct {
SerialNumber string `yaml:"serialNumber"`
CommonName string `yaml:"commonName"`
}

var Irsa interfaces.Irsa = &interfaces.Rsa{}
var Ipem interfaces.Ipem = &interfaces.Pem{}
var Ix509 interfaces.Ix509 = &interfaces.X509{}
8 changes: 4 additions & 4 deletions src/cert-generator/pkg/tls/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ func createCert(template *x509.Certificate, caKey *rsa.PrivateKey, caCert *x509.
return nil, nil, err
}
if template.IsCA {
derBytes, err = x509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey)
derBytes, err = Ix509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey)
if err != nil {
return nil, nil, err
}
} else {
derBytes, err = x509.CreateCertificate(rand.Reader, template, caCert, &privateKey.PublicKey, caKey)
derBytes, err = Ix509.CreateCertificate(rand.Reader, template, caCert, &privateKey.PublicKey, caKey)
if err != nil {
return nil, nil, err
}
}

if err = pem.Encode(&certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
if err = Ipem.Encode(&certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return nil, nil, err
}
if err = pem.Encode(&keyOut, key.RSAPrivateKeyToPEM(privateKey)); err != nil {
if err = Ipem.Encode(&keyOut, key.RSAPrivateKeyToPEM(privateKey)); err != nil {
return nil, nil, err
}

Expand Down

0 comments on commit bd9c6fc

Please sign in to comment.