2021-11-25 01:56:58 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
2021-11-25 01:56:58 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SchemaEncryptionChangeKey uses the currently configured key to decrypt values in the database and the key provided
|
|
|
|
// by this command to encrypt the values again and update them using a transaction.
|
|
|
|
func (p *SQLProvider) SchemaEncryptionChangeKey(ctx context.Context, encryptionKey string) (err error) {
|
|
|
|
tx, err := p.db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error beginning transaction to change encryption key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := sha256.Sum256([]byte(encryptionKey))
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
if err = p.schemaEncryptionChangeKeyTOTP(ctx, tx, key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
if err = p.schemaEncryptionChangeKeyWebauthn(ctx, tx, key); err != nil {
|
2021-12-03 00:04:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = p.setNewEncryptionCheckValue(ctx, &key, tx); err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error %v: rollback due to error: %w", rollbackErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("rollback due to error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SQLProvider) schemaEncryptionChangeKeyTOTP(ctx context.Context, tx *sqlx.Tx, key [32]byte) (err error) {
|
2022-03-06 05:47:40 +00:00
|
|
|
var configs []model.TOTPConfiguration
|
2021-11-25 01:56:58 +00:00
|
|
|
|
|
|
|
for page := 0; true; page++ {
|
|
|
|
if configs, err = p.LoadTOTPConfigurations(ctx, 10, page); err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error %v: rollback due to error: %w", rollbackErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("rollback due to error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, config := range configs {
|
|
|
|
if config.Secret, err = utils.Encrypt(config.Secret, &key); err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error %v: rollback due to error: %w", rollbackErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("rollback due to error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
if err = p.updateTOTPConfigurationSecret(ctx, config); err != nil {
|
2021-11-25 01:56:58 +00:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error %v: rollback due to error: %w", rollbackErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("rollback due to error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configs) != 10 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
func (p *SQLProvider) schemaEncryptionChangeKeyWebauthn(ctx context.Context, tx *sqlx.Tx, key [32]byte) (err error) {
|
2022-03-06 05:47:40 +00:00
|
|
|
var devices []model.WebauthnDevice
|
2021-12-03 00:04:11 +00:00
|
|
|
|
|
|
|
for page := 0; true; page++ {
|
2022-03-03 11:20:43 +00:00
|
|
|
if devices, err = p.LoadWebauthnDevices(ctx, 10, page); err != nil {
|
2021-12-03 00:04:11 +00:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error %v: rollback due to error: %w", rollbackErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("rollback due to error: %w", err)
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
for _, device := range devices {
|
|
|
|
if device.PublicKey, err = utils.Encrypt(device.PublicKey, &key); err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error %v: rollback due to error: %w", rollbackErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("rollback due to error: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
if err = p.updateWebauthnDevicePublicKey(ctx, device); err != nil {
|
2021-12-03 00:04:11 +00:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error %v: rollback due to error: %w", rollbackErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("rollback due to error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(devices) != 10 {
|
|
|
|
break
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
return nil
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SchemaEncryptionCheckKey checks the encryption key configured is valid for the database.
|
|
|
|
func (p *SQLProvider) SchemaEncryptionCheckKey(ctx context.Context, verbose bool) (err error) {
|
|
|
|
version, err := p.SchemaVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if version < 1 {
|
|
|
|
return ErrSchemaEncryptionVersionUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if _, err = p.getEncryptionValue(ctx, encryptionNameCheck); err != nil {
|
|
|
|
errs = append(errs, ErrSchemaEncryptionInvalidKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
2021-12-03 00:04:11 +00:00
|
|
|
if err = p.schemaEncryptionCheckTOTP(ctx); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2022-09-03 01:51:02 +00:00
|
|
|
if err = p.schemaEncryptionCheckWebauthn(ctx); err != nil {
|
2021-12-03 00:04:11 +00:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
for i, e := range errs {
|
|
|
|
if i == 0 {
|
|
|
|
err = e
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
continue
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
err = fmt.Errorf("%w, %v", err, e)
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
func (p *SQLProvider) schemaEncryptionCheckTOTP(ctx context.Context) (err error) {
|
|
|
|
var (
|
2022-03-06 05:47:40 +00:00
|
|
|
config model.TOTPConfiguration
|
2021-12-03 00:04:11 +00:00
|
|
|
row int
|
|
|
|
invalid int
|
|
|
|
total int
|
|
|
|
)
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
pageSize := 10
|
|
|
|
|
|
|
|
var rows *sqlx.Rows
|
|
|
|
|
|
|
|
for page := 0; true; page++ {
|
|
|
|
if rows, err = p.db.QueryxContext(ctx, p.sqlSelectTOTPConfigs, pageSize, pageSize*page); err != nil {
|
2021-11-25 01:56:58 +00:00
|
|
|
_ = rows.Close()
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
return fmt.Errorf("error selecting TOTP configurations: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
row = 0
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
total++
|
|
|
|
row++
|
|
|
|
|
|
|
|
if err = rows.StructScan(&config); err != nil {
|
|
|
|
_ = rows.Close()
|
|
|
|
return fmt.Errorf("error scanning TOTP configuration to struct: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = p.decrypt(config.Secret); err != nil {
|
|
|
|
invalid++
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
_ = rows.Close()
|
|
|
|
|
|
|
|
if row < pageSize {
|
|
|
|
break
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
if invalid != 0 {
|
|
|
|
return fmt.Errorf("%d of %d total TOTP secrets were invalid", invalid, total)
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-03 01:51:02 +00:00
|
|
|
func (p *SQLProvider) schemaEncryptionCheckWebauthn(ctx context.Context) (err error) {
|
2021-12-03 00:04:11 +00:00
|
|
|
var (
|
2022-09-03 01:51:02 +00:00
|
|
|
device model.WebauthnDevice
|
2021-12-03 00:04:11 +00:00
|
|
|
row int
|
|
|
|
invalid int
|
|
|
|
total int
|
|
|
|
)
|
|
|
|
|
|
|
|
pageSize := 10
|
|
|
|
|
|
|
|
var rows *sqlx.Rows
|
|
|
|
|
|
|
|
for page := 0; true; page++ {
|
2022-03-03 11:20:43 +00:00
|
|
|
if rows, err = p.db.QueryxContext(ctx, p.sqlSelectWebauthnDevices, pageSize, pageSize*page); err != nil {
|
2021-12-03 00:04:11 +00:00
|
|
|
_ = rows.Close()
|
|
|
|
|
2022-09-03 01:51:02 +00:00
|
|
|
return fmt.Errorf("error selecting Webauthn devices: %w", err)
|
2021-12-03 00:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
row = 0
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
total++
|
|
|
|
row++
|
|
|
|
|
|
|
|
if err = rows.StructScan(&device); err != nil {
|
|
|
|
_ = rows.Close()
|
2022-09-03 01:51:02 +00:00
|
|
|
return fmt.Errorf("error scanning Webauthn device to struct: %w", err)
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
if _, err = p.decrypt(device.PublicKey); err != nil {
|
|
|
|
invalid++
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
_ = rows.Close()
|
|
|
|
|
|
|
|
if row < pageSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if invalid != 0 {
|
2022-09-03 01:51:02 +00:00
|
|
|
return fmt.Errorf("%d of %d total Webauthn devices were invalid", invalid, total)
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-03 01:51:02 +00:00
|
|
|
func (p *SQLProvider) encrypt(clearText []byte) (cipherText []byte, err error) {
|
2021-11-25 01:56:58 +00:00
|
|
|
return utils.Encrypt(clearText, &p.key)
|
|
|
|
}
|
|
|
|
|
2022-09-03 01:51:02 +00:00
|
|
|
func (p *SQLProvider) decrypt(cipherText []byte) (clearText []byte, err error) {
|
2021-11-25 01:56:58 +00:00
|
|
|
return utils.Decrypt(cipherText, &p.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SQLProvider) getEncryptionValue(ctx context.Context, name string) (value []byte, err error) {
|
|
|
|
var encryptedValue []byte
|
|
|
|
|
|
|
|
err = p.db.GetContext(ctx, &encryptedValue, p.sqlSelectEncryptionValue, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.decrypt(encryptedValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SQLProvider) setNewEncryptionCheckValue(ctx context.Context, key *[32]byte, e sqlx.ExecerContext) (err error) {
|
2022-03-16 00:29:46 +00:00
|
|
|
valueClearText, err := uuid.NewRandom()
|
2021-12-04 04:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
|
|
|
value, err := utils.Encrypt([]byte(valueClearText.String()), key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
_, err = e.ExecContext(ctx, p.sqlUpsertEncryptionValue, encryptionNameCheck, value)
|
|
|
|
} else {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpsertEncryptionValue, encryptionNameCheck, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|