| 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 | "crypto" |
| 21 | "fmt" |
| 22 | "net/http" |
| 23 | |
| 24 | "github.com/rs/zerolog/hlog" |
| 25 | "github.com/sassoftware/relic/v7/internal/authmodel" |
| 26 | "github.com/sassoftware/relic/v7/internal/httperror" |
| 27 | "github.com/sassoftware/relic/v7/internal/signinit" |
| 28 | "github.com/sassoftware/relic/v7/internal/zhttp" |
| 29 | "github.com/sassoftware/relic/v7/lib/readercounter" |
| 30 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 31 | "github.com/sassoftware/relic/v7/signers" |
| 32 | ) |
| 33 | |
| 34 | const defaultHash = crypto.SHA256 |
| 35 | |
| 36 | func (s *Server) serveSign(rw http.ResponseWriter, request *http.Request) error { |
| 37 | // parse parameters |
| 38 | query := request.URL.Query() |
| 39 | keyName := query.Get("key") |
| 40 | if keyName == "" { |
| 41 | return httperror.MissingParameterError("key") |
| 42 | } |
| 43 | filename := query.Get("filename") |
| 44 | if filename == "" { |
| 45 | return httperror.MissingParameterError("filename") |
| 46 | } |
| 47 | sigType := query.Get("sigtype") |
| 48 | // authorize key |
| 49 | userInfo := authmodel.RequestInfo(request) |
| 50 | keyConf, err := s.Config.GetKey(keyName) |
| 51 | if err != nil { |
| 52 | hlog.FromRequest(request).Err(err).Str("key", keyName).Msg("key not found") |
| 53 | return httperror.ErrForbidden |
| 54 | } else if !userInfo.Allowed(keyConf) { |
| 55 | hlog.FromRequest(request).Error().Str("key", keyName).Msg("access to key denied") |
| 56 | return httperror.ErrForbidden |
| 57 | } |
| 58 | // configure signer |
| 59 | mod := signers.ByName(sigType) |
| 60 | if mod == nil { |
| 61 | hlog.FromRequest(request).Error().Str("sigtype", sigType).Msg("signature type not found") |
| 62 | return httperror.ErrUnknownSignatureType |
| 63 | } |
| 64 | hash := defaultHash |
| 65 | if digest := request.URL.Query().Get("digest"); digest != "" { |
| 66 | hash = x509tools.HashByName(digest) |
| 67 | if hash == 0 { |
| 68 | hlog.FromRequest(request).Error().Str("digest", digest).Msg("digest type not found") |
| 69 | return httperror.ErrUnknownDigest |
| 70 | } |
| 71 | } |
| 72 | // parse flags for signer |
| 73 | flags, err := mod.FlagsFromQuery(query) |
| 74 | if err != nil { |
| 75 | hlog.FromRequest(request).Err(err).Str("sigtype", sigType). |
| 76 | Msg("failed to parse signer arguments") |
| 77 | return httperror.BadParameterError(err) |
| 78 | } |
| 79 | // get key from token and initialize signer context |
| 80 | tok := s.tokens[keyConf.Token] |
| 81 | if tok == nil { |
| 82 | return fmt.Errorf("missing token \"%s\" for key \"%s\"", keyConf.Token, keyName) |
| 83 | } |
| 84 | cert, opts, err := signinit.Init(request.Context(), mod, tok, keyName, hash, flags) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | opts.Audit.Attributes["client.ip"] = zhttp.StripPort(request.RemoteAddr) |
| 89 | opts.Audit.Attributes["client.filename"] = filename |
| 90 | userInfo.AuditContext(opts.Audit) |
| 91 | // sign the request stream and output a binpatch or signature blob |
| 92 | counter := readercounter.New(request.Body) |
| 93 | blob, err := mod.Sign(counter, cert, *opts) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | opts.Audit.Attributes["perf.size.in"] = counter.N |
| 98 | opts.Audit.Attributes["perf.size.patch"] = len(blob) |
| 99 | if err := signinit.PublishAudit(opts.Audit); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | ev := hlog.FromRequest(request).Info(). |
| 103 | Str("key", keyConf.Name()). |
| 104 | Str("filename", filename) |
| 105 | if mod.FormatLog != nil { |
| 106 | ev.Dict("package", mod.FormatLog(opts.Audit)) |
| 107 | } |
| 108 | ev.Msg("signed package") |
| 109 | rw.Header().Set("Content-Type", opts.Audit.GetMimeType()) |
| 110 | _, err = rw.Write(blob) |
| 111 | return err |
| 112 | } |