2016-01-09 02:21:02 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/vault/helper/pgpkeys"
|
|
|
|
"github.com/hashicorp/vault/helper/xor"
|
|
|
|
"github.com/hashicorp/vault/shamir"
|
|
|
|
)
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// GenerateRootConfig holds the configuration for a root generation
|
2016-01-09 02:21:02 +00:00
|
|
|
// command.
|
2016-01-15 15:55:35 +00:00
|
|
|
type GenerateRootConfig struct {
|
2016-01-09 02:21:02 +00:00
|
|
|
Nonce string
|
|
|
|
PGPKey string
|
|
|
|
PGPFingerprint string
|
|
|
|
OTP string
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// GenerateRootResult holds the result of a root generation update
|
2016-01-09 02:21:02 +00:00
|
|
|
// command
|
2016-01-15 15:55:35 +00:00
|
|
|
type GenerateRootResult struct {
|
2016-01-09 02:21:02 +00:00
|
|
|
Progress int
|
|
|
|
Required int
|
|
|
|
EncodedRootToken string
|
|
|
|
PGPFingerprint string
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// GenerateRoot is used to return the root generation progress (num shares)
|
|
|
|
func (c *Core) GenerateRootProgress() (int, error) {
|
2016-01-09 02:21:02 +00:00
|
|
|
c.stateLock.RLock()
|
|
|
|
defer c.stateLock.RUnlock()
|
|
|
|
if c.sealed {
|
|
|
|
return 0, ErrSealed
|
|
|
|
}
|
|
|
|
if c.standby {
|
|
|
|
return 0, ErrStandby
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootLock.Lock()
|
|
|
|
defer c.generateRootLock.Unlock()
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
return len(c.generateRootProgress), nil
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// GenerateRootConfig is used to read the root generation configuration
|
2016-01-09 02:21:02 +00:00
|
|
|
// It stubbornly refuses to return the OTP if one is there.
|
2016-01-15 15:55:35 +00:00
|
|
|
func (c *Core) GenerateRootConfiguration() (*GenerateRootConfig, error) {
|
2016-01-09 02:21:02 +00:00
|
|
|
c.stateLock.RLock()
|
|
|
|
defer c.stateLock.RUnlock()
|
|
|
|
if c.sealed {
|
|
|
|
return nil, ErrSealed
|
|
|
|
}
|
|
|
|
if c.standby {
|
|
|
|
return nil, ErrStandby
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootLock.Lock()
|
|
|
|
defer c.generateRootLock.Unlock()
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
// Copy the config if any
|
2016-01-15 15:55:35 +00:00
|
|
|
var conf *GenerateRootConfig
|
|
|
|
if c.generateRootConfig != nil {
|
|
|
|
conf = new(GenerateRootConfig)
|
|
|
|
*conf = *c.generateRootConfig
|
2016-01-09 02:21:02 +00:00
|
|
|
conf.OTP = ""
|
|
|
|
}
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// GenerateRootInit is used to initialize the root generation settings
|
|
|
|
func (c *Core) GenerateRootInit(otp, pgpKey string) error {
|
2016-01-09 02:21:02 +00:00
|
|
|
var fingerprint string
|
|
|
|
switch {
|
|
|
|
case len(otp) > 0:
|
|
|
|
otpBytes, err := base64.StdEncoding.DecodeString(otp)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error decoding base64 OTP value: %s", err)
|
|
|
|
}
|
|
|
|
if otpBytes == nil || len(otpBytes) != 16 {
|
|
|
|
return fmt.Errorf("decoded OTP value is invalid or wrong length")
|
|
|
|
}
|
|
|
|
|
|
|
|
case len(pgpKey) > 0:
|
|
|
|
fingerprints, err := pgpkeys.GetFingerprints([]string{pgpKey}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing PGP key: %s", err)
|
|
|
|
}
|
|
|
|
if len(fingerprints) != 1 || fingerprints[0] == "" {
|
|
|
|
return fmt.Errorf("could not acquire PGP key entity")
|
|
|
|
}
|
|
|
|
fingerprint = fingerprints[0]
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unreachable condition")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.stateLock.RLock()
|
|
|
|
defer c.stateLock.RUnlock()
|
|
|
|
if c.sealed {
|
|
|
|
return ErrSealed
|
|
|
|
}
|
|
|
|
if c.standby {
|
|
|
|
return ErrStandby
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootLock.Lock()
|
|
|
|
defer c.generateRootLock.Unlock()
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
// Prevent multiple concurrent root generations
|
2016-01-15 15:55:35 +00:00
|
|
|
if c.generateRootConfig != nil {
|
2016-01-09 02:21:02 +00:00
|
|
|
return fmt.Errorf("root generation already in progress")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the configuration
|
|
|
|
generationNonce, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootConfig = &GenerateRootConfig{
|
2016-01-09 02:21:02 +00:00
|
|
|
Nonce: generationNonce,
|
|
|
|
OTP: otp,
|
|
|
|
PGPKey: pgpKey,
|
|
|
|
PGPFingerprint: fingerprint,
|
|
|
|
}
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsInfo() {
|
|
|
|
c.logger.Info("core: root generation initialized", "nonce", c.generateRootConfig.Nonce)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// GenerateRootUpdate is used to provide a new key part
|
|
|
|
func (c *Core) GenerateRootUpdate(key []byte, nonce string) (*GenerateRootResult, error) {
|
2016-01-09 02:21:02 +00:00
|
|
|
// Verify the key length
|
|
|
|
min, max := c.barrier.KeyLength()
|
|
|
|
max += shamir.ShareOverhead
|
|
|
|
if len(key) < min {
|
|
|
|
return nil, &ErrInvalidKey{fmt.Sprintf("key is shorter than minimum %d bytes", min)}
|
|
|
|
}
|
|
|
|
if len(key) > max {
|
|
|
|
return nil, &ErrInvalidKey{fmt.Sprintf("key is longer than maximum %d bytes", max)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the seal configuration
|
2016-04-04 14:44:22 +00:00
|
|
|
var config *SealConfig
|
|
|
|
var err error
|
|
|
|
if c.seal.RecoveryKeySupported() {
|
|
|
|
config, err = c.seal.RecoveryConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config, err = c.seal.BarrierConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the barrier is initialized
|
|
|
|
if config == nil {
|
|
|
|
return nil, ErrNotInit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we are already unsealed
|
|
|
|
c.stateLock.RLock()
|
|
|
|
defer c.stateLock.RUnlock()
|
|
|
|
if c.sealed {
|
|
|
|
return nil, ErrSealed
|
|
|
|
}
|
|
|
|
if c.standby {
|
|
|
|
return nil, ErrStandby
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootLock.Lock()
|
|
|
|
defer c.generateRootLock.Unlock()
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// Ensure a generateRoot is in progress
|
|
|
|
if c.generateRootConfig == nil {
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil, fmt.Errorf("no root generation in progress")
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
if nonce != c.generateRootConfig.Nonce {
|
|
|
|
return nil, fmt.Errorf("incorrect nonce supplied; nonce for this root generation operation is %s", c.generateRootConfig.Nonce)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we already have this piece
|
2016-01-15 15:55:35 +00:00
|
|
|
for _, existing := range c.generateRootProgress {
|
2016-01-09 02:21:02 +00:00
|
|
|
if bytes.Equal(existing, key) {
|
2016-09-01 21:40:01 +00:00
|
|
|
return nil, fmt.Errorf("given key has already been provided during this generation operation")
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store this key
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootProgress = append(c.generateRootProgress, key)
|
|
|
|
progress := len(c.generateRootProgress)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
// Check if we don't have enough keys to unlock
|
2016-01-15 15:55:35 +00:00
|
|
|
if len(c.generateRootProgress) < config.SecretThreshold {
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsDebug() {
|
|
|
|
c.logger.Debug("core: cannot generate root, not enough keys", "keys", progress, "threshold", config.SecretThreshold)
|
|
|
|
}
|
2016-01-15 15:55:35 +00:00
|
|
|
return &GenerateRootResult{
|
2016-01-09 02:21:02 +00:00
|
|
|
Progress: progress,
|
|
|
|
Required: config.SecretThreshold,
|
2016-01-15 15:55:35 +00:00
|
|
|
PGPFingerprint: c.generateRootConfig.PGPFingerprint,
|
2016-01-09 02:21:02 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover the master key
|
|
|
|
var masterKey []byte
|
|
|
|
if config.SecretThreshold == 1 {
|
2016-01-15 15:55:35 +00:00
|
|
|
masterKey = c.generateRootProgress[0]
|
|
|
|
c.generateRootProgress = nil
|
2016-01-09 02:21:02 +00:00
|
|
|
} else {
|
2016-01-15 15:55:35 +00:00
|
|
|
masterKey, err = shamir.Combine(c.generateRootProgress)
|
|
|
|
c.generateRootProgress = nil
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to compute master key: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the master key
|
2016-04-04 14:44:22 +00:00
|
|
|
if c.seal.RecoveryKeySupported() {
|
|
|
|
if err := c.seal.VerifyRecoveryKey(masterKey); err != nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: root generation aborted, recovery key verification failed", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := c.barrier.VerifyMaster(masterKey); err != nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: root generation aborted, master key verification failed", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:44:33 +00:00
|
|
|
te, err := c.tokenStore.rootToken()
|
|
|
|
if err != nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: root token generation failed", "error", err)
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-20 00:44:33 +00:00
|
|
|
if te == nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: got nil token entry back from root generation")
|
2016-01-20 00:44:33 +00:00
|
|
|
return nil, fmt.Errorf("got nil token entry back from root generation")
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-01-20 00:44:33 +00:00
|
|
|
uuidBytes, err := uuid.ParseUUID(te.ID)
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
2016-01-20 00:44:33 +00:00
|
|
|
c.tokenStore.Revoke(te.ID)
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: error getting generated token bytes", "error", err)
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-20 00:44:33 +00:00
|
|
|
if uuidBytes == nil {
|
|
|
|
c.tokenStore.Revoke(te.ID)
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: got nil parsed UUID bytes")
|
2016-01-20 00:44:33 +00:00
|
|
|
return nil, fmt.Errorf("got nil parsed UUID bytes")
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
var tokenBytes []byte
|
|
|
|
// Get the encoded value first so that if there is an error we don't create
|
|
|
|
// the root token.
|
|
|
|
switch {
|
2016-01-15 15:55:35 +00:00
|
|
|
case len(c.generateRootConfig.OTP) > 0:
|
2016-01-09 02:21:02 +00:00
|
|
|
// This function performs decoding checks so rather than decode the OTP,
|
|
|
|
// just encode the value we're passing in.
|
2016-01-20 00:44:33 +00:00
|
|
|
tokenBytes, err = xor.XORBase64(c.generateRootConfig.OTP, base64.StdEncoding.EncodeToString(uuidBytes))
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
2016-01-20 00:44:33 +00:00
|
|
|
c.tokenStore.Revoke(te.ID)
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: xor of root token failed", "error", err)
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
case len(c.generateRootConfig.PGPKey) > 0:
|
2016-01-20 00:44:33 +00:00
|
|
|
_, tokenBytesArr, err := pgpkeys.EncryptShares([][]byte{[]byte(te.ID)}, []string{c.generateRootConfig.PGPKey})
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
2016-01-20 00:44:33 +00:00
|
|
|
c.tokenStore.Revoke(te.ID)
|
2016-08-19 20:45:17 +00:00
|
|
|
c.logger.Error("core: error encrypting new root token", "error", err)
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tokenBytes = tokenBytesArr[0]
|
|
|
|
|
|
|
|
default:
|
2016-01-20 00:44:33 +00:00
|
|
|
c.tokenStore.Revoke(te.ID)
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil, fmt.Errorf("unreachable condition")
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
results := &GenerateRootResult{
|
2016-01-09 02:21:02 +00:00
|
|
|
Progress: progress,
|
|
|
|
Required: config.SecretThreshold,
|
|
|
|
EncodedRootToken: base64.StdEncoding.EncodeToString(tokenBytes),
|
2016-01-15 15:55:35 +00:00
|
|
|
PGPFingerprint: c.generateRootConfig.PGPFingerprint,
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsInfo() {
|
|
|
|
c.logger.Info("core: root generation finished", "nonce", c.generateRootConfig.Nonce)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootProgress = nil
|
|
|
|
c.generateRootConfig = nil
|
2016-01-09 02:21:02 +00:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
// GenerateRootCancel is used to cancel an in-progress root generation
|
|
|
|
func (c *Core) GenerateRootCancel() error {
|
2016-01-09 02:21:02 +00:00
|
|
|
c.stateLock.RLock()
|
|
|
|
defer c.stateLock.RUnlock()
|
|
|
|
if c.sealed {
|
|
|
|
return ErrSealed
|
|
|
|
}
|
|
|
|
if c.standby {
|
|
|
|
return ErrStandby
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootLock.Lock()
|
|
|
|
defer c.generateRootLock.Unlock()
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
// Clear any progress or config
|
2016-01-15 15:55:35 +00:00
|
|
|
c.generateRootConfig = nil
|
|
|
|
c.generateRootProgress = nil
|
2016-01-09 02:21:02 +00:00
|
|
|
return nil
|
|
|
|
}
|