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 "reflect" |
| 13 "sync" |
| 14 |
| 15 vpkg "mojo/services/vanadium/security/interfaces/principal" |
| 16 ) |
| 17 |
| 18 type principal struct { |
| 19 private *ecdsa.PrivateKey |
| 20 mu sync.Mutex |
| 21 userList []vpkg.User // GUARDED_BY(mu) |
| 22 curr *vpkg.User // GUARDED_BY(mu) |
| 23 } |
| 24 |
| 25 func (p *principal) publicKey() publicKey { |
| 26 return newECDSAPublicKey(&p.private.PublicKey) |
| 27 } |
| 28 |
| 29 func (p *principal) users() []vpkg.User { |
| 30 p.mu.Lock() |
| 31 defer p.mu.Unlock() |
| 32 userList := make([]vpkg.User, len(p.userList)) |
| 33 copy(userList, p.userList) |
| 34 return userList |
| 35 } |
| 36 |
| 37 func (p *principal) addUser(user vpkg.User) { |
| 38 p.mu.Lock() |
| 39 defer p.mu.Unlock() |
| 40 p.userList = append(p.userList, user) |
| 41 p.curr = &user |
| 42 } |
| 43 |
| 44 func (p *principal) setCurrentUser(user vpkg.User) (err *string) { |
| 45 p.mu.Lock() |
| 46 defer p.mu.Unlock() |
| 47 for _, u := range p.userList { |
| 48 if !reflect.DeepEqual(u, user) { |
| 49 str := fmt.Sprintf("User %v does not exist", user) |
| 50 return &str |
| 51 } |
| 52 } |
| 53 p.curr = &user |
| 54 return |
| 55 } |
| 56 |
| 57 func (p *principal) clearCurrentUser() { |
| 58 p.mu.Lock() |
| 59 defer p.mu.Unlock() |
| 60 p.curr = nil |
| 61 } |
| 62 |
| 63 func newPrincipal() (*principal, error) { |
| 64 priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 65 if err != nil { |
| 66 return nil, err |
| 67 } |
| 68 return &principal{private: priv}, nil |
| 69 } |
OLD | NEW |