From 4625ab382c388d0085eadf86b13aa5cfa6a6eb23 Mon Sep 17 00:00:00 2001 From: Viktor Popov <viktor.popov@vereign.com> Date: Fri, 10 Aug 2018 18:53:08 +0300 Subject: [PATCH] Added pem format for generated keys --- handler/generate_certificate.go | 4 +++- handler/generate_keypair.go | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/handler/generate_certificate.go b/handler/generate_certificate.go index 54364e9..c14ac82 100644 --- a/handler/generate_certificate.go +++ b/handler/generate_certificate.go @@ -75,7 +75,9 @@ func (s *KeyStorageServerImpl) GenerateCertificate(ctx context.Context, in *api. func generateCertificate(publicKeyBytes []byte, caCertFilePath string, caPrivateKeyFilePath string, certificateData *api.GenerateCertificateRequest_CertificateData) ([]byte, error) { - publicKey, err := x509.ParsePKIXPublicKey(publicKeyBytes) + publicKeyPemBlock, _ := pem.Decode(publicKeyBytes) + + publicKey, err := x509.ParsePKIXPublicKey(publicKeyPemBlock.Bytes) if err != nil { return nil, err } diff --git a/handler/generate_keypair.go b/handler/generate_keypair.go index 65a2656..febe5ab 100644 --- a/handler/generate_keypair.go +++ b/handler/generate_keypair.go @@ -24,6 +24,7 @@ import ( "crypto/rsa" "crypto/sha256" "crypto/x509" + "encoding/pem" "code.vereign.com/code/viam-apis/data-storage-agent/client" "code.vereign.com/code/viam-apis/key-storage-agent/api" @@ -129,15 +130,30 @@ func generateKeyPair(keySize int) ([]byte, []byte, error) { publicKey := &privateKey.PublicKey - privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey) + pkcs8PrivateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey) if err != nil { return nil, nil, err } - publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) + + privateKeyPemBlock := &pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: pkcs8PrivateKeyBytes, + } + + privateKeyBytes := pem.EncodeToMemory(privateKeyPemBlock) + + pkixPublicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) if err != nil { return nil, nil, err } + publicKeyPemBlock := &pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: pkixPublicKeyBytes, + } + + publicKeyBytes := pem.EncodeToMemory(publicKeyPemBlock) + return privateKeyBytes, publicKeyBytes, nil } -- GitLab