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 | |
| 9 | "veyron.io/veyron/veyron2/security" |
| 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 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 117 | // InitDefaultBlessings uses the provided principal to create a self blessing for name 'name', |
| 118 | // sets it as default on the principal's BlessingStore and adds it as root to the principal's BlessingRoots. |
| 119 | func InitDefaultBlessings(p security.Principal, name string) error { |
| 120 | blessing, err := p.BlessSelf(name) |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | if err := p.BlessingStore().SetDefault(blessing); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | if _, err := p.BlessingStore().Set(blessing, security.AllPrincipals); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | if err := p.AddToRoots(blessing); err != nil { |
| 131 | return err |
| 132 | } |
| 133 | return nil |
| 134 | } |
| 135 | |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 136 | func mkDir(dir string) error { |
| 137 | if finfo, err := os.Stat(dir); err == nil { |
| 138 | if !finfo.IsDir() { |
| 139 | return fmt.Errorf("%q is not a directory", dir) |
| 140 | } |
| 141 | } else if err := os.MkdirAll(dir, 0700); err != nil { |
| 142 | return fmt.Errorf("failed to create %q: %v", dir, err) |
| 143 | } |
| 144 | return nil |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | func loadKeyFromDir(dir string, passphrase []byte) (*ecdsa.PrivateKey, error) { |
| 148 | keyFile := path.Join(dir, privateKeyFile) |
| 149 | f, err := os.Open(keyFile) |
| 150 | if err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | defer f.Close() |
Ankur | 73e7a93 | 2014-10-24 15:57:03 -0700 | [diff] [blame] | 154 | key, err := LoadPEMKey(f, passphrase) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 155 | if err != nil { |
| 156 | return nil, err |
| 157 | } |
| 158 | return key.(*ecdsa.PrivateKey), nil |
| 159 | } |
| 160 | |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 161 | func initKey(dir string, passphrase []byte) (*ecdsa.PrivateKey, error) { |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 162 | keyFile := path.Join(dir, privateKeyFile) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 163 | 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] | 164 | if err != nil { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 165 | 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] | 166 | } |
| 167 | defer f.Close() |
gauthamt | a134eda | 2014-11-05 17:57:42 -0800 | [diff] [blame] | 168 | _, key, err := NewPrincipalKey() |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 169 | if err != nil { |
| 170 | return nil, fmt.Errorf("failed to generate private key: %v", err) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 171 | } |
Ankur | 73e7a93 | 2014-10-24 15:57:03 -0700 | [diff] [blame] | 172 | if err := SavePEMKey(f, key, passphrase); err != nil { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 173 | return nil, fmt.Errorf("failed to save private key to %q: %v", keyFile, err) |
| 174 | } |
| 175 | return key, nil |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 176 | } |