Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 1 | package security |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame] | 4 | "fmt" |
| 5 | "io" |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 6 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 7 | "veyron.io/veyron/veyron/security/serialization" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 8 | "veyron.io/veyron/veyron2/security" |
| 9 | "veyron.io/veyron/veyron2/vom" |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame] | 12 | func encodeAndStore(obj interface{}, data, signature io.WriteCloser, signer serialization.Signer) error { |
| 13 | if data == nil || signature == nil { |
| 14 | return fmt.Errorf("invalid data/signature handles data:%v sig:%v", data, signature) |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 15 | } |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame] | 16 | swc, err := serialization.NewSigningWriteCloser(data, signature, signer, nil) |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 17 | if err != nil { |
| 18 | return err |
| 19 | } |
| 20 | if err := vom.NewEncoder(swc).Encode(obj); err != nil { |
| 21 | swc.Close() |
| 22 | return err |
| 23 | } |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame] | 24 | return swc.Close() |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 25 | } |
| 26 | |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame] | 27 | func decodeFromStorage(obj interface{}, data, signature io.ReadCloser, publicKey security.PublicKey) error { |
| 28 | if data == nil || signature == nil { |
| 29 | return fmt.Errorf("invalid data/signature handles data:%v sig:%v", data, signature) |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 30 | } |
gauthamt | 1e313bc | 2014-11-10 15:45:56 -0800 | [diff] [blame] | 31 | defer data.Close() |
| 32 | defer signature.Close() |
| 33 | vr, err := serialization.NewVerifyingReader(data, signature, publicKey) |
Ankur | 100eb27 | 2014-09-15 16:48:12 -0700 | [diff] [blame] | 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | return vom.NewDecoder(vr).Decode(obj) |
| 38 | } |