github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/tao/auth/binary.go (about) 1 // Copyright (c) 2014, Kevin Walsh. 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package auth 16 17 // This file implements Marshal() and Unmarshal() functions for elements. 18 19 import ( 20 "fmt" 21 ) 22 23 const ( 24 _ = iota 25 26 // Term tags 27 tagPrin // string, Term, SubPrin 28 tagPrinTail // SubPrin 29 tagStr // string 30 tagBytes // string 31 tagInt // int 32 tagTermVar // string 33 34 // Form tags 35 tagPred // string, []Term 36 tagConst // bool 37 tagNot // Form 38 tagAnd // []Form 39 tagOr // []Form 40 tagImplies // Form, Form 41 tagSpeaksfor // Prin, Prin 42 tagSays // Prin, bool+int, bool+int, Form 43 tagForall // string, Form 44 tagExists // string, Form 45 46 // Other tags 47 tagSubPrin // []PrinExt 48 tagPrinExt // string, []Term 49 ) 50 51 // Context holds outer variable bindings in the order they appear. 52 // Context []string 53 // 54 // (c *Context) push(q *string) { 55 // c = append(c, q) 56 // } 57 // 58 // (c *Context) pop() { 59 // c = c[:len(c)-1] 60 // } 61 // 62 // (c *Context) deBruijn(q *string) int { 63 // n := len(c) 64 // for i := n-1; i >= 0; i-- { 65 // if c[i] == q { 66 // return n-i 67 // } 68 // } 69 // return 0 70 // } 71 72 // Marshal encodes a Form or Term. 73 func Marshal(e LogicElement) []byte { 74 buf := new(Buffer) 75 e.Marshal(buf) 76 return buf.Bytes() 77 } 78 79 // Marshal encodes a Prin. 80 func (t Prin) Marshal(buf *Buffer) { 81 buf.EncodeVarint(tagPrin) 82 buf.EncodeString(t.Type) 83 t.KeyHash.Marshal(buf) 84 t.Ext.Marshal(buf) 85 } 86 87 // Marshal encodes a PrinTail. 88 func (t PrinTail) Marshal(buf *Buffer) { 89 buf.EncodeVarint(tagPrinTail) 90 t.Ext.Marshal(buf) 91 } 92 93 func (t PrinExt) Marshal(buf *Buffer) { 94 buf.EncodeVarint(tagPrinExt) 95 buf.EncodeString(t.Name) 96 buf.EncodeVarint(int64(len(t.Arg))) 97 for _, e := range t.Arg { 98 e.Marshal(buf) 99 } 100 } 101 102 // Marshal encodes a SubPrin. 103 func (s SubPrin) Marshal(buf *Buffer) { 104 buf.EncodeVarint(tagSubPrin) 105 buf.EncodeVarint(int64(len(s))) 106 for _, e := range s { 107 e.Marshal(buf) 108 } 109 } 110 111 // Marshal encodes a Str. 112 func (t Str) Marshal(buf *Buffer) { 113 buf.EncodeVarint(tagStr) 114 buf.EncodeString(string(t)) 115 } 116 117 // Marshal encodes a Bytes. 118 func (t Bytes) Marshal(buf *Buffer) { 119 buf.EncodeVarint(tagBytes) 120 buf.EncodeString(string(t)) 121 } 122 123 // Marshal encodes an Int. 124 func (t Int) Marshal(buf *Buffer) { 125 buf.EncodeVarint(tagInt) 126 buf.EncodeVarint(int64(t)) 127 } 128 129 // Marshal encodes a TermVar. 130 func (t TermVar) Marshal(buf *Buffer) { 131 buf.EncodeVarint(tagTermVar) 132 buf.EncodeString(string(t)) 133 } 134 135 // Marshal encodes a Pred. 136 func (f Pred) Marshal(buf *Buffer) { 137 buf.EncodeVarint(tagPred) 138 buf.EncodeString(f.Name) 139 buf.EncodeVarint(int64(len(f.Arg))) 140 for _, e := range f.Arg { 141 e.Marshal(buf) 142 } 143 } 144 145 // Marshal encodes a Const. 146 func (f Const) Marshal(buf *Buffer) { 147 buf.EncodeVarint(tagConst) 148 buf.EncodeBool(bool(f)) 149 } 150 151 // Marshal encodes a Not. 152 func (f Not) Marshal(buf *Buffer) { 153 buf.EncodeVarint(tagNot) 154 f.Negand.Marshal(buf) 155 } 156 157 // Marshal encodes an And. 158 func (f And) Marshal(buf *Buffer) { 159 buf.EncodeVarint(tagAnd) 160 buf.EncodeVarint(int64(len(f.Conjunct))) 161 for _, e := range f.Conjunct { 162 e.Marshal(buf) 163 } 164 } 165 166 // Marshal encodes an Or. 167 func (f Or) Marshal(buf *Buffer) { 168 buf.EncodeVarint(tagOr) 169 buf.EncodeVarint(int64(len(f.Disjunct))) 170 for _, e := range f.Disjunct { 171 e.Marshal(buf) 172 } 173 } 174 175 // Marshal encodes an Implies. 176 func (f Implies) Marshal(buf *Buffer) { 177 buf.EncodeVarint(tagImplies) 178 f.Antecedent.Marshal(buf) 179 f.Consequent.Marshal(buf) 180 } 181 182 // Marshal encodes a Speaksfor. 183 func (f Speaksfor) Marshal(buf *Buffer) { 184 buf.EncodeVarint(tagSpeaksfor) 185 f.Delegate.Marshal(buf) 186 f.Delegator.Marshal(buf) 187 } 188 189 // Marshal encodes a Says. 190 func (f Says) Marshal(buf *Buffer) { 191 buf.EncodeVarint(tagSays) 192 f.Speaker.Marshal(buf) 193 buf.EncodeBool(f.Commences()) 194 if f.Commences() { 195 buf.EncodeVarint(*f.Time) 196 } 197 buf.EncodeBool(f.Expires()) 198 if f.Expires() { 199 buf.EncodeVarint(*f.Expiration) 200 } 201 f.Message.Marshal(buf) 202 } 203 204 // Marshal encodes a Forall. 205 func (f Forall) Marshal(buf *Buffer) { 206 buf.EncodeVarint(tagForall) 207 buf.EncodeString(f.Var) 208 f.Body.Marshal(buf) 209 } 210 211 // Marshal encodes an Exists. 212 func (f Exists) Marshal(buf *Buffer) { 213 buf.EncodeVarint(tagExists) 214 buf.EncodeString(f.Var) 215 f.Body.Marshal(buf) 216 } 217 218 // decodeStr decodes a Str without the leading tag. 219 func decodeStr(buf *Buffer) (Str, error) { 220 s, err := buf.DecodeString() 221 return Str(s), err 222 } 223 224 // decodeBytes decodes a Bytes without the leading tag. 225 func decodeBytes(buf *Buffer) (Bytes, error) { 226 s, err := buf.DecodeString() 227 return Bytes([]byte(s)), err 228 } 229 230 // decodeInt decodes an Int without the leading tag. 231 func decodeInt(buf *Buffer) (Int, error) { 232 i, err := buf.DecodeVarint() 233 return Int(i), err 234 } 235 236 // decodeTermVar decodes a TermVar without the leading tag. 237 func decodeTermVar(buf *Buffer) (TermVar, error) { 238 v, err := buf.DecodeString() 239 return TermVar(v), err 240 } 241 242 // decodeNameAndArgs decodes a name and term array without leading tags. 243 func decodeNameAndArgs(buf *Buffer) (name string, args []Term, err error) { 244 name, err = buf.DecodeString() 245 if err != nil { 246 return 247 } 248 n, err := buf.DecodeVarint() 249 args = make([]Term, n) 250 for i := int64(0); i < n; i++ { 251 args[i], err = unmarshalTerm(buf) 252 if err != nil { 253 return 254 } 255 } 256 return 257 } 258 259 // decodePrin decodes a Prin without the leading tag. 260 func decodePrin(buf *Buffer) (p Prin, err error) { 261 p.Type, err = buf.DecodeString() 262 if err != nil { 263 return 264 } 265 p.KeyHash, err = unmarshalTerm(buf) 266 if err != nil { 267 return 268 } 269 p.Ext, err = unmarshalSubPrin(buf) 270 return 271 } 272 273 // decodePrinTail decodes a PrinTail without the leading tag. 274 func decodePrinTail(buf *Buffer) (p PrinTail, err error) { 275 p.Ext, err = unmarshalSubPrin(buf) 276 return 277 } 278 279 // decodePrinExt decodes a PrinExt without the leading tag. 280 func decodePrinExt(buf *Buffer) (p PrinExt, err error) { 281 tag, err := buf.DecodeVarint() 282 if err != nil { 283 return PrinExt{}, err 284 } 285 if tag != tagPrinExt { 286 err = fmt.Errorf("unexpected tag: %d", tag) 287 return 288 } 289 p.Name, p.Arg, err = decodeNameAndArgs(buf) 290 if err == nil { 291 } 292 return 293 } 294 295 // unmarshalSubPrin decodes a SubPrin. 296 func unmarshalSubPrin(buf *Buffer) (s SubPrin, err error) { 297 tag, err := buf.DecodeVarint() 298 if err != nil { 299 return 300 } 301 if tag != tagSubPrin { 302 err = fmt.Errorf("unexpected tag: %d", tag) 303 return 304 } 305 return decodeSubPrin(buf) 306 } 307 308 // decodeSubPrin decodes a SubPrin without the leading tag. 309 func decodeSubPrin(buf *Buffer) (s SubPrin, err error) { 310 n, err := buf.DecodeVarint() 311 if err != nil { 312 return 313 } 314 for i := int64(0); i < n; i++ { 315 p, err := decodePrinExt(buf) 316 if err != nil { 317 return nil, err 318 } 319 s = append(s, p) 320 } 321 return 322 } 323 324 // unmarshalTerm decodes a Term. 325 func unmarshalTerm(buf *Buffer) (t Term, err error) { 326 tag, err := buf.DecodeVarint() 327 if err != nil { 328 return nil, err 329 } 330 switch tag { 331 case tagStr: 332 return decodeStr(buf) 333 case tagBytes: 334 return decodeBytes(buf) 335 case tagInt: 336 return decodeInt(buf) 337 case tagPrin: 338 return decodePrin(buf) 339 case tagPrinTail: 340 return decodePrinTail(buf) 341 case tagTermVar: 342 return decodeTermVar(buf) 343 default: 344 return nil, fmt.Errorf("unexpected tag: %d", tag) 345 } 346 } 347 348 // UnmarshalPrin decodes a Prin. 349 func UnmarshalPrin(bytes []byte) (p Prin, err error) { 350 t, err := UnmarshalTerm(bytes) 351 if err != nil { 352 return 353 } 354 p, ok := t.(Prin) // will always be value type here 355 if !ok { 356 err = fmt.Errorf("expected Prin, found %T", t) 357 } 358 return 359 } 360 361 // UnmarshalPrinTail decodes a PrinTail. 362 func UnmarshalPrinTail(bytes []byte) (p PrinTail, err error) { 363 t, err := UnmarshalTerm(bytes) 364 if err != nil { 365 return 366 } 367 p, ok := t.(PrinTail) // will always be value type here 368 if !ok { 369 err = fmt.Errorf("expected PrinTail, found %T", t) 370 } 371 return 372 } 373 374 // UnmarshalPrinExt decodes a PrinExt. 375 func UnmarshalPrinExt(bytes []byte) (p PrinExt, err error) { 376 buf := &Buffer{bytes} 377 return decodePrinExt(buf) 378 } 379 380 // UnmarshalTerm decodes a Term. 381 func UnmarshalTerm(bytes []byte) (Term, error) { 382 buf := &Buffer{bytes} 383 t, err := unmarshalTerm(buf) 384 if err != nil { 385 return nil, err 386 } 387 if len(buf.Bytes()) != 0 { 388 return nil, fmt.Errorf("unexpected trailing bytes") 389 } 390 return t, nil 391 } 392 393 // UnmarshalSubPrin decodes a SubPrin. 394 func UnmarshalSubPrin(bytes []byte) (SubPrin, error) { 395 buf := &Buffer{bytes} 396 t, err := unmarshalSubPrin(buf) 397 if err != nil { 398 return nil, err 399 } 400 if len(buf.Bytes()) != 0 { 401 return nil, fmt.Errorf("unexpected trailing bytes") 402 } 403 return t, nil 404 } 405 406 // UnmarshalForm decodes a Form. 407 func UnmarshalForm(bytes []byte) (Form, error) { 408 buf := &Buffer{bytes} 409 f, err := unmarshalForm(buf) 410 if err != nil { 411 return nil, err 412 } 413 if len(buf.Bytes()) != 0 { 414 return nil, fmt.Errorf("unexpected trailing bytes") 415 } 416 return f, nil 417 } 418 419 // unmarshalForm decodes a Form. 420 func unmarshalForm(buf *Buffer) (Form, error) { 421 tag, err := buf.DecodeVarint() 422 if err != nil { 423 return nil, err 424 } 425 switch tag { 426 case tagPred: 427 return decodePred(buf) 428 case tagConst: 429 return decodeConst(buf) 430 case tagNot: 431 return decodeNot(buf) 432 case tagAnd: 433 return decodeAnd(buf) 434 case tagOr: 435 return decodeOr(buf) 436 case tagImplies: 437 return decodeImplies(buf) 438 case tagSpeaksfor: 439 return decodeSpeaksfor(buf) 440 case tagSays: 441 return decodeSays(buf) 442 case tagForall: 443 return decodeForall(buf) 444 case tagExists: 445 return decodeExists(buf) 446 default: 447 return nil, fmt.Errorf("unexpected tag: %d", tag) 448 } 449 } 450 451 // decodePred decodes a Pred without the leading tag. 452 func decodePred(buf *Buffer) (Pred, error) { 453 name, args, err := decodeNameAndArgs(buf) 454 return Pred{name, args}, err 455 } 456 457 // decodeConst decodes a Const without the leading tag. 458 func decodeConst(buf *Buffer) (Const, error) { 459 b, err := buf.DecodeBool() 460 return Const(b), err 461 } 462 463 // decodeNot decodes a Not without the leading tag. 464 func decodeNot(buf *Buffer) (Not, error) { 465 f, err := unmarshalForm(buf) 466 return Not{f}, err 467 } 468 469 // decodeAnd decodes an And without the leading tag. 470 func decodeAnd(buf *Buffer) (and And, err error) { 471 n, err := buf.DecodeVarint() 472 if err != nil { 473 return 474 } 475 for i := int64(0); i < n; i++ { 476 f, err := unmarshalForm(buf) 477 if err != nil { 478 return and, err 479 } 480 and.Conjunct = append(and.Conjunct, f) 481 } 482 return 483 } 484 485 // decodeOr decodes an Or without the leading tag. 486 func decodeOr(buf *Buffer) (or Or, err error) { 487 n, err := buf.DecodeVarint() 488 if err != nil { 489 return 490 } 491 for i := int64(0); i < n; i++ { 492 f, err := unmarshalForm(buf) 493 if err != nil { 494 return or, err 495 } 496 or.Disjunct = append(or.Disjunct, f) 497 } 498 return 499 } 500 501 // decodeImplies decodes an Implies without the leading tag. 502 func decodeImplies(buf *Buffer) (implies Implies, err error) { 503 implies.Antecedent, err = unmarshalForm(buf) 504 if err != nil { 505 return 506 } 507 implies.Consequent, err = unmarshalForm(buf) 508 return 509 } 510 511 // decodeSpeaksfor decodes an Speaksfor without the leading tag. 512 func decodeSpeaksfor(buf *Buffer) (sfor Speaksfor, err error) { 513 sfor.Delegate, err = unmarshalTerm(buf) 514 if err != nil { 515 return 516 } 517 sfor.Delegator, err = unmarshalTerm(buf) 518 return 519 } 520 521 // decodeSays decodes an Says without the leading tag. 522 func decodeSays(buf *Buffer) (says Says, err error) { 523 says.Speaker, err = unmarshalTerm(buf) 524 if err != nil { 525 return 526 } 527 commences, err := buf.DecodeBool() 528 if err != nil { 529 return 530 } 531 if commences { 532 t, err := buf.DecodeVarint() 533 if err != nil { 534 return says, err 535 } 536 says.Time = &t 537 } 538 expires, err := buf.DecodeBool() 539 if err != nil { 540 return 541 } 542 if expires { 543 t, err := buf.DecodeVarint() 544 if err != nil { 545 return says, err 546 } 547 says.Expiration = &t 548 } 549 says.Message, err = unmarshalForm(buf) 550 return 551 } 552 553 // decodeForall decodes a Forall without the leading tag. 554 func decodeForall(buf *Buffer) (forall Forall, err error) { 555 forall.Var, err = buf.DecodeString() 556 if err != nil { 557 return 558 } 559 forall.Body, err = unmarshalForm(buf) 560 return 561 } 562 563 // decodeExists decodes an Exists without the leading tag. 564 func decodeExists(buf *Buffer) (exists Exists, err error) { 565 exists.Var, err = buf.DecodeString() 566 if err != nil { 567 return 568 } 569 exists.Body, err = unmarshalForm(buf) 570 return 571 }