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 | |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 32 | // NewPrincipalFromSigner creates a principal using the provided signer, storing its |
| 33 | // BlessingRoots and BlessingStore in memory. |
| 34 | func NewPrincipalFromSigner(signer security.Signer) (security.Principal, error) { |
| 35 | return security.CreatePrincipal(signer, newInMemoryBlessingStore(signer.PublicKey()), newInMemoryBlessingRoots()) |
| 36 | } |
| 37 | |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame^] | 38 | // NewPersistentPrincipalFromSigner creates a new principal using the provided Signer and a |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 39 | // 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. |
| 46 | func 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 Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 53 | // LoadPersistentPrincipal reads state for a principal (private key, BlessingRoots, BlessingStore) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 54 | // 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] | 55 | // 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] | 56 | // 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] | 57 | func LoadPersistentPrincipal(dir string, passphrase []byte) (security.Principal, error) { |
| 58 | key, err := loadKeyFromDir(dir, passphrase) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 62 | return newPersistentPrincipalFromSigner(security.NewInMemoryECDSASigner(key), dir) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 65 | // 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. |
| 76 | func CreatePersistentPrincipal(dir string, passphrase []byte) (principal security.Principal, err error) { |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 77 | if err := mkDir(dir); err != nil { |
| 78 | return nil, err |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 79 | } |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 80 | key, err := initKey(dir, nil) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 81 | if err != nil { |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 82 | return nil, fmt.Errorf("failed to initialize private key: %v", err) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 83 | } |
Srdjan Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 84 | return newPersistentPrincipalFromSigner(security.NewInMemoryECDSASigner(key), dir) |
| 85 | } |
| 86 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 87 | // 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. |
| 89 | func 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 Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 106 | func 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 | } |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame^] | 111 | 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 Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 118 | if err != nil { |
| 119 | return nil, fmt.Errorf("failed to load BlessingRoots from %q: %v", dir, err) |
| 120 | } |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame^] | 121 | 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 Petrovic | f07f4a0 | 2014-10-22 16:31:06 -0700 | [diff] [blame] | 127 | 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 | |
| 133 | func 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 Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | func 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() |
Ankur | 73e7a93 | 2014-10-24 15:57:03 -0700 | [diff] [blame] | 151 | key, err := LoadPEMKey(f, passphrase) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 152 | if err != nil { |
| 153 | return nil, err |
| 154 | } |
| 155 | return key.(*ecdsa.PrivateKey), nil |
| 156 | } |
| 157 | |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 158 | func initKey(dir string, passphrase []byte) (*ecdsa.PrivateKey, error) { |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 159 | keyFile := path.Join(dir, privateKeyFile) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 160 | 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] | 161 | if err != nil { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 162 | 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] | 163 | } |
| 164 | defer f.Close() |
gauthamt | a134eda | 2014-11-05 17:57:42 -0800 | [diff] [blame] | 165 | _, key, err := NewPrincipalKey() |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 166 | if err != nil { |
| 167 | return nil, fmt.Errorf("failed to generate private key: %v", err) |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 168 | } |
Ankur | 73e7a93 | 2014-10-24 15:57:03 -0700 | [diff] [blame] | 169 | if err := SavePEMKey(f, key, passphrase); err != nil { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 170 | return nil, fmt.Errorf("failed to save private key to %q: %v", keyFile, err) |
| 171 | } |
| 172 | return key, nil |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 173 | } |