2 * Copyright 2018 Intel Corporation, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 "golang.org/x/crypto/openpgp"
30 var tlsConfig *tls.Config
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
37 caCert, err := ioutil.ReadFile(caCertFile)
43 caCertPool := x509.NewCertPool()
44 caCertPool.AppendCertsFromPEM(caCert)
46 tlsConfig = &tls.Config{
47 ClientAuth: tls.RequireAndVerifyClientCert,
48 ClientCAs: caCertPool,
49 MinVersion: tls.VersionTLS12,
51 tlsConfig.BuildNameToCertificate()
56 // GeneratePGPKeyPair produces a PGP key pair and returns
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)
64 smslogger.WriteError(err.Error())
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)
72 smslogger.WriteError(err.Error())
77 // Sign the subkey in the entity
78 for _, subkey := range entity.Subkeys {
79 err := subkey.Sig.SignKey(subkey.PublicKey, entity.PrivateKey, nil)
81 smslogger.WriteError(err.Error())
86 buffer := new(bytes.Buffer)
87 entity.Serialize(buffer)
88 pbkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
91 entity.SerializePrivate(buffer, nil)
92 prkey := base64.StdEncoding.EncodeToString(buffer.Bytes())
94 return pbkey, prkey, nil