blob: 7247434d9ea20488e341f26003ca59324ad8df20 [file] [log] [blame]
Asim Shankarae8d4c52014-10-08 13:03:31 -07001package security
2
3import (
4 "crypto/ecdsa"
Asim Shankarae8d4c52014-10-08 13:03:31 -07005 "fmt"
6 "os"
7 "path"
8
9 "veyron.io/veyron/veyron2/security"
10)
11
gauthamt1e313bc2014-11-10 15:45:56 -080012const (
13 blessingStoreDataFile = "blessingstore.data"
14 blessingStoreSigFile = "blessingstore.sig"
15
16 blessingRootsDataFile = "blessingroots.data"
17 blessingRootsSigFile = "blessingroots.sig"
18
19 privateKeyFile = "privatekey.pem"
20)
Asim Shankarae8d4c52014-10-08 13:03:31 -070021
Asim Shankarae8d4c52014-10-08 13:03:31 -070022// NewPrincipal mints a new private key and generates a principal based on
23// this key, storing its BlessingRoots and BlessingStore in memory.
24func NewPrincipal() (security.Principal, error) {
gauthamta134eda2014-11-05 17:57:42 -080025 pub, priv, err := NewPrincipalKey()
Asim Shankarae8d4c52014-10-08 13:03:31 -070026 if err != nil {
27 return nil, err
28 }
29 return security.CreatePrincipal(security.NewInMemoryECDSASigner(priv), newInMemoryBlessingStore(pub), newInMemoryBlessingRoots())
30}
31
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -070032// NewPrincipalFromSigner creates a principal using the provided signer, storing its
33// BlessingRoots and BlessingStore in memory.
34func NewPrincipalFromSigner(signer security.Signer) (security.Principal, error) {
35 return security.CreatePrincipal(signer, newInMemoryBlessingStore(signer.PublicKey()), newInMemoryBlessingRoots())
36}
37
gauthamt1e313bc2014-11-10 15:45:56 -080038// NewPersistentPrincipalFromSigner creates a new principal using the provided Signer and a
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -070039// partial state (i.e., BlessingRoots, BlessingStore) that is read from the provided directory 'dir'.
40// Changes to the partial state are persisted and commited to the same directory; the provided
41// signer isn't persisted: the caller is expected to persist it separately or use the
42// {Load,Create}PersistentPrincipal() methods instead.
43//
44// If the directory does not contain any partial state, new partial state instances are created
45// and subsequently commited to 'dir'. If the directory does not exist, it is created.
46func NewPersistentPrincipalFromSigner(signer security.Signer, dir string) (security.Principal, error) {
47 if err := mkDir(dir); err != nil {
48 return nil, err
49 }
50 return newPersistentPrincipalFromSigner(signer, dir)
51}
52
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070053// LoadPersistentPrincipal reads state for a principal (private key, BlessingRoots, BlessingStore)
Asim Shankarae8d4c52014-10-08 13:03:31 -070054// from the provided directory 'dir' and commits all state changes to the same directory.
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070055// If private key file does not exist then an error 'err' is returned such that os.IsNotExist(err) is true.
Suharsh Sivakumar4684f4e2014-10-24 13:42:06 -070056// If private key file exists then 'passphrase' must be correct, otherwise PassphraseErr will be returned.
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070057func LoadPersistentPrincipal(dir string, passphrase []byte) (security.Principal, error) {
58 key, err := loadKeyFromDir(dir, passphrase)
59 if err != nil {
60 return nil, err
61 }
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -070062 return newPersistentPrincipalFromSigner(security.NewInMemoryECDSASigner(key), dir)
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070063}
64
Ankur4704f5f2014-10-23 12:40:54 -070065// CreatePersistentPrincipal creates a new principal (private key, BlessingRoots,
66// BlessingStore) and commits all state changes to the provided directory.
67//
68// The generated private key is serialized and saved encrypted if the 'passphrase'
69// is non-nil, and unencrypted otherwise.
70//
71// If the directory has any preexisting principal data, CreatePersistentPrincipal
72// will return an error.
73//
74// The specified directory may not exist, in which case it gets created by this
75// function.
76func CreatePersistentPrincipal(dir string, passphrase []byte) (principal security.Principal, err error) {
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -070077 if err := mkDir(dir); err != nil {
78 return nil, err
Asim Shankarae8d4c52014-10-08 13:03:31 -070079 }
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070080 key, err := initKey(dir, nil)
Asim Shankarae8d4c52014-10-08 13:03:31 -070081 if err != nil {
Ankur4704f5f2014-10-23 12:40:54 -070082 return nil, fmt.Errorf("failed to initialize private key: %v", err)
Asim Shankarae8d4c52014-10-08 13:03:31 -070083 }
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -070084 return newPersistentPrincipalFromSigner(security.NewInMemoryECDSASigner(key), dir)
85}
86
Suharsh Sivakumar8a7fba42014-10-27 12:40:48 -070087// InitDefaultBlessings uses the provided principal to create a self blessing for name 'name',
88// sets it as default on the principal's BlessingStore and adds it as root to the principal's BlessingRoots.
89func InitDefaultBlessings(p security.Principal, name string) error {
90 blessing, err := p.BlessSelf(name)
91 if err != nil {
92 return err
93 }
94 if err := p.BlessingStore().SetDefault(blessing); err != nil {
95 return err
96 }
97 if _, err := p.BlessingStore().Set(blessing, security.AllPrincipals); err != nil {
98 return err
99 }
100 if err := p.AddToRoots(blessing); err != nil {
101 return err
102 }
103 return nil
104}
105
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -0700106func newPersistentPrincipalFromSigner(signer security.Signer, dir string) (security.Principal, error) {
107 serializationSigner, err := security.CreatePrincipal(signer, nil, nil)
108 if err != nil {
109 return nil, fmt.Errorf("failed to create serialization.Signer: %v", err)
110 }
gauthamt1e313bc2014-11-10 15:45:56 -0800111 dataFile := path.Join(dir, blessingRootsDataFile)
112 signatureFile := path.Join(dir, blessingRootsSigFile)
113 fs, err := NewFileSerializer(dataFile, signatureFile)
114 if err != nil {
115 return nil, err
116 }
117 roots, err := newPersistingBlessingRoots(fs, serializationSigner)
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -0700118 if err != nil {
119 return nil, fmt.Errorf("failed to load BlessingRoots from %q: %v", dir, err)
120 }
gauthamt1e313bc2014-11-10 15:45:56 -0800121 dataFile = path.Join(dir, blessingStoreDataFile)
122 signatureFile = path.Join(dir, blessingStoreSigFile)
123 if fs, err = NewFileSerializer(dataFile, signatureFile); err != nil {
124 return nil, err
125 }
126 store, err := newPersistingBlessingStore(fs, serializationSigner)
Srdjan Petrovicf07f4a02014-10-22 16:31:06 -0700127 if err != nil {
128 return nil, fmt.Errorf("failed to load BlessingStore from %q: %v", dir, err)
129 }
130 return security.CreatePrincipal(signer, store, roots)
131}
132
133func mkDir(dir string) error {
134 if finfo, err := os.Stat(dir); err == nil {
135 if !finfo.IsDir() {
136 return fmt.Errorf("%q is not a directory", dir)
137 }
138 } else if err := os.MkdirAll(dir, 0700); err != nil {
139 return fmt.Errorf("failed to create %q: %v", dir, err)
140 }
141 return nil
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700142}
143
144func loadKeyFromDir(dir string, passphrase []byte) (*ecdsa.PrivateKey, error) {
145 keyFile := path.Join(dir, privateKeyFile)
146 f, err := os.Open(keyFile)
147 if err != nil {
148 return nil, err
149 }
150 defer f.Close()
Ankur73e7a932014-10-24 15:57:03 -0700151 key, err := LoadPEMKey(f, passphrase)
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700152 if err != nil {
153 return nil, err
154 }
155 return key.(*ecdsa.PrivateKey), nil
156}
157
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700158func initKey(dir string, passphrase []byte) (*ecdsa.PrivateKey, error) {
Asim Shankarae8d4c52014-10-08 13:03:31 -0700159 keyFile := path.Join(dir, privateKeyFile)
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700160 f, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
Asim Shankarae8d4c52014-10-08 13:03:31 -0700161 if err != nil {
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700162 return nil, fmt.Errorf("failed to open %q for writing: %v", keyFile, err)
Asim Shankarae8d4c52014-10-08 13:03:31 -0700163 }
164 defer f.Close()
gauthamta134eda2014-11-05 17:57:42 -0800165 _, key, err := NewPrincipalKey()
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700166 if err != nil {
167 return nil, fmt.Errorf("failed to generate private key: %v", err)
Asim Shankarae8d4c52014-10-08 13:03:31 -0700168 }
Ankur73e7a932014-10-24 15:57:03 -0700169 if err := SavePEMKey(f, key, passphrase); err != nil {
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700170 return nil, fmt.Errorf("failed to save private key to %q: %v", keyFile, err)
171 }
172 return key, nil
Asim Shankarae8d4c52014-10-08 13:03:31 -0700173}