github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/support_infrastructure/secret_service/secret_service.go (about) 1 // Copyright (c) 2016, Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 13 package secret_service 14 15 import ( 16 "container/list" 17 "errors" 18 19 "github.com/jlmucb/cloudproxy/go/support_libraries/protected_objects" 20 "github.com/jlmucb/cloudproxy/go/tao" 21 "github.com/jlmucb/cloudproxy/go/tao/auth" 22 ) 23 24 func ReadObject(l *list.List, encKey *tao.Keys, id *protected_objects.ObjectIdMessage, 25 program *auth.Prin, domain *tao.Domain) (*string, []byte, error) { 26 27 if !domain.Guard.IsAuthorized(*program, "READ", []string{id.String()}) { 28 return nil, nil, errors.New("program not authorized to read requested secret") 29 } 30 return readObjRec(l, encKey, id) 31 } 32 33 func readObjRec(l *list.List, encKey *tao.Keys, id *protected_objects.ObjectIdMessage) (*string, 34 []byte, error) { 35 36 elem := protected_objects.FindElementById(l, *id.ObjName, *id.ObjEpoch) 37 if elem == nil { 38 return nil, nil, errors.New("object not found") 39 } 40 pObj := elem.Value.(protected_objects.ProtectedObjectMessage) 41 if pObj.ProtectorObjId == nil { 42 // Decrypt root using encKeys. 43 rootKey, err := encKey.CryptingKey.Decrypt(pObj.GetBlob()) 44 if err != nil { 45 return nil, nil, err 46 } 47 str := "key" 48 return &str, rootKey, nil 49 } 50 parentType, parentKey, err := readObjRec(l, encKey, pObj.ProtectorObjId) 51 if err != nil { 52 return nil, nil, err 53 } 54 if *parentType != "key" { 55 return nil, nil, errors.New("internal node with type not key") 56 } 57 obj, err := protected_objects.RecoverProtectedObject(&pObj, parentKey) 58 if err != nil { 59 return nil, nil, err 60 } 61 return obj.ObjType, obj.ObjVal, nil 62 } 63 64 func WriteObject(l *list.List, encKey *tao.Keys, id *protected_objects.ObjectIdMessage, 65 program *auth.Prin, domain *tao.Domain, newType string, 66 newVal []byte) error { 67 68 if !domain.Guard.IsAuthorized(*program, "WRITE", []string{id.String()}) { 69 return errors.New("program not authorized to write requested secret") 70 } 71 72 element := protected_objects.FindElementById(l, *id.ObjName, *id.ObjEpoch) 73 if element == nil { 74 return errors.New("attemtping to write non-existant object") 75 } 76 pOld := element.Value.(protected_objects.ProtectedObjectMessage) 77 parentId := pOld.ProtectorObjId 78 if parentId == nil { 79 return errors.New("attempting to write root key") 80 } 81 parentType, parentKey, err := readObjRec(l, encKey, parentId) 82 if err != nil { 83 return err 84 } 85 if *parentType != "key" { 86 return errors.New("parent of object to be written is not a key") 87 } 88 new := protected_objects.ObjectMessage{ 89 ObjId: id, 90 ObjVal: newVal, 91 ObjType: &newType} 92 pNew, err := protected_objects.MakeProtectedObject(new, *parentId.ObjName, 93 *parentId.ObjEpoch, parentKey) 94 if err != nil { 95 return errors.New("can not make protected object") 96 } 97 element.Value = *pNew 98 return nil 99 } 100 101 func CreateObject(l *list.List, newId, protectorId *protected_objects.ObjectIdMessage, 102 encKey *tao.Keys, program *auth.Prin, domain *tao.Domain, newType string, 103 newVal []byte) error { 104 105 if !domain.Guard.IsAuthorized(*program, "CREATE", []string{protectorId.String()}) { 106 return errors.New("program not authorized to create requested secret") 107 } 108 109 _, _, err := readObjRec(l, encKey, newId) 110 if err == nil { 111 return errors.New("creating object with existing id") 112 } 113 114 protectorType, protectorKey, err := readObjRec(l, encKey, protectorId) 115 if err != nil { 116 return err 117 } 118 if *protectorType != "key" { 119 return errors.New("creating object protected by object type not key") 120 } 121 122 new := protected_objects.ObjectMessage{ 123 ObjId: newId, 124 ObjVal: newVal, 125 ObjType: &newType} 126 pNew, err := protected_objects.MakeProtectedObject(new, *protectorId.ObjName, 127 *protectorId.ObjEpoch, protectorKey) 128 129 l.PushFront(*pNew) 130 return nil 131 } 132 133 func DeleteObject(l *list.List, id *protected_objects.ObjectIdMessage, program *auth.Prin, 134 domain *tao.Domain) error { 135 136 if !domain.Guard.IsAuthorized(*program, "DELETE", []string{id.String()}) { 137 return errors.New("program not authorized to delete requested secret") 138 } 139 140 element := protected_objects.FindElementById(l, *id.ObjName, *id.ObjEpoch) 141 if element == nil { 142 return errors.New("object to be deleted not found") 143 } 144 l.Remove(element) 145 return nil 146 }