2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2023-01-27 18:34:31 +03:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
type PKIListIntermediateCommand struct {
|
2023-01-27 18:34:31 +03:00
|
|
|
*BaseCommand
|
|
|
|
|
|
|
|
flagConfig string
|
|
|
|
flagReturnIndicator string
|
|
|
|
flagDefaultDisabled bool
|
|
|
|
flagList bool
|
|
|
|
|
|
|
|
flagUseNames bool
|
|
|
|
|
|
|
|
flagSignatureMatch bool
|
|
|
|
flagIndirectSignMatch bool
|
|
|
|
flagKeyIdMatch bool
|
|
|
|
flagSubjectMatch bool
|
|
|
|
flagPathMatch bool
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) Synopsis() string {
|
2023-02-09 19:40:26 +03:00
|
|
|
return "Determine which of a list of certificates, were issued by a given parent certificate"
|
2023-01-27 18:34:31 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) Help() string {
|
2023-01-27 18:34:31 +03:00
|
|
|
helpText := `
|
|
|
|
Usage: vault pki list-intermediates PARENT [CHILD] [CHILD] [CHILD] ...
|
2023-01-27 19:37:42 +03:00
|
|
|
|
|
|
|
Lists the set of intermediate CAs issued by this parent issuer.
|
|
|
|
|
|
|
|
PARENT is the certificate that might be the issuer that everything should
|
|
|
|
be verified against.
|
|
|
|
|
|
|
|
CHILD is an optional list of paths to certificates to be compared to the
|
|
|
|
PARENT, or pki mounts to look for certificates on. If CHILD is omitted
|
|
|
|
entirely, the list will be constructed from all accessible pki mounts.
|
|
|
|
|
|
|
|
This returns a list of issuing certificates, and whether they are a match.
|
|
|
|
By default, the type of match required is whether the PARENT has the
|
|
|
|
expected subject, key_id, and could have (directly) signed this issuer.
|
|
|
|
The match criteria can be updated by changed the corresponding flag.
|
|
|
|
|
|
|
|
` + c.Flags().Help()
|
2023-01-27 18:34:31 +03:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) Flags() *FlagSets {
|
2023-01-27 18:34:31 +03:00
|
|
|
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
|
|
|
|
f := set.NewFlagSet("Command Options")
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "subject_match",
|
|
|
|
Target: &c.flagSubjectMatch,
|
|
|
|
Default: true,
|
|
|
|
EnvVar: "",
|
2023-01-27 19:37:42 +03:00
|
|
|
Usage: `Whether the subject name of the potential parent cert matches the issuer name of the child cert.`,
|
2023-01-27 18:34:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "key_id_match",
|
|
|
|
Target: &c.flagKeyIdMatch,
|
|
|
|
Default: true,
|
|
|
|
EnvVar: "",
|
2023-01-27 19:37:42 +03:00
|
|
|
Usage: `Whether the subject key id (SKID) of the potential parent cert matches the authority key id (AKID) of the child cert.`,
|
2023-01-27 18:34:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "path_match",
|
|
|
|
Target: &c.flagPathMatch,
|
|
|
|
Default: false,
|
|
|
|
EnvVar: "",
|
2023-01-27 19:37:42 +03:00
|
|
|
Usage: `Whether the potential parent appears in the certificate chain field (ca_chain) of the issued cert.`,
|
2023-01-27 18:34:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "direct_sign",
|
|
|
|
Target: &c.flagSignatureMatch,
|
|
|
|
Default: true,
|
|
|
|
EnvVar: "",
|
2023-01-27 19:37:42 +03:00
|
|
|
Usage: `Whether the key of the potential parent directly signed this issued certificate.`,
|
2023-01-27 18:34:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "indirect_sign",
|
|
|
|
Target: &c.flagIndirectSignMatch,
|
|
|
|
Default: true,
|
|
|
|
EnvVar: "",
|
2023-01-27 19:37:42 +03:00
|
|
|
Usage: `Whether trusting the parent certificate is sufficient to trust the child certificate.`,
|
2023-01-27 18:34:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "use_names",
|
|
|
|
Target: &c.flagUseNames,
|
|
|
|
Default: false,
|
|
|
|
EnvVar: "",
|
2023-01-27 19:37:42 +03:00
|
|
|
Usage: `Whether the list of issuers returned is referred to by name (when it exists) rather than by uuid.`,
|
2023-01-27 18:34:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) Run(args []string) int {
|
2023-01-27 18:34:31 +03:00
|
|
|
f := c.Flags()
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = f.Args()
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
|
|
|
c.UI.Error("Not enough arguments (expected potential parent, got nothing)")
|
|
|
|
return 1
|
|
|
|
} else if len(args) > 2 {
|
|
|
|
for _, arg := range args {
|
|
|
|
if strings.HasPrefix(arg, "-") {
|
|
|
|
c.UI.Warn(fmt.Sprintf("Options (%v) must be specified before positional arguments (%v)", arg, args[0]))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to obtain client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
issuer := sanitizePath(args[0])
|
|
|
|
var issued []string
|
|
|
|
if len(args) > 1 {
|
|
|
|
for _, arg := range args[1:] {
|
|
|
|
cleanPath := sanitizePath(arg)
|
|
|
|
// Arg Might be a Fully Qualified Path
|
|
|
|
if strings.Contains(cleanPath, "/issuer/") ||
|
|
|
|
strings.Contains(cleanPath, "/certs/") ||
|
|
|
|
strings.Contains(cleanPath, "/revoked/") {
|
|
|
|
issued = append(issued, cleanPath)
|
|
|
|
} else { // Or Arg Might be a Mount
|
|
|
|
mountCaList, err := c.getIssuerListFromMount(client, arg)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
issued = append(issued, mountCaList...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mountListRaw, err := client.Logical().Read("/sys/mounts/")
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to Read List of Mounts With Potential Issuers: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
for path, rawValueMap := range mountListRaw.Data {
|
|
|
|
valueMap := rawValueMap.(map[string]interface{})
|
|
|
|
if valueMap["type"].(string) == "pki" {
|
|
|
|
mountCaList, err := c.getIssuerListFromMount(client, sanitizePath(path))
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
issued = append(issued, mountCaList...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
childrenMatches := make(map[string]bool)
|
|
|
|
|
|
|
|
constraintMap := map[string]bool{
|
|
|
|
// This comparison isn't strictly correct, despite a standard ordering these are sets
|
|
|
|
"subject_match": c.flagSubjectMatch,
|
|
|
|
"path_match": c.flagPathMatch,
|
|
|
|
"trust_match": c.flagIndirectSignMatch,
|
|
|
|
"key_id_match": c.flagKeyIdMatch,
|
|
|
|
"signature_match": c.flagSignatureMatch,
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:51:44 +03:00
|
|
|
issuerResp, err := readIssuer(client, issuer)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to read parent issuer on path %s: %s", issuer, err.Error()))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-01-27 18:34:31 +03:00
|
|
|
for _, child := range issued {
|
|
|
|
path := sanitizePath(child)
|
|
|
|
if path != "" {
|
2023-02-14 16:51:44 +03:00
|
|
|
verifyResults, err := verifySignBetween(client, issuerResp, path)
|
2023-01-27 18:34:31 +03:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to run verification on path %v: %v", path, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
childrenMatches[path] = checkIfResultsMatchFilters(verifyResults, constraintMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.outputResults(childrenMatches)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) getIssuerListFromMount(client *api.Client, mountString string) ([]string, error) {
|
2023-01-27 18:34:31 +03:00
|
|
|
var issuerList []string
|
|
|
|
issuerListEndpoint := sanitizePath(mountString) + "/issuers"
|
|
|
|
rawIssuersResp, err := client.Logical().List(issuerListEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return issuerList, fmt.Errorf("failed to read list of issuers within mount %v: %v", mountString, err)
|
|
|
|
}
|
|
|
|
if rawIssuersResp == nil { // No Issuers (Empty Mount)
|
|
|
|
return issuerList, nil
|
|
|
|
}
|
|
|
|
issuersMap := rawIssuersResp.Data["keys"]
|
|
|
|
certList := issuersMap.([]interface{})
|
|
|
|
for _, certId := range certList {
|
|
|
|
identifier := certId.(string)
|
|
|
|
if c.flagUseNames {
|
|
|
|
issuerReadResp, err := client.Logical().Read(sanitizePath(mountString) + "/issuer/" + identifier)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Warn(fmt.Sprintf("Unable to Fetch Issuer to Recover Name at: %v", sanitizePath(mountString)+"/issuer/"+identifier))
|
|
|
|
}
|
|
|
|
if issuerReadResp != nil {
|
|
|
|
issuerName := issuerReadResp.Data["issuer_name"].(string)
|
|
|
|
if issuerName != "" {
|
|
|
|
identifier = issuerName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
issuerList = append(issuerList, sanitizePath(mountString)+"/issuer/"+identifier)
|
|
|
|
}
|
|
|
|
return issuerList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkIfResultsMatchFilters(verifyResults, constraintMap map[string]bool) bool {
|
|
|
|
for key, required := range constraintMap {
|
2023-01-27 19:37:42 +03:00
|
|
|
if required && !verifyResults[key] {
|
|
|
|
return false
|
2023-01-27 18:34:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) outputResults(results map[string]bool) error {
|
2023-01-27 18:34:31 +03:00
|
|
|
switch Format(c.UI) {
|
|
|
|
case "", "table":
|
|
|
|
return c.outputResultsTable(results)
|
|
|
|
case "json":
|
|
|
|
return c.outputResultsJSON(results)
|
|
|
|
case "yaml":
|
|
|
|
return c.outputResultsYAML(results)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown output format: %v", Format(c.UI))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) outputResultsTable(results map[string]bool) error {
|
2023-01-27 18:34:31 +03:00
|
|
|
data := []string{"intermediate" + hopeDelim + "match?"}
|
|
|
|
for field, finding := range results {
|
|
|
|
row := field + hopeDelim + strconv.FormatBool(finding)
|
|
|
|
data = append(data, row)
|
|
|
|
}
|
|
|
|
c.UI.Output(tableOutput(data, &columnize.Config{
|
|
|
|
Delim: hopeDelim,
|
|
|
|
}))
|
|
|
|
c.UI.Output("\n")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) outputResultsJSON(results map[string]bool) error {
|
2023-01-27 18:34:31 +03:00
|
|
|
bytes, err := json.MarshalIndent(results, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UI.Output(string(bytes))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:37:42 +03:00
|
|
|
func (c *PKIListIntermediateCommand) outputResultsYAML(results map[string]bool) error {
|
2023-01-27 18:34:31 +03:00
|
|
|
bytes, err := yaml.Marshal(results)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UI.Output(string(bytes))
|
|
|
|
return nil
|
|
|
|
}
|