| 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 server |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "context" |
| 22 | "crypto/x509" |
| 23 | "encoding/pem" |
| 24 | "fmt" |
| 25 | "net/http" |
| 26 | |
| 27 | "golang.org/x/crypto/openpgp" |
| 28 | "golang.org/x/crypto/openpgp/armor" |
| 29 | |
| 30 | "github.com/go-chi/chi/v5" |
| 31 | "github.com/sassoftware/relic/v7/config" |
| 32 | "github.com/sassoftware/relic/v7/internal/authmodel" |
| 33 | "github.com/sassoftware/relic/v7/internal/httperror" |
| 34 | "github.com/sassoftware/relic/v7/internal/signinit" |
| 35 | ) |
| 36 | |
| 37 | type keyInfo struct { |
| 38 | X509Certificate string |
| 39 | PGPCertificate string |
| 40 | } |
| 41 | |
| 42 | func (s *Server) serveGetKey(rw http.ResponseWriter, req *http.Request) error { |
| 43 | userInfo := authmodel.RequestInfo(req) |
| 44 | keyName := chi.URLParam(req, "key") |
| 45 | keyConf, err := s.Config.GetKey(keyName) |
| 46 | if err == nil && userInfo.Allowed(keyConf) { |
| 47 | info, err := s.getKeyInfo(req.Context(), keyConf) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | return writeJSON(rw, info) |
| 52 | } |
| 53 | return httperror.ErrForbidden |
| 54 | } |
| 55 | |
| 56 | func (s *Server) getKeyInfo(ctx context.Context, keyConf *config.KeyConfig) (keyInfo, error) { |
| 57 | tok := s.tokens[keyConf.Token] |
| 58 | if tok == nil { |
| 59 | return keyInfo{}, fmt.Errorf("missing token \"%s\" for key \"%s\"", keyConf.Token, keyConf.Name()) |
| 60 | } |
| 61 | cert, _, err := signinit.InitKey(ctx, tok, keyConf.Name()) |
| 62 | if err != nil { |
| 63 | return keyInfo{}, err |
| 64 | } |
| 65 | var info keyInfo |
| 66 | if cert.PgpKey != nil { |
| 67 | info.PGPCertificate, err = marshalPGPCert(cert.PgpKey) |
| 68 | if err != nil { |
| 69 | return keyInfo{}, err |
| 70 | } |
| 71 | } |
| 72 | if cert.Leaf != nil { |
| 73 | info.X509Certificate, err = marshalX509Cert(cert.Certificates) |
| 74 | if err != nil { |
| 75 | return keyInfo{}, err |
| 76 | } |
| 77 | } |
| 78 | return info, nil |
| 79 | } |
| 80 | |
| 81 | // marshal entire X509 certificate chain in PEM format |
| 82 | func marshalX509Cert(certs []*x509.Certificate) (string, error) { |
| 83 | var buf bytes.Buffer |
| 84 | for _, cert := range certs { |
| 85 | block := &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw} |
| 86 | if err := pem.Encode(&buf, block); err != nil { |
| 87 | return "", err |
| 88 | } |
| 89 | } |
| 90 | return buf.String(), nil |
| 91 | } |
| 92 | |
| 93 | // marshal PGP public certificate in ASCII armor |
| 94 | func marshalPGPCert(entity *openpgp.Entity) (string, error) { |
| 95 | var buf bytes.Buffer |
| 96 | w, err := armor.Encode(&buf, "PGP PUBLIC KEY BLOCK", nil) |
| 97 | if err != nil { |
| 98 | return "", err |
| 99 | } |
| 100 | if err := entity.Serialize(w); err != nil { |
| 101 | return "", err |
| 102 | } |
| 103 | if err := w.Close(); err != nil { |
| 104 | return "", err |
| 105 | } |
| 106 | buf.WriteString("\n") |
| 107 | return buf.String(), nil |
| 108 | } |