2015-08-18 05:57:27 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2015-08-25 04:05:44 +05:30
|
|
|
"github.com/go-gorp/gorp"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/lib/pq"
|
|
|
|
|
|
|
|
pcrypto "github.com/coreos/dex/pkg/crypto"
|
|
|
|
"github.com/coreos/go-oidc/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyTableName = "key"
|
|
|
|
)
|
|
|
|
|
2015-08-26 05:11:20 +05:30
|
|
|
var (
|
|
|
|
ErrorCannotDecryptKeys = errors.New("Cannot Decrypt Keys")
|
|
|
|
)
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func init() {
|
|
|
|
register(table{
|
|
|
|
name: keyTableName,
|
|
|
|
model: privateKeySetBlob{},
|
|
|
|
autoinc: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPrivateKeySetModel(pks *key.PrivateKeySet) (*privateKeySetModel, error) {
|
|
|
|
pkeys := pks.Keys()
|
|
|
|
keys := make([]privateKeyModel, len(pkeys))
|
|
|
|
for i, pkey := range pkeys {
|
|
|
|
keys[i] = privateKeyModel{
|
|
|
|
ID: pkey.ID(),
|
|
|
|
PKCS1: x509.MarshalPKCS1PrivateKey(pkey.PrivateKey),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m := privateKeySetModel{
|
|
|
|
Keys: keys,
|
|
|
|
ExpiresAt: pks.ExpiresAt(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type privateKeyModel struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
PKCS1 []byte `json:"pkcs1"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *privateKeyModel) PrivateKey() (*key.PrivateKey, error) {
|
|
|
|
d, err := x509.ParsePKCS1PrivateKey(m.PKCS1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pk := key.PrivateKey{
|
|
|
|
KeyID: m.ID,
|
|
|
|
PrivateKey: d,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type privateKeySetModel struct {
|
|
|
|
Keys []privateKeyModel `json:"keys"`
|
|
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *privateKeySetModel) PrivateKeySet() (*key.PrivateKeySet, error) {
|
|
|
|
keys := make([]*key.PrivateKey, len(m.Keys))
|
|
|
|
for i, pkm := range m.Keys {
|
|
|
|
pk, err := pkm.PrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys[i] = pk
|
|
|
|
}
|
|
|
|
return key.NewPrivateKeySet(keys, m.ExpiresAt), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type privateKeySetBlob struct {
|
|
|
|
Value []byte `db:"value"`
|
|
|
|
}
|
|
|
|
|
2015-10-29 05:19:25 +05:30
|
|
|
func NewPrivateKeySetRepo(dbm *gorp.DbMap, useOldFormat bool, secrets ...[]byte) (*PrivateKeySetRepo, error) {
|
2015-12-17 06:41:23 +05:30
|
|
|
if len(secrets) == 0 {
|
|
|
|
return nil, errors.New("must provide at least one key secret")
|
|
|
|
}
|
2015-08-26 05:11:20 +05:30
|
|
|
for i, secret := range secrets {
|
|
|
|
if len(secret) != 32 {
|
|
|
|
return nil, fmt.Errorf("key secret %d: expected 32-byte secret", i)
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
r := &PrivateKeySetRepo{
|
2015-10-29 05:19:25 +05:30
|
|
|
dbMap: dbm,
|
|
|
|
useOldFormat: useOldFormat,
|
|
|
|
secrets: secrets,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type PrivateKeySetRepo struct {
|
2015-10-29 05:19:25 +05:30
|
|
|
dbMap *gorp.DbMap
|
|
|
|
useOldFormat bool
|
|
|
|
secrets [][]byte
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PrivateKeySetRepo) Set(ks key.KeySet) error {
|
|
|
|
qt := pq.QuoteIdentifier(keyTableName)
|
|
|
|
_, err := r.dbMap.Exec(fmt.Sprintf("DELETE FROM %s", qt))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pks, ok := ks.(*key.PrivateKeySet)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("unable to cast to PrivateKeySet")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := newPrivateKeySetModel(pks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-29 05:19:25 +05:30
|
|
|
var v []byte
|
|
|
|
|
|
|
|
if r.useOldFormat {
|
|
|
|
v, err = pcrypto.AESEncrypt(j, r.active())
|
|
|
|
} else {
|
|
|
|
v, err = pcrypto.Encrypt(j, r.active())
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &privateKeySetBlob{Value: v}
|
|
|
|
return r.dbMap.Insert(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PrivateKeySetRepo) Get() (key.KeySet, error) {
|
|
|
|
qt := pq.QuoteIdentifier(keyTableName)
|
|
|
|
objs, err := r.dbMap.Select(&privateKeySetBlob{}, fmt.Sprintf("SELECT * FROM %s", qt))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(objs) == 0 {
|
|
|
|
return nil, key.ErrorNoKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
b, ok := objs[0].(*privateKeySetBlob)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("unable to cast to KeySet")
|
|
|
|
}
|
|
|
|
|
2015-08-26 05:11:20 +05:30
|
|
|
var pks *key.PrivateKeySet
|
|
|
|
for _, secret := range r.secrets {
|
|
|
|
var j []byte
|
2015-10-29 05:19:25 +05:30
|
|
|
|
|
|
|
if r.useOldFormat {
|
|
|
|
j, err = pcrypto.AESDecrypt(b.Value, secret)
|
|
|
|
} else {
|
|
|
|
j, err = pcrypto.Decrypt(b.Value, secret)
|
|
|
|
}
|
|
|
|
|
2015-08-26 05:11:20 +05:30
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2015-08-26 05:11:20 +05:30
|
|
|
var m privateKeySetModel
|
|
|
|
if err = json.Unmarshal(j, &m); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pks, err = m.PrivateKeySet()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2015-08-26 05:11:20 +05:30
|
|
|
return nil, ErrorCannotDecryptKeys
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
return key.KeySet(pks), nil
|
|
|
|
}
|
2015-08-26 05:11:20 +05:30
|
|
|
|
|
|
|
func (r *PrivateKeySetRepo) active() []byte {
|
|
|
|
return r.secrets[0]
|
|
|
|
}
|