mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-14 18:47:01 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package pki
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/certutil"
|
|
"github.com/hashicorp/vault/sdk/helper/errutil"
|
|
)
|
|
|
|
func comparePublicKey(sc *storageContext, key *keyEntry, publicKey crypto.PublicKey) (bool, error) {
|
|
publicKeyForKeyEntry, err := getPublicKey(sc.Context, sc.Backend, key)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return certutil.ComparePublicKeysAndType(publicKeyForKeyEntry, publicKey)
|
|
}
|
|
|
|
func getPublicKey(ctx context.Context, b *backend, key *keyEntry) (crypto.PublicKey, error) {
|
|
if key.PrivateKeyType == certutil.ManagedPrivateKey {
|
|
keyId, err := extractManagedKeyId([]byte(key.PrivateKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return getManagedKeyPublicKey(ctx, b, keyId)
|
|
}
|
|
|
|
signer, _, _, err := getSignerFromKeyEntryBytes(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return signer.Public(), nil
|
|
}
|
|
|
|
func getSignerFromKeyEntryBytes(key *keyEntry) (crypto.Signer, certutil.BlockType, *pem.Block, error) {
|
|
if key.PrivateKeyType == certutil.UnknownPrivateKey {
|
|
return nil, certutil.UnknownBlock, nil, errutil.InternalError{Err: fmt.Sprintf("unsupported unknown private key type for key: %s (%s)", key.ID, key.Name)}
|
|
}
|
|
|
|
if key.PrivateKeyType == certutil.ManagedPrivateKey {
|
|
return nil, certutil.UnknownBlock, nil, errutil.InternalError{Err: fmt.Sprintf("can not get a signer from a managed key: %s (%s)", key.ID, key.Name)}
|
|
}
|
|
|
|
bytes, blockType, blk, err := getSignerFromBytes([]byte(key.PrivateKey))
|
|
if err != nil {
|
|
return nil, certutil.UnknownBlock, nil, errutil.InternalError{Err: fmt.Sprintf("failed parsing key entry bytes for key id: %s (%s): %s", key.ID, key.Name, err.Error())}
|
|
}
|
|
|
|
return bytes, blockType, blk, nil
|
|
}
|
|
|
|
func getSignerFromBytes(keyBytes []byte) (crypto.Signer, certutil.BlockType, *pem.Block, error) {
|
|
pemBlock, _ := pem.Decode(keyBytes)
|
|
if pemBlock == nil {
|
|
return nil, certutil.UnknownBlock, pemBlock, errutil.InternalError{Err: "no data found in PEM block"}
|
|
}
|
|
|
|
signer, blk, err := certutil.ParseDERKey(pemBlock.Bytes)
|
|
if err != nil {
|
|
return nil, certutil.UnknownBlock, pemBlock, errutil.InternalError{Err: fmt.Sprintf("failed to parse PEM block: %s", err.Error())}
|
|
}
|
|
return signer, blk, pemBlock, nil
|
|
}
|
|
|
|
func getPublicKeyFromBytes(keyBytes []byte) (crypto.PublicKey, error) {
|
|
signer, _, _, err := getSignerFromBytes(keyBytes)
|
|
if err != nil {
|
|
return nil, errutil.InternalError{Err: fmt.Sprintf("failed parsing key bytes: %s", err.Error())}
|
|
}
|
|
|
|
return signer.Public(), nil
|
|
}
|
|
|
|
func importKeyFromBytes(sc *storageContext, keyValue string, keyName string) (*keyEntry, bool, error) {
|
|
signer, _, _, err := getSignerFromBytes([]byte(keyValue))
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
privateKeyType := certutil.GetPrivateKeyTypeFromSigner(signer)
|
|
if privateKeyType == certutil.UnknownPrivateKey {
|
|
return nil, false, errors.New("unsupported private key type within pem bundle")
|
|
}
|
|
|
|
key, existed, err := sc.importKey(keyValue, keyName, privateKeyType)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return key, existed, nil
|
|
}
|