Adding PGP key creation capability for vault init
[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         "io/ioutil"
26
27         smslogger "sms/log"
28 )
29
30 var tlsConfig *tls.Config
31
32 // GetTLSConfig initializes a tlsConfig using the CA's certificate
33 // This config is then used to enable the server for mutual TLS
34 func GetTLSConfig(caCertFile string) (*tls.Config, error) {
35         // Initialize tlsConfig once
36         if tlsConfig == nil {
37                 caCert, err := ioutil.ReadFile(caCertFile)
38
39                 if err != nil {
40                         return nil, err
41                 }
42
43                 caCertPool := x509.NewCertPool()
44                 caCertPool.AppendCertsFromPEM(caCert)
45
46                 tlsConfig = &tls.Config{
47                         ClientAuth: tls.RequireAndVerifyClientCert,
48                         ClientCAs:  caCertPool,
49                         MinVersion: tls.VersionTLS12,
50                 }
51                 tlsConfig.BuildNameToCertificate()
52         }
53         return tlsConfig, nil
54 }
55
56 // GeneratePGPKeyPair produces a PGP key pair and returns
57 // two things:
58 // A base64 encoded form of the public part of the entity
59 // A base64 encoded form of the private key
60 func GeneratePGPKeyPair() (string, string, error) {
61         var entity *openpgp.Entity
62         entity, err := openpgp.NewEntity("aaf.sms.init", "PGP Key for unsealing", "", nil)
63         if err != nil {
64                 smslogger.WriteError(err.Error())
65                 return "", "", err
66         }
67
68         // Sign the identity in the entity
69         for _, id := range entity.Identities {
70                 err = id.SelfSignature.SignUserId(id.UserId.Id, entity.PrimaryKey, entity.PrivateKey, nil)
71                 if err != nil {
72                         smslogger.WriteError(err.Error())
73                         return "", "", err
74                 }
75         }
76
77         // Sign the subkey in the entity
78         for _, subkey := range entity.Subkeys {
79                 err := subkey.Sig.SignKey(subkey.PublicKey, entity.PrivateKey, nil)
80                 if err != nil {
81                         smslogger.WriteError(err.Error())
82                         return "", "", err
83                 }
84         }
85
86         buffer := new(bytes.Buffer)
87         entity.Serialize(buffer)
88         pbkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
89
90         buffer.Reset()
91         entity.SerializePrivate(buffer, nil)
92         prkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
93
94         return pbkey, prkey, nil
95 }