mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-20 20:05:14 +00: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
2.5 KiB
Go
97 lines
2.5 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package transit
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
|
"github.com/hashicorp/vault/sdk/helper/keysutil"
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
)
|
|
|
|
const WrappingKeyName = "wrapping-key"
|
|
|
|
func (b *backend) pathWrappingKey() *framework.Path {
|
|
return &framework.Path{
|
|
Pattern: "wrapping_key",
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
OperationPrefix: operationPrefixTransit,
|
|
OperationSuffix: "wrapping-key",
|
|
},
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
logical.ReadOperation: b.pathWrappingKeyRead,
|
|
},
|
|
HelpSynopsis: pathWrappingKeyHelpSyn,
|
|
HelpDescription: pathWrappingKeyHelpDesc,
|
|
}
|
|
}
|
|
|
|
func (b *backend) pathWrappingKeyRead(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
|
|
p, err := b.getWrappingKey(ctx, req.Storage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
wrappingKey := p.Keys[strconv.Itoa(p.LatestVersion)]
|
|
|
|
derBytes, err := x509.MarshalPKIXPublicKey(wrappingKey.RSAKey.Public())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error marshaling RSA public key: %w", err)
|
|
}
|
|
pemBlock := &pem.Block{
|
|
Type: "PUBLIC KEY",
|
|
Bytes: derBytes,
|
|
}
|
|
pemBytes := pem.EncodeToMemory(pemBlock)
|
|
if pemBytes == nil || len(pemBytes) == 0 {
|
|
return nil, fmt.Errorf("failed to PEM-encode RSA public key")
|
|
}
|
|
|
|
publicKeyString := string(pemBytes)
|
|
|
|
resp := &logical.Response{
|
|
Data: map[string]interface{}{
|
|
"public_key": publicKeyString,
|
|
},
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
func (b *backend) getWrappingKey(ctx context.Context, storage logical.Storage) (*keysutil.Policy, error) {
|
|
polReq := keysutil.PolicyRequest{
|
|
Upsert: true,
|
|
Storage: storage,
|
|
Name: fmt.Sprintf("import/%s", WrappingKeyName),
|
|
KeyType: keysutil.KeyType_RSA4096,
|
|
Derived: false,
|
|
Convergent: false,
|
|
Exportable: false,
|
|
AllowPlaintextBackup: false,
|
|
AutoRotatePeriod: 0,
|
|
}
|
|
p, _, err := b.GetPolicy(ctx, polReq, b.GetRandomReader())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if p == nil {
|
|
return nil, fmt.Errorf("error retrieving wrapping key: returned policy was nil")
|
|
}
|
|
if b.System().CachingDisabled() {
|
|
p.Unlock()
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
const (
|
|
pathWrappingKeyHelpSyn = "Returns the public key to use for wrapping imported keys"
|
|
pathWrappingKeyHelpDesc = "This path is used to retrieve the RSA-4096 wrapping key " +
|
|
"for wrapping keys that are being imported into transit."
|
|
)
|