2015-05-15 16:13:05 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
2015-11-12 16:24:32 +00:00
|
|
|
"encoding/base64"
|
2015-05-15 16:13:05 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
"github.com/hashicorp/vault/helper/certutil"
|
2015-05-15 16:13:05 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-09-29 23:13:54 +00:00
|
|
|
func pathIssue(b *backend) *framework.Path {
|
2015-11-16 16:42:06 +00:00
|
|
|
ret := &framework.Path{
|
2015-09-29 23:13:54 +00:00
|
|
|
Pattern: "issue/" + framework.GenericNameRegex("role"),
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathIssue,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
2015-10-02 19:47:45 +00:00
|
|
|
HelpSynopsis: pathIssueHelpSyn,
|
|
|
|
HelpDescription: pathIssueHelpDesc,
|
2015-09-29 23:13:54 +00:00
|
|
|
}
|
2015-11-16 16:42:06 +00:00
|
|
|
|
|
|
|
ret.Fields = addNonCACommonFields(map[string]*framework.FieldSchema{})
|
|
|
|
|
|
|
|
return ret
|
2015-09-29 23:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func pathSign(b *backend) *framework.Path {
|
2015-09-30 01:48:31 +00:00
|
|
|
ret := &framework.Path{
|
2015-09-29 23:13:54 +00:00
|
|
|
Pattern: "sign/" + framework.GenericNameRegex("role"),
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathSign,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
2015-10-02 19:47:45 +00:00
|
|
|
HelpSynopsis: pathSignHelpSyn,
|
|
|
|
HelpDescription: pathSignHelpDesc,
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
2015-09-30 01:48:31 +00:00
|
|
|
|
2015-11-16 16:42:06 +00:00
|
|
|
ret.Fields = addNonCACommonFields(map[string]*framework.FieldSchema{})
|
|
|
|
|
2015-09-30 01:48:31 +00:00
|
|
|
ret.Fields["csr"] = &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-11-12 16:24:32 +00:00
|
|
|
Default: "",
|
2015-09-30 01:48:31 +00:00
|
|
|
Description: `PEM-format CSR to be signed.`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 15:16:09 +00:00
|
|
|
func pathSignVerbatim(b *backend) *framework.Path {
|
|
|
|
ret := &framework.Path{
|
|
|
|
Pattern: "sign-verbatim/" + framework.GenericNameRegex("role"),
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathSignVerbatim,
|
2015-11-18 15:16:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathSignHelpSyn,
|
|
|
|
HelpDescription: pathSignHelpDesc,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Fields = addNonCACommonFields(map[string]*framework.FieldSchema{})
|
|
|
|
|
|
|
|
ret.Fields["csr"] = &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "",
|
|
|
|
Description: `PEM-format CSR to be signed. Values will be
|
|
|
|
taken verbatim from the CSR, except for
|
|
|
|
basic constraints.`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-11-19 21:51:27 +00:00
|
|
|
// pathIssue issues a certificate and private key from given parameters,
|
|
|
|
// subject to role restrictions
|
2015-10-02 19:47:45 +00:00
|
|
|
func (b *backend) pathIssue(
|
2015-05-15 16:13:05 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-11-18 15:16:09 +00:00
|
|
|
roleName := data.Get("role").(string)
|
|
|
|
|
|
|
|
// Get the role
|
|
|
|
role, err := b.getRole(req.Storage, roleName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Unknown role: %s", roleName)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.pathIssueSignCert(req, data, role, false, false)
|
2015-09-30 01:48:31 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 21:51:27 +00:00
|
|
|
// pathSign issues a certificate from a submitted CSR, subject to role
|
|
|
|
// restrictions
|
2015-10-02 19:47:45 +00:00
|
|
|
func (b *backend) pathSign(
|
2015-09-30 01:48:31 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
roleName := data.Get("role").(string)
|
|
|
|
|
|
|
|
// Get the role
|
|
|
|
role, err := b.getRole(req.Storage, roleName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Unknown role: %s", roleName)), nil
|
|
|
|
}
|
|
|
|
|
2015-11-18 15:16:09 +00:00
|
|
|
return b.pathIssueSignCert(req, data, role, true, false)
|
|
|
|
}
|
|
|
|
|
2015-11-19 21:51:27 +00:00
|
|
|
// pathSignVerbatim issues a certificate from a submitted CSR, *not* subject to
|
|
|
|
// role restrictions
|
2015-11-18 15:16:09 +00:00
|
|
|
func (b *backend) pathSignVerbatim(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
|
|
|
|
ttl := b.System().DefaultLeaseTTL()
|
|
|
|
role := &roleEntry{
|
|
|
|
TTL: ttl.String(),
|
|
|
|
AllowLocalhost: true,
|
|
|
|
AllowAnyName: true,
|
|
|
|
AllowIPSANs: true,
|
|
|
|
EnforceHostnames: false,
|
2016-02-19 23:50:51 +00:00
|
|
|
KeyType: "any",
|
2015-11-18 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.pathIssueSignCert(req, data, role, true, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathIssueSignCert(
|
|
|
|
req *logical.Request, data *framework.FieldData, role *roleEntry, useCSR, useCSRValues bool) (*logical.Response, error) {
|
2015-11-16 21:32:49 +00:00
|
|
|
format := getFormat(data)
|
|
|
|
if format == "" {
|
2015-11-12 16:24:32 +00:00
|
|
|
return logical.ErrorResponse(
|
2016-02-01 18:19:41 +00:00
|
|
|
`The "format" path parameter must be "pem", "der", or "pem_bundle"`), nil
|
2015-11-12 16:24:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:48:31 +00:00
|
|
|
var caErr error
|
|
|
|
signingBundle, caErr := fetchCAInfo(req)
|
|
|
|
switch caErr.(type) {
|
|
|
|
case certutil.UserError:
|
|
|
|
return nil, certutil.UserError{Err: fmt.Sprintf(
|
|
|
|
"Could not fetch the CA certificate (was one set?): %s", caErr)}
|
|
|
|
case certutil.InternalError:
|
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf(
|
|
|
|
"Error fetching CA certificate: %s", caErr)}
|
|
|
|
}
|
|
|
|
|
2015-10-02 19:47:45 +00:00
|
|
|
var parsedBundle *certutil.ParsedCertBundle
|
2015-11-18 15:16:09 +00:00
|
|
|
var err error
|
2015-10-02 19:47:45 +00:00
|
|
|
if useCSR {
|
2015-11-18 15:16:09 +00:00
|
|
|
parsedBundle, err = signCert(b, role, signingBundle, false, useCSRValues, req, data)
|
2015-10-02 19:47:45 +00:00
|
|
|
} else {
|
2015-10-14 01:13:40 +00:00
|
|
|
parsedBundle, err = generateCert(b, role, signingBundle, false, req, data)
|
2015-10-02 19:47:45 +00:00
|
|
|
}
|
2015-09-30 01:48:31 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case certutil.UserError:
|
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2015-08-29 13:03:02 +00:00
|
|
|
case certutil.InternalError:
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 16:43:36 +00:00
|
|
|
cb, err := parsedBundle.ToCertBundle()
|
2015-06-15 17:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error converting raw cert bundle to cert bundle: %s", err)
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 17:33:23 +00:00
|
|
|
resp := b.Secret(SecretCertsType).Response(
|
2015-11-19 16:32:18 +00:00
|
|
|
map[string]interface{}{
|
2016-03-08 14:25:48 +00:00
|
|
|
"certificate": cb.Certificate,
|
|
|
|
"issuing_ca": cb.IssuingCA,
|
|
|
|
"serial_number": cb.SerialNumber,
|
2015-11-19 16:32:18 +00:00
|
|
|
},
|
2015-06-15 17:33:23 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"serial_number": cb.SerialNumber,
|
|
|
|
})
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2016-02-01 18:19:41 +00:00
|
|
|
switch format {
|
|
|
|
case "pem":
|
|
|
|
resp.Data["issuing_ca"] = cb.IssuingCA
|
|
|
|
resp.Data["certificate"] = cb.Certificate
|
|
|
|
|
|
|
|
if !useCSR {
|
|
|
|
resp.Data["private_key"] = cb.PrivateKey
|
|
|
|
resp.Data["private_key_type"] = cb.PrivateKeyType
|
|
|
|
}
|
|
|
|
|
|
|
|
case "pem_bundle":
|
|
|
|
resp.Data["issuing_ca"] = cb.IssuingCA
|
|
|
|
resp.Data["certificate"] = fmt.Sprintf("%s\n%s", cb.Certificate, cb.IssuingCA)
|
|
|
|
if !useCSR {
|
|
|
|
resp.Data["private_key"] = cb.PrivateKey
|
|
|
|
resp.Data["private_key_type"] = cb.PrivateKeyType
|
|
|
|
resp.Data["certificate"] = fmt.Sprintf("%s\n%s\n%s", cb.PrivateKey, cb.Certificate, cb.IssuingCA)
|
|
|
|
}
|
2015-11-19 16:32:18 +00:00
|
|
|
|
2016-02-01 18:19:41 +00:00
|
|
|
case "der":
|
2015-11-12 16:24:32 +00:00
|
|
|
resp.Data["certificate"] = base64.StdEncoding.EncodeToString(parsedBundle.CertificateBytes)
|
|
|
|
resp.Data["issuing_ca"] = base64.StdEncoding.EncodeToString(parsedBundle.IssuingCABytes)
|
2015-11-19 16:32:18 +00:00
|
|
|
if !useCSR {
|
|
|
|
resp.Data["private_key"] = base64.StdEncoding.EncodeToString(parsedBundle.PrivateKeyBytes)
|
|
|
|
}
|
2015-11-12 16:24:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 13:03:02 +00:00
|
|
|
resp.Secret.TTL = parsedBundle.Certificate.NotAfter.Sub(time.Now())
|
2015-05-15 16:13:05 +00:00
|
|
|
|
|
|
|
err = req.Storage.Put(&logical.StorageEntry{
|
2015-06-15 17:33:23 +00:00
|
|
|
Key: "certs/" + cb.SerialNumber,
|
2015-06-17 16:43:36 +00:00
|
|
|
Value: parsedBundle.CertificateBytes,
|
2015-05-15 16:13:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to store certificate locally")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-10-02 19:47:45 +00:00
|
|
|
const pathIssueHelpSyn = `
|
|
|
|
Request a certificate using a certain role with the provided details.
|
2015-05-15 16:13:05 +00:00
|
|
|
`
|
|
|
|
|
2015-10-02 19:47:45 +00:00
|
|
|
const pathIssueHelpDesc = `
|
|
|
|
This path allows requesting a certificate to be issued according to the
|
|
|
|
policy of the given role. The certificate will only be issued if the
|
|
|
|
requested details are allowed by the role policy.
|
|
|
|
|
|
|
|
This path returns a certificate and a private key. If you want a workflow
|
|
|
|
that does not expose a private key, generate a CSR locally and use the
|
|
|
|
sign path instead.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathSignHelpSyn = `
|
|
|
|
Request certificates using a certain role with the provided details.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathSignHelpDesc = `
|
2015-05-15 16:13:05 +00:00
|
|
|
This path allows requesting certificates to be issued according to the
|
|
|
|
policy of the given role. The certificate will only be issued if the
|
|
|
|
requested common name is allowed by the role policy.
|
2015-10-02 19:47:45 +00:00
|
|
|
|
|
|
|
This path requires a CSR; if you want Vault to generate a private key
|
|
|
|
for you, use the issue path instead.
|
2015-05-15 16:13:05 +00:00
|
|
|
`
|