2022-03-16 17:04:07 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2020-09-16 12:02:02 -04:00
|
|
|
package session
|
2020-09-03 13:24:34 -04:00
|
|
|
|
|
|
|
|
import (
|
2022-03-16 17:04:07 -04:00
|
|
|
"context"
|
2020-09-03 13:24:34 -04:00
|
|
|
"crypto/ed25519"
|
|
|
|
|
|
2021-02-03 16:25:56 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
2021-11-04 08:55:30 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/libs/crypto"
|
2022-03-16 17:04:07 -04:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2020-09-03 13:24:34 -04:00
|
|
|
)
|
|
|
|
|
|
2022-07-20 16:24:46 -07:00
|
|
|
// DeriveED25519Key generates a key based on the project's session DEK, the
|
2020-09-03 13:24:34 -04:00
|
|
|
// requesting user, and the generated job ID.
|
2022-03-16 17:04:07 -04:00
|
|
|
func DeriveED25519Key(ctx context.Context, wrapper wrapping.Wrapper, userId, jobId string) (ed25519.PublicKey, ed25519.PrivateKey, error) {
|
2021-02-03 16:25:56 -08:00
|
|
|
const op = "session.DeriveED25519Key"
|
2021-04-08 20:54:32 -04:00
|
|
|
var uId, jId []byte
|
|
|
|
|
if userId != "" {
|
|
|
|
|
uId = []byte(userId)
|
2020-09-03 13:24:34 -04:00
|
|
|
}
|
2021-04-08 20:54:32 -04:00
|
|
|
if jobId != "" {
|
|
|
|
|
jId = []byte(jobId)
|
2020-09-03 13:24:34 -04:00
|
|
|
}
|
2021-11-04 08:55:30 -04:00
|
|
|
if wrapper == nil {
|
2023-06-06 17:09:08 -04:00
|
|
|
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing wrapper")
|
2021-11-04 08:55:30 -04:00
|
|
|
}
|
2021-04-08 20:54:32 -04:00
|
|
|
|
2022-03-16 17:04:07 -04:00
|
|
|
reader, err := crypto.NewDerivedReader(ctx, wrapper, 32, uId, jId)
|
2021-02-03 16:25:56 -08:00
|
|
|
if err != nil {
|
2023-06-06 17:09:08 -04:00
|
|
|
return nil, nil, errors.Wrap(ctx, err, op)
|
2021-02-03 16:25:56 -08:00
|
|
|
}
|
2021-04-08 20:54:32 -04:00
|
|
|
return ed25519.GenerateKey(reader)
|
2020-09-03 13:24:34 -04:00
|
|
|
}
|