blob: 2fdd8cd6d1630af5ab4e67bbece220afefd5f2e6 [file] [log] [blame]
Asim Shankarae8d4c52014-10-08 13:03:31 -07001package security
Ankur100eb272014-09-15 16:48:12 -07002
3import (
gauthamt1e313bc2014-11-10 15:45:56 -08004 "fmt"
5 "io"
Ankur100eb272014-09-15 16:48:12 -07006
Jiri Simsa519c5072014-09-17 21:37:57 -07007 "veyron.io/veyron/veyron/security/serialization"
Jiri Simsa519c5072014-09-17 21:37:57 -07008 "veyron.io/veyron/veyron2/security"
9 "veyron.io/veyron/veyron2/vom"
Ankur100eb272014-09-15 16:48:12 -070010)
11
gauthamt1e313bc2014-11-10 15:45:56 -080012func 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)
Ankur100eb272014-09-15 16:48:12 -070015 }
gauthamt1e313bc2014-11-10 15:45:56 -080016 swc, err := serialization.NewSigningWriteCloser(data, signature, signer, nil)
Ankur100eb272014-09-15 16:48:12 -070017 if err != nil {
18 return err
19 }
20 if err := vom.NewEncoder(swc).Encode(obj); err != nil {
21 swc.Close()
22 return err
23 }
gauthamt1e313bc2014-11-10 15:45:56 -080024 return swc.Close()
Ankur100eb272014-09-15 16:48:12 -070025}
26
gauthamt1e313bc2014-11-10 15:45:56 -080027func 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)
Ankur100eb272014-09-15 16:48:12 -070030 }
gauthamt1e313bc2014-11-10 15:45:56 -080031 defer data.Close()
32 defer signature.Close()
33 vr, err := serialization.NewVerifyingReader(data, signature, publicKey)
Ankur100eb272014-09-15 16:48:12 -070034 if err != nil {
35 return err
36 }
37 return vom.NewDecoder(vr).Decode(obj)
38}