| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cryptor |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/base64" |
| 10 |
"encoding/json" |
| 11 |
"io/ioutil" |
| 12 |
"os" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/cloudflare/redoctober/config" |
| 16 |
"github.com/cloudflare/redoctober/keycache" |
| 17 |
"github.com/cloudflare/redoctober/passvault" |
| 18 |
"github.com/cloudflare/redoctober/persist" |
| 19 |
) |
| 20 |
|
| 21 |
func TestHash(t *testing.T) { |
| 22 |
decryptJson := []byte("{\"Version\":1,\"VaultId\":529853895,\"KeySet\":[{\"Name\":[\"Bob\",\"Alice\"],\"Key\":\"2j3tI2PBFBbwFX0BlQdUuA==\"},{\"Name\":[\"Bob\",\"Carol\"],\"Key\":\"yLSSB/U6+5rc1E+gjGXT4w==\"},{\"Name\":[\"Alice\",\"Bob\"],\"Key\":\"DDlWHF7szzISuXWaEz8llQ==\"},{\"Name\":[\"Alice\",\"Carol\"],\"Key\":\"TkA13aPrYFNbveIbl0qdww==\"},{\"Name\":[\"Carol\",\"Bob\"],\"Key\":\"KXm0uObmRJ2ZvSYEWPwk2A==\"},{\"Name\":[\"Carol\",\"Alice\"],\"Key\":\"L9c+PqtxPh9y6apRvtbCQw==\"}],\"KeySetRSA\":{\"Alice\":{\"Key\":\"fj5mqnq7y5KCafCGT1I51xI5JsX746+9TTSsp/8ybf3iZjhFzSlwP3aNmsOx3SUKTmZlfs+b+MeD4eKJ2uKBFzQHAIPO0fwoiCDKHhKH6KsolNq4+jgpkLAMOLsQGs8g6BhJy6bCFRjZVc3IdlQABPM6PkTbuSvKhn9atDFwZQD5TJBISi7d2hw4LradtLITbNqwiFMTQQ9+psXzyavY8H3LNHKGgf5Od7IpthEQPCHi4nw7X/YVRTEMfoIVcMcKOwYjlC45/VJEHK9Zy3DSiLBzjmr57YNIVjw8YZY5DGBWqbgu51RUbIcrqyLphBhXoBRu4R+yrhygBNWbvkkifA==\"},\"Bob\":{\"Key\":\"HTYiZ18sf721cAN1LRNkJ/+L4AKWilMrkMyNiBjWcl9HRTVPNXITqQBXd0fBggGNPiZr6VQTySK4ZFvJKGDGiz17Te/ToDn8Yk/B9cqMsN5fHoQtXvl8IZo2wioA67ccAJ1gHMMNpPyLdF43SQqgI+XaQ2lMSYLMfxxDmBBOQ1SWAto0BDRdnsqpwUwIPKQ9Y3/1osmrjLmJoAC3MPplexYWhexNwJtSd+mFdVZ3Qe4x9RsRHcN/myihOt/67V60qzs13F0RZkMSDzj5Ddg+1KVNJZY9dmolPNkAZj8z20L9uzpatrTYTR6A8q/sRn+inO7ZQVQ00XO6q6lYYQzxnw==\"},\"Carol\":{\"Key\":\"ItrvS02nSfbcA2fl1L1i61xqPEDKRdsrYe3+UCbkT+ipheiQRPSuikbzeV2kshn4yJDeku5bmTNqW8HSGtU7GTgCoIWV8WmEf4w6ovzShPbu+VrIZvRz3wjh2oYHT/gtPVAQnBa/71FeoBNxy5l/hBcUmBky43j83Mlt2+8QZx6PEUDmpaPQemVh99+C20nQtkAUFeMc2Ge4y7RlHSxtfABvwlXx1NzCD40nyJfF1SjV/fZh/E2Al4Tavx6DOJkYGoJ2mp7XBvX0IF2tp8T3U5VpnTek/WuNrLL9z7/jqzWh87lZ5KheWXhGkU1BNH4lfIj43pDkSy50aDvS0zYfHQ==\"}},\"IV\":\"58r9Mz8e06mItBG9nSV/0Q==\",\"Data\":\"QE9ZhcGXNXauUdMk04biUGy1SoP5H2nF/j2JjiiVFKPdIdRp/Gc+AZvUI9n22ZM4q+zDiJz7qvK4bKaPpXhTmGP0XheaFUukeVNS9STMoTbNcY/ZtVOz6hizUPF7gSq388QPUsT+Axml3rEUTWOhnw==\",\"Signature\":\"ItiAS26GFlbM5szJr5HXVB9BR+s=\"}") |
| 23 |
|
| 24 |
var encrypted EncryptedData |
| 25 |
if err := json.Unmarshal(decryptJson, &encrypted); err != nil { |
| 26 |
t.Fatalf("Error unmarshalling json, %v", err) |
| 27 |
} |
| 28 |
|
| 29 |
var hmacKey, _ = base64.StdEncoding.DecodeString("Qugc5ZQ0vC7KQSgmDHTVgQ==") |
| 30 |
var signature = append([]byte{}, encrypted.Signature...) |
| 31 |
|
| 32 |
expectedSig := encrypted.computeHmac(hmacKey) |
| 33 |
|
| 34 |
if diff := bytes.Compare(signature, expectedSig); diff != 0 { |
| 35 |
t.Fatalf("Error comparing signature %v", base64.StdEncoding.EncodeToString(expectedSig)) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
encrypted.Version = 2 |
| 40 |
unexpectedSig := encrypted.computeHmac(hmacKey) |
| 41 |
|
| 42 |
if diff := bytes.Compare(signature, unexpectedSig); diff == 0 { |
| 43 |
t.Fatalf("Error comparing signature") |
| 44 |
} |
| 45 |
encrypted.Version = 1 |
| 46 |
|
| 47 |
|
| 48 |
encrypted.VaultId = 529853896 |
| 49 |
unexpectedSig = encrypted.computeHmac(hmacKey) |
| 50 |
|
| 51 |
if diff := bytes.Compare(signature, unexpectedSig); diff == 0 { |
| 52 |
t.Fatalf("Error comparing signature") |
| 53 |
} |
| 54 |
encrypted.VaultId = 529853895 |
| 55 |
|
| 56 |
|
| 57 |
encrypted.KeySet[0], encrypted.KeySet[1] = encrypted.KeySet[1], encrypted.KeySet[0] |
| 58 |
unexpectedSig = encrypted.computeHmac(hmacKey) |
| 59 |
|
| 60 |
if diff := bytes.Compare(signature, unexpectedSig); diff != 0 { |
| 61 |
t.Fatalf("Error comparing signature %v, %v", |
| 62 |
base64.StdEncoding.EncodeToString(unexpectedSig), |
| 63 |
base64.StdEncoding.EncodeToString(signature)) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
encrypted.Version = 1 |
| 68 |
delete(encrypted.KeySetRSA, "Carol") |
| 69 |
unexpectedSig = encrypted.computeHmac(hmacKey) |
| 70 |
|
| 71 |
if diff := bytes.Compare(signature, unexpectedSig); diff == 0 { |
| 72 |
t.Fatalf("Error comparing signature") |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
func TestDuplicates(t *testing.T) { |
| 78 |
|
| 79 |
names := []string{"Alice", "Bob", "Carl"} |
| 80 |
recs := make(map[string]passvault.PasswordRecord, 0) |
| 81 |
left := []string{"Alice", "Bob"} |
| 82 |
right := []string{"Bob", "Carl"} |
| 83 |
|
| 84 |
|
| 85 |
cache := keycache.NewCache() |
| 86 |
records, err := passvault.InitFrom("memory") |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("%v", err) |
| 89 |
} |
| 90 |
|
| 91 |
cfg := &config.Delegations{Persist: false} |
| 92 |
store, err := persist.New(cfg) |
| 93 |
if err != nil { |
| 94 |
t.Fatal(err.Error()) |
| 95 |
} |
| 96 |
|
| 97 |
c := Cryptor{&records, &cache, store} |
| 98 |
|
| 99 |
for _, name := range names { |
| 100 |
pr, err := records.AddNewRecord(name, "weakpassword", true, passvault.DefaultRecordType) |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("%v", err) |
| 103 |
} |
| 104 |
|
| 105 |
recs[name] = pr |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
ac := AccessStructure{ |
| 110 |
LeftNames: left, |
| 111 |
RightNames: right, |
| 112 |
} |
| 113 |
|
| 114 |
resp, err := c.Encrypt([]byte("Hello World!"), []string{}, ac) |
| 115 |
if err != nil { |
| 116 |
t.Fatalf("Error: %s", err) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
for name, pr := range recs { |
| 121 |
err = cache.AddKeyFromRecord(pr, name, "weakpassword", nil, nil, 2, "", "1h") |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("%v", err) |
| 124 |
} |
| 125 |
|
| 126 |
_, _, _, _, err := c.Decrypt(resp, name) |
| 127 |
if err == nil { |
| 128 |
t.Fatalf("That shouldn't have worked!") |
| 129 |
} |
| 130 |
|
| 131 |
cache.Flush() |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestEncryptDecrypt(t *testing.T) { |
| 136 |
|
| 137 |
names := []string{"Alice", "Bob", "Carl"} |
| 138 |
recs := make(map[string]passvault.PasswordRecord, 0) |
| 139 |
left := []string{"Alice", "Bob"} |
| 140 |
right := []string{"Bob", "Carl"} |
| 141 |
|
| 142 |
|
| 143 |
cache := keycache.NewCache() |
| 144 |
records, err := passvault.InitFrom("memory") |
| 145 |
if err != nil { |
| 146 |
t.Fatalf("%v", err) |
| 147 |
} |
| 148 |
|
| 149 |
cfg := &config.Delegations{Persist: false} |
| 150 |
store, err := persist.New(cfg) |
| 151 |
if err != nil { |
| 152 |
t.Fatal(err.Error()) |
| 153 |
} |
| 154 |
|
| 155 |
c := Cryptor{&records, &cache, store} |
| 156 |
|
| 157 |
for _, name := range names { |
| 158 |
pr, err := records.AddNewRecord(name, "weakpassword", true, passvault.DefaultRecordType) |
| 159 |
if err != nil { |
| 160 |
t.Fatalf("%v", err) |
| 161 |
} |
| 162 |
|
| 163 |
recs[name] = pr |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
ac := AccessStructure{ |
| 168 |
LeftNames: left, |
| 169 |
RightNames: right, |
| 170 |
} |
| 171 |
|
| 172 |
resp, err := c.Encrypt([]byte("Hello World!"), []string{}, ac) |
| 173 |
if err != nil { |
| 174 |
t.Fatalf("Error: %s", err) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
for name, pr := range recs { |
| 179 |
err = cache.AddKeyFromRecord(pr, name, "weakpassword", nil, nil, 2, "", "1h") |
| 180 |
if err != nil { |
| 181 |
t.Fatalf("%v", err) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
_, _, _, _, err = c.Decrypt(resp, "alice") |
| 187 |
if err != nil { |
| 188 |
t.Fatalf("%v", err) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func tempName() (string, error) { |
| 193 |
tmpf, err := ioutil.TempFile("", "ro_cryptor") |
| 194 |
if err != nil { |
| 195 |
return "", err |
| 196 |
} |
| 197 |
|
| 198 |
name := tmpf.Name() |
| 199 |
tmpf.Close() |
| 200 |
return name, nil |
| 201 |
} |
| 202 |
|
| 203 |
func TestRestore(t *testing.T) { |
| 204 |
const testUses = 5 |
| 205 |
|
| 206 |
|
| 207 |
temp, err := tempName() |
| 208 |
if err != nil { |
| 209 |
t.Fatal(err) |
| 210 |
} |
| 211 |
defer os.Remove(temp) |
| 212 |
|
| 213 |
|
| 214 |
names := []string{"Alice", "Bob", "Carl"} |
| 215 |
recs := make(map[string]passvault.PasswordRecord, 0) |
| 216 |
|
| 217 |
|
| 218 |
cache := keycache.NewCache() |
| 219 |
records, err := passvault.InitFrom("memory") |
| 220 |
if err != nil { |
| 221 |
t.Fatalf("%v", err) |
| 222 |
} |
| 223 |
|
| 224 |
for _, name := range names { |
| 225 |
pr, err := records.AddNewRecord(name, "weakpassword", true, passvault.DefaultRecordType) |
| 226 |
if err != nil { |
| 227 |
t.Fatalf("%v", err) |
| 228 |
} |
| 229 |
|
| 230 |
recs[name] = pr |
| 231 |
} |
| 232 |
|
| 233 |
alice, ok := records.GetRecord("Alice") |
| 234 |
if !ok { |
| 235 |
t.Fatal("Alice not found in password vault.") |
| 236 |
} |
| 237 |
|
| 238 |
carl, ok := records.GetRecord("Carl") |
| 239 |
if !ok { |
| 240 |
t.Fatal("Carl not found in password vault.") |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
cfg := &config.Delegations{ |
| 245 |
Persist: true, |
| 246 |
Mechanism: persist.FileMechanism, |
| 247 |
Location: temp, |
| 248 |
Policy: "(Alice & Bob) | (Bob & Carl)", |
| 249 |
Users: []string{"Alice", "Bob", "Carl"}, |
| 250 |
} |
| 251 |
|
| 252 |
store, err := persist.New(cfg) |
| 253 |
if err != nil { |
| 254 |
t.Fatal(err.Error()) |
| 255 |
} |
| 256 |
|
| 257 |
c := Cryptor{&records, &cache, store} |
| 258 |
c.persist.Persist() |
| 259 |
|
| 260 |
err = c.Delegate(alice, "Alice", "weakpassword", []string{"Bob"}, []string{}, |
| 261 |
testUses, "", "1h") |
| 262 |
if err != nil { |
| 263 |
t.Fatal(err) |
| 264 |
} |
| 265 |
|
| 266 |
err = c.Delegate(carl, "Carl", "weakpassword", []string{"Bob"}, []string{}, |
| 267 |
testUses, "", "1h") |
| 268 |
|
| 269 |
|
| 270 |
store, err = persist.New(cfg) |
| 271 |
if err != nil { |
| 272 |
t.Fatal(err.Error()) |
| 273 |
} |
| 274 |
|
| 275 |
c = Cryptor{&records, &cache, store} |
| 276 |
if _, err := os.Stat(temp); err != nil { |
| 277 |
t.Fatalf("Not persisting: %v", err) |
| 278 |
} |
| 279 |
|
| 280 |
err = c.Restore("Alice", "weakpassword", 2, "", "1h") |
| 281 |
if err != ErrRestoreDelegations { |
| 282 |
t.Fatal(err) |
| 283 |
} |
| 284 |
|
| 285 |
err = c.Restore("Carl", "weakpassword", 2, "", "1h") |
| 286 |
if err != ErrRestoreDelegations { |
| 287 |
t.Fatal(err) |
| 288 |
} |
| 289 |
|
| 290 |
status := c.Status() |
| 291 |
if status.State != persist.Inactive { |
| 292 |
t.Fatalf("The persistent delegations should be %s, not %s", |
| 293 |
persist.Inactive, status.State) |
| 294 |
} |
| 295 |
|
| 296 |
err = c.Restore("Carl", "weakpassword", 0, "", "0h") |
| 297 |
if err != ErrRestoreDelegations { |
| 298 |
t.Fatal(err) |
| 299 |
} |
| 300 |
|
| 301 |
err = c.Restore("Bob", "weakpassword", 2, "", "1h") |
| 302 |
if err != nil { |
| 303 |
t.Fatal(err) |
| 304 |
} |
| 305 |
|
| 306 |
status = c.Status() |
| 307 |
if status.State != persist.Active { |
| 308 |
t.Fatalf("The persistent delegations should be %s, not %s", |
| 309 |
persist.Active, status.State) |
| 310 |
} |
| 311 |
|
| 312 |
if len(c.cache.UserKeys) != 2 { |
| 313 |
t.Fatalf("Delegations do not seem to have been restored.") |
| 314 |
} |
| 315 |
|
| 316 |
usage, ok := c.cache.UserKeys[keycache.DelegateIndex{Name: "Alice"}] |
| 317 |
if !ok { |
| 318 |
t.Fatalf("Alice not found in active delegations.") |
| 319 |
} |
| 320 |
|
| 321 |
if usage.Uses != testUses { |
| 322 |
t.Fatalf("Invalid number of uses in restored delegations.") |
| 323 |
} |
| 324 |
|
| 325 |
usage, ok = c.cache.UserKeys[keycache.DelegateIndex{Name: "Carl"}] |
| 326 |
if !ok { |
| 327 |
t.Fatalf("Carl not found in active delegations.") |
| 328 |
} |
| 329 |
|
| 330 |
if usage.Uses != testUses { |
| 331 |
t.Fatalf("Invalid number of uses in restored delegations.") |
| 332 |
} |
| 333 |
|
| 334 |
_, ok = c.cache.UserKeys[keycache.DelegateIndex{Name: "Bob"}] |
| 335 |
if ok { |
| 336 |
t.Fatalf("Bob shouldn't be in the active delegations.") |
| 337 |
} |
| 338 |
|
| 339 |
} |