OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package main |
| 6 |
| 7 import ( |
| 8 "crypto/ecdsa" |
| 9 "crypto/elliptic" |
| 10 "crypto/rand" |
| 11 "fmt" |
| 12 "sync" |
| 13 |
| 14 vpkg "mojo/services/vanadium/security/interfaces/principal" |
| 15 ) |
| 16 |
| 17 type principal struct { |
| 18 private *ecdsa.PrivateKey |
| 19 mu sync.Mutex |
| 20 blessings map[vpkg.UserId]*wireBlessings // GUARDED_BY(mu) |
| 21 curr *vpkg.UserId // GUARDED_BY(mu) |
| 22 } |
| 23 |
| 24 func (p *principal) publicKey() publicKey { |
| 25 return newECDSAPublicKey(&p.private.PublicKey) |
| 26 } |
| 27 |
| 28 func (p *principal) currentBlessing() *wireBlessings { |
| 29 p.mu.Lock() |
| 30 defer p.mu.Unlock() |
| 31 if p.curr == nil { |
| 32 return nil |
| 33 } |
| 34 return p.blessings[*p.curr] |
| 35 } |
| 36 |
| 37 func (p *principal) users() ([]vpkg.UserId, *vpkg.UserId) { |
| 38 p.mu.Lock() |
| 39 defer p.mu.Unlock() |
| 40 var users []vpkg.UserId |
| 41 for id, _ := range p.blessings { |
| 42 users = append(users, id) |
| 43 } |
| 44 return users, p.curr |
| 45 } |
| 46 |
| 47 func (p *principal) addUser(id vpkg.UserId, blessing *wireBlessings) { |
| 48 p.mu.Lock() |
| 49 defer p.mu.Unlock() |
| 50 p.blessings[id] = blessing |
| 51 p.curr = &id |
| 52 } |
| 53 |
| 54 func (p *principal) setCurrentUser(id vpkg.UserId) (err *string) { |
| 55 p.mu.Lock() |
| 56 defer p.mu.Unlock() |
| 57 if _, ok := p.blessings[id]; !ok { |
| 58 str := fmt.Sprintf("UserId %v does not exist", id) |
| 59 return &str |
| 60 } |
| 61 p.curr = &id |
| 62 return |
| 63 } |
| 64 |
| 65 func (p *principal) unsetCurrentUser() { |
| 66 p.mu.Lock() |
| 67 defer p.mu.Unlock() |
| 68 p.curr = nil |
| 69 } |
| 70 |
| 71 func newPrincipal() (*principal, error) { |
| 72 priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 73 if err != nil { |
| 74 return nil, err |
| 75 } |
| 76 return &principal{private: priv, blessings: make(map[vpkg.UserId]*wireBl
essings)}, nil |
| 77 } |
OLD | NEW |