-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connect: intermediate CA certs generated with the vault provider lack…
… URI SANs (#6491) This only affects vault versions >=1.1.1 because the prior code accidentally relied upon a bug that was fixed in hashicorp/vault#6505 The existing tests should have caught this, but they were using a vendored copy of vault version 0.10.3. This fixes the tests by running an actual copy of vault instead of an in-process copy. This has the added benefit of changing the dependency on vault to just vault/api. Also update VaultProvider to use similar SetIntermediate validation code as the ConsulProvider implementation.
- Loading branch information
Showing
818 changed files
with
9,719 additions
and
218,595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ca | ||
|
||
import ( | ||
"bytes" | ||
"crypto/x509" | ||
"fmt" | ||
|
||
"github.com/hashicorp/consul/agent/connect" | ||
) | ||
|
||
func validateSetIntermediate( | ||
intermediatePEM, rootPEM string, | ||
currentPrivateKey string, // optional | ||
spiffeID *connect.SpiffeIDSigning, | ||
) error { | ||
// Get the key from the incoming intermediate cert so we can compare it | ||
// to the currently stored key. | ||
intermediate, err := connect.ParseCert(intermediatePEM) | ||
if err != nil { | ||
return fmt.Errorf("error parsing intermediate PEM: %v", err) | ||
} | ||
|
||
if currentPrivateKey != "" { | ||
privKey, err := connect.ParseSigner(currentPrivateKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Compare the two keys to make sure they match. | ||
b1, err := x509.MarshalPKIXPublicKey(intermediate.PublicKey) | ||
if err != nil { | ||
return err | ||
} | ||
b2, err := x509.MarshalPKIXPublicKey(privKey.Public()) | ||
if err != nil { | ||
return err | ||
} | ||
if !bytes.Equal(b1, b2) { | ||
return fmt.Errorf("intermediate cert is for a different private key") | ||
} | ||
} | ||
|
||
// Validate the remaining fields and make sure the intermediate validates against | ||
// the given root cert. | ||
if !intermediate.IsCA { | ||
return fmt.Errorf("intermediate is not a CA certificate") | ||
} | ||
if uriCount := len(intermediate.URIs); uriCount != 1 { | ||
return fmt.Errorf("incoming intermediate cert has unexpected number of URIs: %d", uriCount) | ||
} | ||
if got, want := intermediate.URIs[0].String(), spiffeID.URI().String(); got != want { | ||
return fmt.Errorf("incoming cert URI %q does not match current URI: %q", got, want) | ||
} | ||
|
||
pool := x509.NewCertPool() | ||
pool.AppendCertsFromPEM([]byte(rootPEM)) | ||
_, err = intermediate.Verify(x509.VerifyOptions{ | ||
Roots: pool, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("could not verify intermediate cert against root: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.