Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 1 | package security |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ecdsa" |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 5 | "fmt" |
| 6 | "os" |
| 7 | "path" |
| 8 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 9 | "v.io/core/veyron2/security" |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame] | 12 | const ( |
| 13 | blessingStoreDataFile = "blessingstore.data" |
| 14 | blessingStoreSigFile = "blessingstore.sig" |
| 15 | |
| 16 | blessingRootsDataFile = "blessingroots.data" |
| 17 | blessingRootsSigFile = "blessingroots.sig" |
| 18 | |
| 19 | privateKeyFile = "privatekey.pem" |
| 20 | ) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 21 | |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 22 | // NewPrincipal mints a new private key and generates a principal based on |
| 23 | // this key, storing its BlessingRoots and BlessingStore in memory. |
| 24 | func NewPrincipal() (security.Principal, error) { |
gauthamt | a134eda | 2014-11-05 17:57:42 -0800 | [diff] [blame] | 25 | pub, priv, err := NewPrincipalKey() |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | return security.CreatePrincipal(security.NewInMemoryECDSASigner(priv), newInMemoryBlessingStore(pub), newInMemoryBlessingRoots()) |
| 30 | } |
| 31 | |
gauthamt | 9810816 | 2014-11-11 18:45:56 -0800 | [diff] [blame] | 32 | // PrincipalStateSerializer is used to persist BlessingRoots/BlessingStore state for |
| 33 | // a principal with the provided SerializerReaderWriters. |
| 34 | type PrincipalStateSerializer struct { |
| 35 | BlessingRoots SerializerReaderWriter |
| 36 | BlessingStore SerializerReaderWriter |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 37 | } |
| 38 | |
gauthamt | 9810816 | 2014-11-11 18:45:56 -0800 | [diff] [blame] | 39 | // NewPrincipalStateSerializer is a convenience function that returns a serializer |
| 40 | // for BlessingStore and BlessingRoots given a directory location. We create the |
| 41 | // directory if it does not already exist. |
| 42 | func NewPrincipalStateSerializer(dir string) (*PrincipalStateSerializer, error) { |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 43 | if err := mkDir(dir); err != nil { |
| 44 | return nil, err |
| 45 | } |
gauthamt | 9810816 | 2014-11-11 18:45:56 -0800 | [diff] [blame] | 46 | return &PrincipalStateSerializer{ |
Ankur | 27c56fd | 2014-11-17 19:30:34 -0800 | [diff] [blame] | 47 | BlessingRoots: NewFileSerializer(path.Join(dir, blessingRootsDataFile), path.Join(dir, blessingRootsSigFile)), |
| 48 | BlessingStore: NewFileSerializer(path.Join(dir, blessingStoreDataFile), path.Join(dir, blessingStoreSigFile)), |
gauthamt | 9810816 | 2014-11-11 18:45:56 -0800 | [diff] [blame] | 49 | }, nil |
| 50 | } |
| 51 | |
| 52 | // NewPrincipalFromSigner creates a new principal using the provided Signer. If previously |
| 53 | // persisted state is available, we use the serializers to populate BlessingRoots/BlessingStore |
| 54 | // for the Principal. If provided, changes to the state are persisted and committed with the |
| 55 | // same serializers. Otherwise, the state (ie: BlessingStore, BlessingRoots) is kept in memory. |
| 56 | func NewPrincipalFromSigner(signer security.Signer, state *PrincipalStateSerializer) (security.Principal, error) { |
| 57 | if state == nil { |
| 58 | return security.CreatePrincipal(signer, newInMemoryBlessingStore(signer.PublicKey()), newInMemoryBlessingRoots()) |
| 59 | } |
| 60 | serializationSigner, err := security.CreatePrincipal(signer, nil, nil) |
| 61 | if err != nil { |
| 62 | return nil, fmt.Errorf("failed to create serialization.Signer: %v", err) |
| 63 | } |
| 64 | blessingRoots, err := newPersistingBlessingRoots(state.BlessingRoots, serializationSigner) |
| 65 | if err != nil { |
| 66 | return nil, fmt.Errorf("failed to load BlessingRoots: %v", err) |
| 67 | } |
| 68 | blessingStore, err := newPersistingBlessingStore(state.BlessingStore, serializationSigner) |
| 69 | if err != nil { |
| 70 | return nil, fmt.Errorf("failed to load BlessingStore: %v", err) |
| 71 | } |
| 72 | return security.CreatePrincipal(signer, blessingStore, blessingRoots) |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 75 | // LoadPersistentPrincipal reads state for a principal (private key, BlessingRoots, BlessingStore) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 76 | // from the provided directory 'dir' and commits all state changes to the same directory. |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 77 | // If private key file does not exist then an error 'err' is returned such that os.IsNotExist(err) is true. |
Suharsh Sivakumar | 4684f4e | 2014-10-24 13:42:06 -0700 | [diff] [blame] | 78 | // If private key file exists then 'passphrase' must be correct, otherwise PassphraseErr will be returned. |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 79 | func LoadPersistentPrincipal(dir string, passphrase []byte) (security.Principal, error) { |
| 80 | key, err := loadKeyFromDir(dir, passphrase) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
gauthamt | 9810816 | 2014-11-11 18:45:56 -0800 | [diff] [blame] | 84 | state, err := NewPrincipalStateSerializer(dir) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | return NewPrincipalFromSigner(security.NewInMemoryECDSASigner(key), state) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 91 | // CreatePersistentPrincipal creates a new principal (private key, BlessingRoots, |
| 92 | // BlessingStore) and commits all state changes to the provided directory. |
| 93 | // |
| 94 | // The generated private key is serialized and saved encrypted if the 'passphrase' |
| 95 | // is non-nil, and unencrypted otherwise. |
| 96 | // |
| 97 | // If the directory has any preexisting principal data, CreatePersistentPrincipal |
| 98 | // will return an error. |
| 99 | // |
| 100 | // The specified directory may not exist, in which case it gets created by this |
| 101 | // function. |
| 102 | func CreatePersistentPrincipal(dir string, passphrase []byte) (principal security.Principal, err error) { |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 103 | if err := mkDir(dir); err != nil { |
| 104 | return nil, err |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 105 | } |
Suharsh Sivakumar | 4e09188 | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 106 | key, err := initKey(dir, passphrase) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 107 | if err != nil { |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 108 | return nil, fmt.Errorf("failed to initialize private key: %v", err) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 109 | } |
gauthamt | 9810816 | 2014-11-11 18:45:56 -0800 | [diff] [blame] | 110 | state, err := NewPrincipalStateSerializer(dir) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | return NewPrincipalFromSigner(security.NewInMemoryECDSASigner(key), state) |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 117 | // SetDefaultBlessings sets the provided blessings as default and shareable with |
| 118 | // all peers on provided principal's BlessingStore, and also adds it as a root to |
| 119 | // the principal's BlessingRoots. |
| 120 | func SetDefaultBlessings(p security.Principal, blessings security.Blessings) error { |
| 121 | if err := p.BlessingStore().SetDefault(blessings); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | if _, err := p.BlessingStore().Set(blessings, security.AllPrincipals); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | if err := p.AddToRoots(blessings); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | return nil |
| 131 | } |
| 132 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 133 | // InitDefaultBlessings uses the provided principal to create a self blessing for name 'name', |
| 134 | // sets it as default on the principal's BlessingStore and adds it as root to the principal's BlessingRoots. |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 135 | // TODO(ataly): Get rid this function given that we have SetDefaultBlessings. |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 136 | func InitDefaultBlessings(p security.Principal, name string) error { |
| 137 | blessing, err := p.BlessSelf(name) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 141 | return SetDefaultBlessings(p, blessing) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 144 | func mkDir(dir string) error { |
| 145 | if finfo, err := os.Stat(dir); err == nil { |
| 146 | if !finfo.IsDir() { |
| 147 | return fmt.Errorf("%q is not a directory", dir) |
| 148 | } |
| 149 | } else if err := os.MkdirAll(dir, 0700); err != nil { |
| 150 | return fmt.Errorf("failed to create %q: %v", dir, err) |
| 151 | } |
| 152 | return nil |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | func loadKeyFromDir(dir string, passphrase []byte) (*ecdsa.PrivateKey, error) { |
| 156 | keyFile := path.Join(dir, privateKeyFile) |
| 157 | f, err := os.Open(keyFile) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | defer f.Close() |
Ankur | 73e7a93 | 2014-10-24 15:57:03 -0700 | [diff] [blame] | 162 | key, err := LoadPEMKey(f, passphrase) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | return key.(*ecdsa.PrivateKey), nil |
| 167 | } |
| 168 | |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 169 | func initKey(dir string, passphrase []byte) (*ecdsa.PrivateKey, error) { |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 170 | keyFile := path.Join(dir, privateKeyFile) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 171 | f, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 172 | if err != nil { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 173 | return nil, fmt.Errorf("failed to open %q for writing: %v", keyFile, err) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 174 | } |
| 175 | defer f.Close() |
gauthamt | a134eda | 2014-11-05 17:57:42 -0800 | [diff] [blame] | 176 | _, key, err := NewPrincipalKey() |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 177 | if err != nil { |
| 178 | return nil, fmt.Errorf("failed to generate private key: %v", err) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 179 | } |
Ankur | 73e7a93 | 2014-10-24 15:57:03 -0700 | [diff] [blame] | 180 | if err := SavePEMKey(f, key, passphrase); err != nil { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 181 | return nil, fmt.Errorf("failed to save private key to %q: %v", keyFile, err) |
| 182 | } |
| 183 | return key, nil |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 184 | } |