dc5c7bf8c71893edd278ee423c3441bdc35ed8c7
[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                         // Change to RequireAndVerify once we have mandatory certs
49                         ClientAuth: tls.VerifyClientCertIfGiven,
50                         ClientCAs:  caCertPool,
51                         MinVersion: tls.VersionTLS12,
52                 }
53                 tlsConfig.BuildNameToCertificate()
54         }
55         return tlsConfig, nil
56 }
57
58 // GeneratePGPKeyPair produces a PGP key pair and returns
59 // two things:
60 // A base64 encoded form of the public part of the entity
61 // A base64 encoded form of the private key
62 func GeneratePGPKeyPair() (string, string, error) {
63         var entity *openpgp.Entity
64         entity, err := openpgp.NewEntity("aaf.sms.init", "PGP Key for unsealing", "", nil)
65         if err != nil {
66                 smslogger.WriteError(err.Error())
67                 return "", "", err
68         }
69
70         // Sign the identity in the entity
71         for _, id := range entity.Identities {
72                 err = id.SelfSignature.SignUserId(id.UserId.Id, entity.PrimaryKey, entity.PrivateKey, nil)
73                 if err != nil {
74                         smslogger.WriteError(err.Error())
75                         return "", "", err
76                 }
77         }
78
79         // Sign the subkey in the entity
80         for _, subkey := range entity.Subkeys {
81                 err := subkey.Sig.SignKey(subkey.PublicKey, entity.PrivateKey, nil)
82                 if err != nil {
83                         smslogger.WriteError(err.Error())
84                         return "", "", err
85                 }
86         }
87
88         buffer := new(bytes.Buffer)
89         entity.Serialize(buffer)
90         pbkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
91
92         buffer.Reset()
93         entity.SerializePrivate(buffer, nil)
94         prkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
95
96         return pbkey, prkey, nil
97 }
98
99 // DecryptPGPBytes decrypts a PGP encoded input string and returns
100 // a base64 representation of the decoded string
101 func DecryptPGPBytes(data string, prKey string) (string, error) {
102         // Convert private key to bytes from base64
103         prKeyBytes, err := base64.StdEncoding.DecodeString(prKey)
104         if err != nil {
105                 smslogger.WriteError("Error Decoding base64 private key: " + err.Error())
106                 return "", err
107         }
108
109         dataBytes, err := base64.StdEncoding.DecodeString(data)
110         if err != nil {
111                 smslogger.WriteError("Error Decoding base64 data: " + err.Error())
112                 return "", err
113         }
114
115         prEntity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(prKeyBytes)))
116         if err != nil {
117                 smslogger.WriteError("Error reading entity from PGP key: " + err.Error())
118                 return "", err
119         }
120
121         prEntityList := &openpgp.EntityList{prEntity}
122         message, err := openpgp.ReadMessage(bytes.NewBuffer(dataBytes), prEntityList, nil, nil)
123         if err != nil {
124                 smslogger.WriteError("Error Decrypting message: " + err.Error())
125                 return "", err
126         }
127
128         var retBuf bytes.Buffer
129         retBuf.ReadFrom(message.UnverifiedBody)
130
131         return retBuf.String(), nil
132 }