// Handle listens for incoming requests for CA information, and returns // a list containing information on each root certificate. func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) error { req := new(client.InfoReq) body, err := ioutil.ReadAll(r.Body) if err != nil { log.Warningf("failed to read request body: %v", err) return errors.NewBadRequest(err) } err = json.Unmarshal(body, req) if err != nil { log.Warningf("failed to unmarshal request: %v", err) return errors.NewBadRequest(err) } cert, err := h.sign.Certificate(req.Label, req.Profile) if err != nil { return err } resp := client.InfoResp{ Certificate: bundler.PemBlockToString(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}), } response := api.NewSuccessResponse(resp) w.Header().Set("Content-Type", "application/json") enc := json.NewEncoder(w) return enc.Encode(response) }
// initialCAHandler is an HTTP handler that accepts a JSON blob in the // same format as the CSR endpoint; this blob should contain the // identity information for the CA's root key. This endpoint is not // suitable for creating intermediate certificates. func initialCAHandler(w http.ResponseWriter, r *http.Request) error { log.Info("setting up initial CA handler") req := new(csr.CertificateRequest) body, err := ioutil.ReadAll(r.Body) if err != nil { log.Warningf("failed to read request body: %v", err) return errors.NewBadRequest(err) } err = json.Unmarshal(body, req) if err != nil { log.Warningf("failed to unmarshal request: %v", err) return errors.NewBadRequest(err) } key, cert, err := initca.New(req) if err != nil { log.Warningf("failed to initialise new CA: %v", err) return err } response := api.NewSuccessResponse(&NewCA{string(key), string(cert)}) enc := json.NewEncoder(w) err = enc.Encode(response) return err }
// Handle implements an http.Handler interface for the bundle handler. func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) error { blob, matched, err := api.ProcessRequestFirstMatchOf(r, [][]string{ []string{"certificate"}, []string{"domain"}, }) if err != nil { log.Warningf("invalid request: %v", err) return err } flavor := blob["flavor"] bf := bundler.Ubiquitous if flavor != "" { bf = bundler.BundleFlavor(flavor) } log.Infof("request for flavor %v", bf) var result *bundler.Bundle switch matched[0] { case "domain": bundle, err := h.bundler.BundleFromRemote(blob["domain"], blob["ip"], bf) if err != nil { log.Warningf("couldn't bundle from remote: %v", err) return err } result = bundle case "certificate": bundle, err := h.bundler.BundleFromPEM([]byte(blob["certificate"]), []byte(blob["private_key"]), bf) if err != nil { log.Warning("bad PEM certifcate or private key") return err } serverName := blob["domain"] ip := blob["ip"] if serverName != "" { err := bundle.Cert.VerifyHostname(serverName) if err != nil { return errors.Wrap(errors.CertificateError, errors.VerifyFailed, err) } } if ip != "" { err := bundle.Cert.VerifyHostname(ip) if err != nil { return errors.Wrap(errors.CertificateError, errors.VerifyFailed, err) } } result = bundle } log.Info("wrote response") return api.SendResponse(w, result) }
// Handle responds to requests for the CA to generate a new private // key and certificate request on behalf of the client. The format for // these requests is documented in the API documentation. func (g *Handler) Handle(w http.ResponseWriter, r *http.Request) error { log.Info("request for CSR") req := new(csr.CertificateRequest) body, err := ioutil.ReadAll(r.Body) if err != nil { log.Warningf("failed to read request body: %v", err) return errors.NewBadRequest(err) } err = json.Unmarshal(body, req) if err != nil { log.Warningf("failed to unmarshal request: %v", err) return errors.NewBadRequest(err) } if req.CA != nil { log.Warningf("request received with CA section") return errors.NewBadRequestString("ca section only permitted in initca") } csr, key, err := g.generator.ProcessRequest(req) if err != nil { log.Warningf("failed to process CSR: %v", err) // The validator returns a *cfssl/errors.HttpError return err } sum, err := computeSum(csr) if err != nil { return errors.NewBadRequest(err) } // Both key and csr are returned PEM-encoded. response := api.NewSuccessResponse(&CertRequest{ Key: string(key), CSR: string(csr), Sums: map[string]Sum{"certificate_request": sum}, }) w.Header().Set("Content-Type", "application/json") enc := json.NewEncoder(w) err = enc.Encode(response) return err }
// Handle responds to requests for the CA to sign the certificate request // present in the "certificate_request" parameter for the host named // in the "hostname" parameter. The certificate should be PEM-encoded. If // provided, subject information from the "subject" parameter will be used // in place of the subject information from the CSR. func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) error { log.Info("signature request received") body, err := ioutil.ReadAll(r.Body) if err != nil { return err } r.Body.Close() var req jsonSignRequest err = json.Unmarshal(body, &req) if err != nil { return errors.NewBadRequestString("Unable to parse sign request") } signReq := jsonReqToTrue(req) if signReq.Hosts == nil { return errors.NewBadRequestString("missing parameter 'hostname' or 'hosts'") } if req.Request == "" { return errors.NewBadRequestString("missing parameter 'certificate_request'") } var cert []byte var profile *config.SigningProfile policy := h.signer.Policy() if policy != nil && policy.Profiles != nil && req.Profile != "" { profile = policy.Profiles[req.Profile] } if profile == nil && policy != nil { profile = policy.Default } if profile.Provider != nil { log.Error("profile requires authentication") return errors.NewBadRequestString("authentication required") } cert, err = h.signer.Sign(signReq) if err != nil { log.Warningf("failed to sign request: %v", err) return err } result := map[string]string{"certificate": string(cert)} log.Info("wrote response") return api.SendResponse(w, result) }
// Handle accepts client information requests, and uses the label to // look up the signer whose public certificate should be retrieved. If // the label is empty, the default label is used. func (h *MultiHandler) Handle(w http.ResponseWriter, r *http.Request) error { req := new(client.InfoReq) body, err := ioutil.ReadAll(r.Body) if err != nil { log.Warningf("failed to read request body: %v", err) return errors.NewBadRequest(err) } err = json.Unmarshal(body, req) if err != nil { log.Warningf("failed to unmarshal request: %v", err) return errors.NewBadRequest(err) } log.Debug("checking label") if req.Label == "" { req.Label = h.defaultLabel } if _, ok := h.signers[req.Label]; !ok { log.Warningf("request for invalid endpoint") return errors.NewBadRequestString("bad label") } log.Debug("getting cert") cert, err := h.signers[req.Label].Certificate("", req.Profile) if err != nil { log.Infof("error getting certificate: %v", err) return err } resp := client.InfoResp{ Certificate: bundler.PemBlockToString(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}), } response := api.NewSuccessResponse(resp) w.Header().Set("Content-Type", "application/json") enc := json.NewEncoder(w) return enc.Encode(response) }
// ProcessRequest validates and processes the incoming request. It is // a wrapper around a validator and the ParseRequest function. func (g *Generator) ProcessRequest(req *CertificateRequest) (csr, key []byte, err error) { log.Info("generate received request") err = g.Validator(req) if err != nil { log.Warningf("invalid request: %v", err) return } csr, key, err = ParseRequest(req) if err != nil { return nil, nil, err } return }
// scanHandler is an HTTP handler that accepts GET parameters for host (required) // family and scanner, and uses these to perform scans, returning a JSON blob result. func scanHandler(w http.ResponseWriter, r *http.Request) error { log.Info("setting up scan handler") err := r.ParseForm() log.Info(r.Form) if err != nil { log.Warningf("failed to parse body: %v", err) return errors.NewBadRequest(err) } if len(r.Form["host"]) == 0 { log.Warningf("no host given") return errors.NewBadRequestString("no host given") } host := r.Form["host"][0] var family, scanner string if len(r.Form["family"]) > 0 { family = r.Form["family"][0] } if len(r.Form["scanner"]) > 0 { scanner = r.Form["scanner"][0] } results, err := scan.Default.RunScans(host, family, scanner) if err != nil { log.Warningf("%v", err) return errors.NewBadRequest(err) } response := api.NewSuccessResponse(results) enc := json.NewEncoder(w) err = enc.Encode(response) return err }
// registerHandlers instantiates various handlers and associate them to corresponding endpoints. func registerHandlers(c cli.Config) error { log.Info("Setting up signer endpoint") s, err := sign.SignerFromConfig(c) if err != nil { log.Warningf("sign and authsign endpoints are disabled: %v", err) } else { if signHandler, err := apisign.NewHandlerFromSigner(s); err == nil { log.Info("Assigning handler to /sign") http.Handle("/api/v1/cfssl/sign", signHandler) } else { log.Warningf("endpoint '/api/v1/cfssl/sign' is disabled: %v", err) } if signHandler, err := apisign.NewAuthHandlerFromSigner(s); err == nil { log.Info("Assigning handler to /authsign") http.Handle("/api/v1/cfssl/authsign", signHandler) } else { log.Warningf("endpoint '/api/v1/cfssl/authsign' is disabled: %v", err) } } log.Info("Setting up info endpoint") infoHandler, err := info.NewHandler(s) if err != nil { log.Warningf("endpoint '/api/v1/cfssl/info' is disabled: %v", err) } else { http.Handle("/api/v1/cfssl/info", infoHandler) } log.Info("Setting up new cert endpoint") if err != nil { log.Errorf("endpoint '/api/v1/cfssl/newcert' is disabled") } else { newCertGenerator := generator.NewCertGeneratorHandlerFromSigner(generator.CSRValidate, s) http.Handle("/api/v1/cfssl/newcert", newCertGenerator) } log.Info("Setting up bundler endpoint") bundleHandler, err := bundle.NewHandler(c.CABundleFile, c.IntBundleFile) if err != nil { log.Warningf("endpoint '/api/v1/cfssl/bundle' is disabled: %v", err) } else { http.Handle("/api/v1/cfssl/bundle", bundleHandler) } log.Info("Setting up CSR endpoint") generatorHandler, err := generator.NewHandler(generator.CSRValidate) if err != nil { log.Errorf("Failed to set up CSR endpoint: %v", err) return err } http.Handle("/api/v1/cfssl/newkey", generatorHandler) log.Info("Setting up initial CA endpoint") http.Handle("/api/v1/cfssl/init_ca", initca.NewHandler()) log.Info("Setting up scan endpoint") http.Handle("/api/v1/cfssl/scan", scan.NewHandler()) log.Info("Setting up scaninfo endpoint") http.Handle("/api/v1/cfssl/scaninfo", scan.NewInfoHandler()) log.Info("Handler set up complete.") return nil }
// Handle responds to requests for the CA to generate a new private // key and certificate on behalf of the client. The format for these // requests is documented in the API documentation. func (cg *CertGeneratorHandler) Handle(w http.ResponseWriter, r *http.Request) error { log.Info("request for CSR") req := new(genSignRequest) body, err := ioutil.ReadAll(r.Body) if err != nil { log.Warningf("failed to read request body: %v", err) return errors.NewBadRequest(err) } err = json.Unmarshal(body, req) if err != nil { log.Warningf("failed to unmarshal request: %v", err) return errors.NewBadRequest(err) } if req.Request == nil { log.Warning("empty request received") return errors.NewBadRequestString("missing request section") } if req.Request.CA != nil { log.Warningf("request received with CA section") return errors.NewBadRequestString("ca section only permitted in initca") } csr, key, err := cg.generator.ProcessRequest(req.Request) if err != nil { log.Warningf("failed to process CSR: %v", err) // The validator returns a *cfssl/errors.HttpError return err } var certPEM []byte var profile *config.SigningProfile policy := cg.signer.Policy() if policy != nil && policy.Profiles != nil && req.Profile != "" { profile = policy.Profiles[req.Profile] } if profile == nil && policy != nil { profile = policy.Default } // This API does not override the subject because it was already added to the CSR signReq := signer.SignRequest{ Hosts: signer.SplitHosts(req.Hostname), Request: string(csr), Profile: req.Profile, Label: req.Label, } certBytes, err := cg.signer.Sign(signReq) if err != nil { log.Warningf("failed to sign request: %v", err) return err } reqSum, err := computeSum(csr) if err != nil { return errors.NewBadRequest(err) } certSum, err := computeSum(certBytes) if err != nil { return errors.NewBadRequest(err) } result := map[string]interface{}{ "private_key": string(key), "certificate_request": string(csr), "certificate": string(certPEM), "sums": map[string]Sum{ "certificate_request": reqSum, "certificate": certSum, }, } return api.SendResponse(w, result) }