| 1 | // |
| 2 | // Copyright (c) SAS Institute 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 signinit |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "crypto" |
| 22 | "fmt" |
| 23 | "time" |
| 24 | |
| 25 | "github.com/sassoftware/relic/v7/cmdline/shared" |
| 26 | "github.com/sassoftware/relic/v7/config" |
| 27 | "github.com/sassoftware/relic/v7/lib/audit" |
| 28 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 29 | "github.com/sassoftware/relic/v7/signers" |
| 30 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 31 | "github.com/sassoftware/relic/v7/token" |
| 32 | ) |
| 33 | |
| 34 | // InitKey loads the cert chain for a key |
| 35 | func InitKey(ctx context.Context, tok token.Token, keyName string) (*certloader.Certificate, *config.KeyConfig, error) { |
| 36 | key, err := tok.GetKey(ctx, keyName) |
| 37 | if err != nil { |
| 38 | return nil, nil, err |
| 39 | } |
| 40 | kconf := key.Config() |
| 41 | // parse certificates |
| 42 | cert, err := certloader.LoadTokenCertificates(key, kconf.X509Certificate, kconf.PgpCertificate, key.Certificate()) |
| 43 | if err != nil { |
| 44 | return nil, nil, err |
| 45 | } |
| 46 | cert.KeyName = keyName |
| 47 | return cert, kconf, nil |
| 48 | } |
| 49 | |
| 50 | // InitKey prepares to sign using the named key, preparing a cert chain and |
| 51 | // signing options according to the server configuration |
| 52 | func Init(ctx context.Context, mod *signers.Signer, tok token.Token, keyName string, hash crypto.Hash, flags *signers.FlagValues) (*certloader.Certificate, *signers.SignOpts, error) { |
| 53 | cert, kconf, err := InitKey(ctx, tok, keyName) |
| 54 | if err != nil { |
| 55 | return nil, nil, err |
| 56 | } |
| 57 | // create audit info |
| 58 | auditInfo := audit.New(kconf.Name(), mod.Name, hash) |
| 59 | now := time.Now().UTC() |
| 60 | auditInfo.SetTimestamp(now) |
| 61 | if cert.Leaf != nil { |
| 62 | auditInfo.SetX509Cert(cert.Leaf) |
| 63 | } else if mod.CertTypes&signers.CertTypeX509 != 0 { |
| 64 | return nil, nil, sigerrors.ErrNoCertificate{Type: "x509"} |
| 65 | } |
| 66 | if cert.PgpKey != nil { |
| 67 | auditInfo.SetPgpCert(cert.PgpKey) |
| 68 | } else if mod.CertTypes&signers.CertTypePgp != 0 { |
| 69 | return nil, nil, sigerrors.ErrNoCertificate{Type: "pgp"} |
| 70 | } |
| 71 | if kconf.Timestamp && !flags.GetBool("no-timestamp") { |
| 72 | cert.Timestamper, err = GetTimestamper() |
| 73 | if err != nil { |
| 74 | return nil, nil, err |
| 75 | } |
| 76 | } |
| 77 | opts := signers.SignOpts{ |
| 78 | Hash: hash, |
| 79 | Time: now, |
| 80 | Audit: auditInfo, |
| 81 | Flags: flags, |
| 82 | } |
| 83 | opts = opts.WithContext(ctx) |
| 84 | return cert, &opts, nil |
| 85 | } |
| 86 | |
| 87 | func PublishAudit(info *audit.Info) error { |
| 88 | aconf := shared.CurrentConfig.Amqp |
| 89 | if aconf != nil && aconf.URL != "" { |
| 90 | if err := info.Publish(aconf); err != nil { |
| 91 | return fmt.Errorf("failed to publish audit log: %w", err) |
| 92 | } |
| 93 | } |
| 94 | return nil |
| 95 | } |