2015-11-18 15:16:09 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/helper/certutil"
|
2016-07-28 19:19:27 +00:00
|
|
|
"github.com/hashicorp/vault/helper/errutil"
|
2015-11-18 15:16:09 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathGenerateIntermediate(b *backend) *framework.Path {
|
|
|
|
ret := &framework.Path{
|
|
|
|
Pattern: "intermediate/generate/" + framework.GenericNameRegex("exported"),
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathGenerateIntermediate,
|
2015-11-18 15:16:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathGenerateIntermediateHelpSyn,
|
|
|
|
HelpDescription: pathGenerateIntermediateHelpDesc,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Fields = addCACommonFields(map[string]*framework.FieldSchema{})
|
|
|
|
ret.Fields = addCAKeyGenerationFields(ret.Fields)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathSetSignedIntermediate(b *backend) *framework.Path {
|
|
|
|
ret := &framework.Path{
|
|
|
|
Pattern: "intermediate/set-signed",
|
|
|
|
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"certificate": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `PEM-format certificate. This must be a CA
|
|
|
|
certificate with a public key matching the
|
|
|
|
previously-generated key from the generation
|
|
|
|
endpoint.`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathSetSignedIntermediate,
|
2015-11-18 15:16:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathSetSignedIntermediateHelpSyn,
|
|
|
|
HelpDescription: pathSetSignedIntermediateHelpDesc,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathGenerateIntermediate(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
exported, format, role, errorResp := b.getGenerationParams(data)
|
|
|
|
if errorResp != nil {
|
|
|
|
return errorResp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp *logical.Response
|
|
|
|
parsedBundle, err := generateIntermediateCSR(b, role, nil, req, data)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.UserError:
|
2015-11-18 15:16:09 +00:00
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.InternalError:
|
2015-11-18 15:16:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
csrb, err := parsedBundle.ToCSRBundle()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error converting raw CSR bundle to CSR bundle: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = &logical.Response{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case "pem":
|
|
|
|
resp.Data["csr"] = csrb.CSR
|
|
|
|
if exported {
|
|
|
|
resp.Data["private_key"] = csrb.PrivateKey
|
|
|
|
resp.Data["private_key_type"] = csrb.PrivateKeyType
|
|
|
|
}
|
2016-02-01 18:19:41 +00:00
|
|
|
|
|
|
|
case "pem_bundle":
|
|
|
|
resp.Data["csr"] = csrb.CSR
|
|
|
|
if exported {
|
|
|
|
resp.Data["csr"] = fmt.Sprintf("%s\n%s", csrb.PrivateKey, csrb.CSR)
|
|
|
|
resp.Data["private_key"] = csrb.PrivateKey
|
|
|
|
resp.Data["private_key_type"] = csrb.PrivateKeyType
|
|
|
|
}
|
|
|
|
|
2015-11-18 15:16:09 +00:00
|
|
|
case "der":
|
|
|
|
resp.Data["csr"] = base64.StdEncoding.EncodeToString(parsedBundle.CSRBytes)
|
|
|
|
if exported {
|
|
|
|
resp.Data["private_key"] = base64.StdEncoding.EncodeToString(parsedBundle.PrivateKeyBytes)
|
|
|
|
resp.Data["private_key_type"] = csrb.PrivateKeyType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb := &certutil.CertBundle{}
|
|
|
|
cb.PrivateKey = csrb.PrivateKey
|
|
|
|
cb.PrivateKeyType = csrb.PrivateKeyType
|
|
|
|
|
|
|
|
entry, err := logical.StorageEntryJSON("config/ca_bundle", cb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = req.Storage.Put(entry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathSetSignedIntermediate(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
cert := data.Get("certificate").(string)
|
|
|
|
|
|
|
|
if cert == "" {
|
2017-05-10 14:28:35 +00:00
|
|
|
return logical.ErrorResponse("no certificate provided in the \"certificate\" parameter"), nil
|
2015-11-18 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inputBundle, err := certutil.ParsePEMBundle(cert)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.InternalError:
|
2015-11-18 15:16:09 +00:00
|
|
|
return nil, err
|
|
|
|
default:
|
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if inputBundle.Certificate == nil {
|
|
|
|
return logical.ErrorResponse("supplied certificate could not be successfully parsed"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cb := &certutil.CertBundle{}
|
|
|
|
entry, err := req.Storage.Get("config/ca_bundle")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return logical.ErrorResponse("could not find any existing entry with a private key"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = entry.DecodeJSON(cb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cb.PrivateKey) == 0 || cb.PrivateKeyType == "" {
|
2016-02-09 21:08:30 +00:00
|
|
|
return logical.ErrorResponse("could not find an existing private key"), nil
|
2015-11-18 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parsedCB, err := cb.ToParsedCertBundle()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if parsedCB.PrivateKey == nil {
|
|
|
|
return nil, fmt.Errorf("saved key could not be parsed successfully")
|
|
|
|
}
|
|
|
|
|
|
|
|
inputBundle.PrivateKey = parsedCB.PrivateKey
|
|
|
|
inputBundle.PrivateKeyType = parsedCB.PrivateKeyType
|
|
|
|
inputBundle.PrivateKeyBytes = parsedCB.PrivateKeyBytes
|
|
|
|
|
|
|
|
if !inputBundle.Certificate.IsCA {
|
|
|
|
return logical.ErrorResponse("the given certificate is not marked for CA use and cannot be used with this backend"), nil
|
|
|
|
}
|
|
|
|
|
2016-09-28 00:50:17 +00:00
|
|
|
if err := inputBundle.Verify(); err != nil {
|
|
|
|
return nil, fmt.Errorf("verification of parsed bundle failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-11-18 15:16:09 +00:00
|
|
|
cb, err = inputBundle.ToCertBundle()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error converting raw values into cert bundle: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err = logical.StorageEntryJSON("config/ca_bundle", cb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = req.Storage.Put(entry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:12:58 +00:00
|
|
|
entry.Key = "certs/" + normalizeSerial(cb.SerialNumber)
|
2016-02-24 21:19:01 +00:00
|
|
|
entry.Value = inputBundle.CertificateBytes
|
|
|
|
err = req.Storage.Put(entry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-18 15:16:09 +00:00
|
|
|
// For ease of later use, also store just the certificate at a known
|
2015-11-19 21:51:27 +00:00
|
|
|
// location
|
2015-11-18 15:16:09 +00:00
|
|
|
entry.Key = "ca"
|
|
|
|
entry.Value = inputBundle.CertificateBytes
|
|
|
|
err = req.Storage.Put(entry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-19 21:51:27 +00:00
|
|
|
// Build a fresh CRL
|
2015-11-18 15:16:09 +00:00
|
|
|
err = buildCRL(b, req)
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathGenerateIntermediateHelpSyn = `
|
|
|
|
Generate a new CSR and private key used for signing.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathGenerateIntermediateHelpDesc = `
|
|
|
|
See the API documentation for more information.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathSetSignedIntermediateHelpSyn = `
|
|
|
|
Provide the signed intermediate CA cert.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathSetSignedIntermediateHelpDesc = `
|
|
|
|
See the API documentation for more information.
|
|
|
|
`
|