Index: services/vanadium/security/principal.go |
diff --git a/services/vanadium/security/principal.go b/services/vanadium/security/principal.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e126b498606fca3b70b5b1c2b861ca02adb5b4a3 |
--- /dev/null |
+++ b/services/vanadium/security/principal.go |
@@ -0,0 +1,77 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package main |
+ |
+import ( |
+ "crypto/ecdsa" |
+ "crypto/elliptic" |
+ "crypto/rand" |
+ "fmt" |
+ "sync" |
+ |
+ vpkg "mojo/services/vanadium/security/interfaces/principal" |
+) |
+ |
+type principal struct { |
+ private *ecdsa.PrivateKey |
+ mu sync.Mutex |
+ blessings map[vpkg.User]*wireBlessings // GUARDED_BY(mu) |
+ curr *vpkg.User // GUARDED_BY(mu) |
+} |
+ |
+func (p *principal) publicKey() publicKey { |
+ return newECDSAPublicKey(&p.private.PublicKey) |
+} |
+ |
+func (p *principal) currentBlessing() *wireBlessings { |
+ p.mu.Lock() |
+ defer p.mu.Unlock() |
+ if p.curr == nil { |
+ return nil |
+ } |
+ return p.blessings[*p.curr] |
+} |
+ |
+func (p *principal) users() ([]vpkg.User, *vpkg.User) { |
+ p.mu.Lock() |
+ defer p.mu.Unlock() |
+ var users []vpkg.User |
+ for user, _ := range p.blessings { |
+ users = append(users, user) |
+ } |
+ return users, p.curr |
+} |
+ |
+func (p *principal) addUser(user vpkg.User, blessing *wireBlessings) { |
+ p.mu.Lock() |
+ defer p.mu.Unlock() |
+ p.blessings[user] = blessing |
+ p.curr = &user |
+} |
+ |
+func (p *principal) setCurrentUser(user vpkg.User) (err *string) { |
+ p.mu.Lock() |
+ defer p.mu.Unlock() |
+ if _, ok := p.blessings[user]; !ok { |
+ str := fmt.Sprintf("User %v does not exist", user) |
+ return &str |
+ } |
+ p.curr = &user |
+ return |
+} |
+ |
+func (p *principal) unsetCurrentUser() { |
+ p.mu.Lock() |
+ defer p.mu.Unlock() |
+ p.curr = nil |
+} |
+ |
+func newPrincipal() (*principal, error) { |
+ priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
+ if err != nil { |
+ return nil, err |
+ } |
+ return &principal{private: priv, blessings: make(map[vpkg.User]*wireBlessings)}, nil |
+} |