From a02a75b47cd9850c3f9be17186a21316ac880184 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Wed, 21 Mar 2018 17:16:13 -0700 Subject: Adding PGP decrypt to auth package Adding PGP decrypt function to auth. This can be used by quorum client in its code for decryption Issue-ID: AAF-165 Change-Id: I6d236c3164ccb93c11527ac8c2e725987236f9f0 Signed-off-by: Kiran Kamineni --- sms-service/src/sms/auth/auth.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'sms-service/src') diff --git a/sms-service/src/sms/auth/auth.go b/sms-service/src/sms/auth/auth.go index 341f377..ad5afdc 100644 --- a/sms-service/src/sms/auth/auth.go +++ b/sms-service/src/sms/auth/auth.go @@ -22,6 +22,7 @@ import ( "crypto/x509" "encoding/base64" "golang.org/x/crypto/openpgp" + "golang.org/x/crypto/openpgp/packet" "io/ioutil" smslogger "sms/log" @@ -93,3 +94,38 @@ func GeneratePGPKeyPair() (string, string, error) { return pbkey, prkey, nil } + +// DecryptPGPBytes decrypts a PGP encoded input string and returns +// a base64 representation of the decoded string +func DecryptPGPBytes(data string, prKey string) (string, error) { + // Convert private key to bytes from base64 + prKeyBytes, err := base64.StdEncoding.DecodeString(prKey) + if err != nil { + smslogger.WriteError("Error Decoding base64 private key: " + err.Error()) + return "", err + } + + dataBytes, err := base64.StdEncoding.DecodeString(data) + if err != nil { + smslogger.WriteError("Error Decoding base64 data: " + err.Error()) + return "", err + } + + prEntity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(prKeyBytes))) + if err != nil { + smslogger.WriteError("Error reading entity from PGP key: " + err.Error()) + return "", err + } + + prEntityList := &openpgp.EntityList{prEntity} + message, err := openpgp.ReadMessage(bytes.NewBuffer(dataBytes), prEntityList, nil, nil) + if err != nil { + smslogger.WriteError("Error Decrypting message: " + err.Error()) + return "", err + } + + var retBuf bytes.Buffer + retBuf.ReadFrom(message.UnverifiedBody) + + return retBuf.String(), nil +} -- cgit 1.2.3-korg