Adding PGP decrypt to auth package
[aaf/sms.git] / sms-service / src / sms / auth / auth.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package auth
18
19 import (
20         "bytes"
21         "crypto/tls"
22         "crypto/x509"
23         "encoding/base64"
24         "golang.org/x/crypto/openpgp"
25         "golang.org/x/crypto/openpgp/packet"
26         "io/ioutil"
27
28         smslogger "sms/log"
29 )
30
31 var tlsConfig *tls.Config
32
33 // GetTLSConfig initializes a tlsConfig using the CA's certificate
34 // This config is then used to enable the server for mutual TLS
35 func GetTLSConfig(caCertFile string) (*tls.Config, error) {
36         // Initialize tlsConfig once
37         if tlsConfig == nil {
38                 caCert, err := ioutil.ReadFile(caCertFile)
39
40                 if err != nil {
41                         return nil, err
42                 }
43
44                 caCertPool := x509.NewCertPool()
45                 caCertPool.AppendCertsFromPEM(caCert)
46
47                 tlsConfig = &tls.Config{
48                         ClientAuth: tls.RequireAndVerifyClientCert,
49                         ClientCAs:  caCertPool,
50                         MinVersion: tls.VersionTLS12,
51                 }
52                 tlsConfig.BuildNameToCertificate()
53         }
54         return tlsConfig, nil
55 }
56
57 // GeneratePGPKeyPair produces a PGP key pair and returns
58 // two things:
59 // A base64 encoded form of the public part of the entity
60 // A base64 encoded form of the private key
61 func GeneratePGPKeyPair() (string, string, error) {
62         var entity *openpgp.Entity
63         entity, err := openpgp.NewEntity("aaf.sms.init", "PGP Key for unsealing", "", nil)
64         if err != nil {
65                 smslogger.WriteError(err.Error())
66                 return "", "", err
67         }
68
69         // Sign the identity in the entity
70         for _, id := range entity.Identities {
71                 err = id.SelfSignature.SignUserId(id.UserId.Id, entity.PrimaryKey, entity.PrivateKey, nil)
72                 if err != nil {
73                         smslogger.WriteError(err.Error())
74                         return "", "", err
75                 }
76         }
77
78         // Sign the subkey in the entity
79         for _, subkey := range entity.Subkeys {
80                 err := subkey.Sig.SignKey(subkey.PublicKey, entity.PrivateKey, nil)
81                 if err != nil {
82                         smslogger.WriteError(err.Error())
83                         return "", "", err
84                 }
85         }
86
87         buffer := new(bytes.Buffer)
88         entity.Serialize(buffer)
89         pbkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
90
91         buffer.Reset()
92         entity.SerializePrivate(buffer, nil)
93         prkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
94
95         return pbkey, prkey, nil
96 }
97
98 // DecryptPGPBytes decrypts a PGP encoded input string and returns
99 // a base64 representation of the decoded string
100 func DecryptPGPBytes(data string, prKey string) (string, error) {
101         // Convert private key to bytes from base64
102         prKeyBytes, err := base64.StdEncoding.DecodeString(prKey)
103         if err != nil {
104                 smslogger.WriteError("Error Decoding base64 private key: " + err.Error())
105                 return "", err
106         }
107
108         dataBytes, err := base64.StdEncoding.DecodeString(data)
109         if err != nil {
110                 smslogger.WriteError("Error Decoding base64 data: " + err.Error())
111                 return "", err
112         }
113
114         prEntity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(prKeyBytes)))
115         if err != nil {
116                 smslogger.WriteError("Error reading entity from PGP key: " + err.Error())
117                 return "", err
118         }
119
120         prEntityList := &openpgp.EntityList{prEntity}
121         message, err := openpgp.ReadMessage(bytes.NewBuffer(dataBytes), prEntityList, nil, nil)
122         if err != nil {
123                 smslogger.WriteError("Error Decrypting message: " + err.Error())
124                 return "", err
125         }
126
127         var retBuf bytes.Buffer
128         retBuf.ReadFrom(message.UnverifiedBody)
129
130         return retBuf.String(), nil
131 }