Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 1 | package ipc |
| 2 | |
| 3 | import ( |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 4 | "crypto/sha256" |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 5 | "fmt" |
| 6 | "reflect" |
| 7 | "sync" |
| 8 | |
Suharsh Sivakumar | af862a5 | 2015-02-04 13:50:47 -0800 | [diff] [blame] | 9 | "v.io/core/veyron/runtimes/google/ipc/stream" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame^] | 10 | "v.io/v23/ipc" |
| 11 | "v.io/v23/security" |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | // clientEncodeBlessings gets or inserts the blessings into the cache. |
| 15 | func clientEncodeBlessings(cache stream.VCDataCache, blessings security.Blessings) ipc.BlessingsRequest { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 16 | blessingsCacheAny := cache.GetOrInsert(clientBlessingsCacheKey{}, newClientBlessingsCache) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 17 | blessingsCache := blessingsCacheAny.(*clientBlessingsCache) |
| 18 | return blessingsCache.getOrInsert(blessings) |
| 19 | } |
| 20 | |
| 21 | // clientAckBlessings verifies that the server has updated its cache to include blessings. |
| 22 | // This means that subsequent rpcs from the client with blessings can send only a cache key. |
| 23 | func clientAckBlessings(cache stream.VCDataCache, blessings security.Blessings) { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 24 | blessingsCacheAny := cache.GetOrInsert(clientBlessingsCacheKey{}, newClientBlessingsCache) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 25 | blessingsCache := blessingsCacheAny.(*clientBlessingsCache) |
| 26 | blessingsCache.acknowledge(blessings) |
| 27 | } |
| 28 | |
| 29 | // serverDecodeBlessings insert the key and blessings into the cache or get the blessings if only |
| 30 | // key is provided in req. |
| 31 | func serverDecodeBlessings(cache stream.VCDataCache, req ipc.BlessingsRequest, stats *ipcStats) (security.Blessings, error) { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 32 | blessingsCacheAny := cache.GetOrInsert(serverBlessingsCacheKey{}, newServerBlessingsCache) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 33 | blessingsCache := blessingsCacheAny.(*serverBlessingsCache) |
| 34 | return blessingsCache.getOrInsert(req, stats) |
| 35 | } |
| 36 | |
| 37 | // IMPLEMENTATION DETAILS BELOW |
| 38 | |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 39 | // clientBlessingsCache is a thread-safe map from blessings to cache id. |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 40 | type clientBlessingsCache struct { |
| 41 | sync.RWMutex |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 42 | m map[[sha256.Size]byte]clientCacheValue |
| 43 | curId uint64 |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type clientCacheValue struct { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 47 | id uint64 |
| 48 | // ack is set to true once the server has confirmed receipt of the cache id. |
| 49 | // Clients that insert into the cache when ack is false must send both the id |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 50 | // and the blessings. |
| 51 | ack bool |
| 52 | } |
| 53 | |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 54 | // clientBlessingsCacheKey is the key used to retrieve the clientBlessingsCache from the VCDataCache. |
| 55 | type clientBlessingsCacheKey struct{} |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 56 | |
| 57 | func newClientBlessingsCache() interface{} { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 58 | return &clientBlessingsCache{m: make(map[[sha256.Size]byte]clientCacheValue)} |
| 59 | } |
| 60 | |
| 61 | func getBlessingsHashKey(blessings security.Blessings) (key [sha256.Size]byte) { |
| 62 | h := sha256.New() |
| 63 | for _, chain := range security.MarshalBlessings(blessings).CertificateChains { |
| 64 | if len(chain) == 0 { |
| 65 | continue |
| 66 | } |
| 67 | cert := chain[len(chain)-1] |
| 68 | s := sha256.Sum256(cert.Signature.R) |
| 69 | h.Write(s[:]) |
| 70 | s = sha256.Sum256(cert.Signature.S) |
| 71 | h.Write(s[:]) |
| 72 | } |
| 73 | copy(key[:], h.Sum(nil)) |
| 74 | return |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | func (c *clientBlessingsCache) getOrInsert(blessings security.Blessings) ipc.BlessingsRequest { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 78 | key := getBlessingsHashKey(blessings) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 79 | c.RLock() |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 80 | val, exists := c.m[key] |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 81 | c.RUnlock() |
| 82 | if exists { |
| 83 | return c.makeBlessingsRequest(val, blessings) |
| 84 | } |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 85 | // If the val doesn't exist we must create a new id, update the cache, and send the id and blessings. |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 86 | c.Lock() |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 87 | // We must check that the val wasn't inserted in the time we changed locks. |
| 88 | val, exists = c.m[key] |
| 89 | if !exists { |
| 90 | val = clientCacheValue{id: c.nextIdLocked()} |
| 91 | c.m[key] = val |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 92 | } |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 93 | c.Unlock() |
| 94 | return c.makeBlessingsRequest(val, blessings) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | func (c *clientBlessingsCache) acknowledge(blessings security.Blessings) { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 98 | key := getBlessingsHashKey(blessings) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 99 | c.Lock() |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 100 | val := c.m[key] |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 101 | val.ack = true |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 102 | c.m[key] = val |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 103 | c.Unlock() |
| 104 | } |
| 105 | |
| 106 | func (c *clientBlessingsCache) makeBlessingsRequest(val clientCacheValue, blessings security.Blessings) ipc.BlessingsRequest { |
| 107 | if val.ack { |
| 108 | // when the value is acknowledged, only send the key, since the server has confirmed that it knows the key. |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 109 | return ipc.BlessingsRequest{Key: val.id} |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 110 | } |
| 111 | // otherwise we still need to send both key and blessings, but we must ensure that we send the same key. |
| 112 | wireBlessings := security.MarshalBlessings(blessings) |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 113 | return ipc.BlessingsRequest{val.id, &wireBlessings} |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 116 | // nextIdLocked creates a new id for inserting blessings. It must be called after acquiring a writer lock. |
| 117 | func (c *clientBlessingsCache) nextIdLocked() uint64 { |
| 118 | c.curId++ |
| 119 | return c.curId |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // serverBlessingsCache is a thread-safe map from cache key to blessings. |
| 123 | type serverBlessingsCache struct { |
| 124 | sync.RWMutex |
| 125 | m map[uint64]security.Blessings |
| 126 | } |
| 127 | |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 128 | // serverBlessingsCacheKey is the key used to retrieve the serverBlessingsCache from the VCDataCache. |
| 129 | type serverBlessingsCacheKey struct{} |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 130 | |
| 131 | func newServerBlessingsCache() interface{} { |
| 132 | return &serverBlessingsCache{m: make(map[uint64]security.Blessings)} |
| 133 | } |
| 134 | |
| 135 | func (c *serverBlessingsCache) getOrInsert(req ipc.BlessingsRequest, stats *ipcStats) (security.Blessings, error) { |
| 136 | // In the case that the key sent is 0, we are running in VCSecurityNone and should |
| 137 | // return nil for the client Blessings. |
| 138 | if req.Key == 0 { |
| 139 | return nil, nil |
| 140 | } |
| 141 | if req.Blessings == nil { |
| 142 | // Fastpath, lookup based on the key. |
| 143 | c.RLock() |
| 144 | cached, exists := c.m[req.Key] |
| 145 | c.RUnlock() |
| 146 | if !exists { |
| 147 | return nil, fmt.Errorf("ipc: key was not in the cache") |
| 148 | } |
| 149 | stats.recordBlessingCache(true) |
| 150 | return cached, nil |
| 151 | } |
Suharsh Sivakumar | defea2b | 2015-01-13 09:33:50 -0800 | [diff] [blame] | 152 | // Always count the slow path as a cache miss since we do not get the benefit of sending only the cache key. |
| 153 | stats.recordBlessingCache(false) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 154 | // Slowpath, might need to update the cache, or check that the received blessings are |
| 155 | // the same as what's in the cache. |
| 156 | recv, err := security.NewBlessings(*req.Blessings) |
| 157 | if err != nil { |
| 158 | return nil, fmt.Errorf("ipc: create new client blessings failed: %v", err) |
| 159 | } |
| 160 | c.Lock() |
| 161 | defer c.Unlock() |
| 162 | if cached, exists := c.m[req.Key]; exists { |
| 163 | // TODO(suharshs): Replace this reflect.DeepEqual() with a less expensive check. |
| 164 | if !reflect.DeepEqual(cached, recv) { |
| 165 | return nil, fmt.Errorf("client sent invalid Blessings") |
| 166 | } |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 167 | return cached, nil |
| 168 | } |
| 169 | c.m[req.Key] = recv |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 170 | return recv, nil |
| 171 | } |