Skip to content

Commit 9a2d48e

Browse files
committed
Use vaults to manage many keys
1 parent f0a8342 commit 9a2d48e

21 files changed

+462
-361
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@ WIP. Feedback and comments are welcome.
99

1010
| | |
1111
| --- | --- |
12-
| ![Encrypted field](img/E2Eencrypted.png) | ![Passphrase](img/E2Epasphrase.png) |
12+
| ![Encrypted field](img/WhithoutKey.png) | ![Partial](img/MultiKey.png) |
13+
| ![Vaults](img/Vaults.png) | ![Passphrase](img/VaultsPass.png) |
14+
15+
16+
### Release 1.0
17+
18+
Simple shared key
19+
20+
21+
### Release 1.2
22+
23+
Manage many shared key to create «vaults»
24+
25+
26+
### Release 2.0
27+
28+
TODO: Add users with roles and public/private keys. Random key by vault. Store vault key for each users.
29+
30+
1331

1432
## Server
1533

@@ -19,7 +37,7 @@ gin-model-template allows to quickly write database tables and REST handlers.
1937

2038
For end to end encryption, server maintains a global variable of the passphrase salt and check that each encrypted field start with this salt.
2139

22-
### Config
40+
### Custom Config
2341
- ``people.go`` and ``user.go`` are 2 samples tables which can be customized.
2442
- ``repo.go`` contains db parameters
2543
- ``server.go`` contains REST handlers

img/MultiKey.png

27.4 KB
Loading

img/Vaults.png

23.5 KB
Loading

img/VaultsPass.png

31.4 KB
Loading

img/VaultsReencrypt.png

26.4 KB
Loading

img/WhithoutKey.png

24.5 KB
Loading

models/people.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ func PostPeople(c *gin.Context) {
112112
fmt.Println(people)
113113
}
114114

115-
// XXX Check encrypted field match current key
116-
if people.XAddress != "" && people.XAddress[0:16] != CurrentSalt {
115+
// XXX Check encrypted field match a valid key
116+
if people.XAddress != "" && TestValidSalt(dbmap, people.XAddress[0:16]) == false {
117117
people.Name = ""
118118
}
119-
if people.XDateOfBirth != "" && people.XDateOfBirth[0:16] != CurrentSalt {
119+
if people.XDateOfBirth != "" && TestValidSalt(dbmap, people.XDateOfBirth[0:16]) == false {
120120
people.Name = ""
121121
}
122122

@@ -162,11 +162,12 @@ func UpdatePeople(c *gin.Context) {
162162
Created: people.Created, //people read from previous select
163163
}
164164

165-
// XXX Check encrypted field match current key
166-
if people.XAddress != "" && people.XAddress[0:16] != CurrentSalt {
165+
// XXX Check encrypted field match a valid key
166+
// else create mandatory field error
167+
if people.XAddress != "" && TestValidSalt(dbmap, people.XAddress[0:16]) == false {
167168
people.Name = ""
168169
}
169-
if people.XDateOfBirth != "" && people.XDateOfBirth[0:16] != CurrentSalt {
170+
if people.XDateOfBirth != "" && TestValidSalt(dbmap, people.XDateOfBirth[0:16]) == false {
170171
people.Name = ""
171172
}
172173

models/people_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,24 @@ func TestPeople(t *testing.T) {
3636

3737
b := new(bytes.Buffer)
3838

39-
router.PUT("/api/v1/utils/:id", UpdateVerifKey)
39+
/*router.PUT("/api/v1/utils/:id", UpdateVerifKey)
4040
log.Println("= http PUT one Util")
4141
var k = Util{VerifyKey: "XXXXXXXXXXXXXXXX"}
4242
json.NewEncoder(b).Encode(k)
4343
req, err := http.NewRequest("PUT", "/api/v1/utils/1", b)
4444
req.Header.Set("Content-Type", "application/json")
4545
resp := httptest.NewRecorder()
4646
router.ServeHTTP(resp, req)
47-
assert.Equal(t, 200, resp.Code, "http PUT Salt key success")
47+
assert.Equal(t, 200, resp.Code, "http PUT Salt key success")*/
48+
log.Println("= http POST Vault")
49+
router.PUT("/api/v1/vaults", PostVault)
50+
var k = Vault{VerifyKey: "XXXXXXXXXXXXXXXX", VaultName: "123"}
51+
json.NewEncoder(b).Encode(k)
52+
req, err := http.NewRequest("PUT", "/api/v1/vaults", b)
53+
req.Header.Set("Content-Type", "application/json")
54+
resp := httptest.NewRecorder()
55+
router.ServeHTTP(resp, req)
56+
assert.Equal(t, 201, resp.Code, "http PUT Salt key success")
4857

4958
// Add
5059
log.Println("= http POST People")
@@ -207,9 +216,10 @@ func TestE2ECrypto(t *testing.T) {
207216
var urla = "/api/v1"
208217
router.POST(urla+"/peoples", PostPeople)
209218
router.GET(urla+"/peoples/:id", GetPeople)
210-
router.GET(urla+"/verifkey/:id", GetVerifKey)
211-
router.PUT(urla+"/verifkey/:id", UpdateVerifKey)
212-
//router.PUT(urla+"/:id", UpdatePeople)
219+
//router.GET(urla+"/verifkey/:id", GetVerifKey)
220+
//router.PUT(urla+"/verifkey/:id", UpdateVerifKey)
221+
router.GET(urla+"/vaults/:id", GetVault)
222+
router.PUT(urla+"/vaults", PostVault)
213223

214224
log.Println("= Create scrypt key")
215225
Salt, _ := newRandBytes(12)
@@ -226,14 +236,15 @@ func TestE2ECrypto(t *testing.T) {
226236
//fmt.Printf("%+v %+v %+v\n", Saltb64, Nonceb64, Cipherb64)
227237

228238
b := new(bytes.Buffer)
229-
var v = Util{Id: 1, VerifyKey: Saltb64 + Nonceb64 + Cipherb64}
239+
//var v = Util{Id: 1, VerifyKey: Saltb64 + Nonceb64 + Cipherb64}
240+
var v = Vault{VerifyKey: Saltb64 + Nonceb64 + Cipherb64, VaultName: "test"}
230241
json.NewEncoder(b).Encode(v)
231242
//fmt.Printf("%+v\n", b)
232-
req, _ := http.NewRequest("PUT", urla+"/verifkey/1", b)
243+
req, _ := http.NewRequest("PUT", urla+"/vaults", b)
233244
req.Header.Set("Content-Type", "application/json")
234245
resp := httptest.NewRecorder()
235246
router.ServeHTTP(resp, req)
236-
assert.Equal(t, 200, resp.Code, "Verify key set")
247+
assert.Equal(t, 201, resp.Code, "Verify key set")
237248

238249
log.Println("= Create People with encrypted XAddress")
239250
plaintext := "some private people text"

models/repo.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ import (
1212
"strings"
1313
)
1414

15-
var CurrentSalt string
15+
// TestValidSalt : test if key is a valid salt in vaults
16+
func TestValidSalt(dbmap *gorp.DbMap, test string) bool {
17+
var vault Vault
18+
err := dbmap.SelectOne(&vault, "SELECT id FROM vault WHERE verifykey LIKE ?", test+"%")
19+
if err == nil && vault.Id != 0 {
20+
return true
21+
}
22+
return false
23+
}
1624

17-
// gin Middlware to select database
25+
// Database : gin Middlware to select database
1826
func Database(connString string) gin.HandlerFunc {
1927
dbmap := InitDb(connString)
2028
return func(c *gin.Context) {
@@ -23,6 +31,7 @@ func Database(connString string) gin.HandlerFunc {
2331
}
2432
}
2533

34+
// InitDb : create or update and connect to db on startup
2635
func InitDb(dbName string) *gorp.DbMap {
2736
// XXX fix database type
2837
db, err := sql.Open("sqlite3", dbName)
@@ -32,21 +41,23 @@ func InitDb(dbName string) *gorp.DbMap {
3241
// XXX fix tables names
3342
dbmap.AddTableWithName(People{}, "People").SetKeys(true, "Id")
3443
dbmap.AddTableWithName(User{}, "User").SetKeys(true, "Id")
35-
dbmap.AddTableWithName(Util{}, "Util").SetKeys(true, "Id")
44+
dbmap.AddTableWithName(Vault{}, "Vault").SetKeys(true, "Id")
3645
err = dbmap.CreateTablesIfNotExists()
3746
checkErr(err, "Create tables failed")
3847

39-
var u Util
48+
/*var u Util
4049
dbmap.SelectOne(&u, "select * from Util where id = 1")
4150
if u.Id != 1 {
4251
dbmap.Insert(&u)
4352
} else {
4453
CurrentSalt = u.VerifyKey[0:16]
45-
}
54+
}*/
4655

4756
return dbmap
4857
}
4958

59+
60+
// ParseQuery : Parse a http query
5061
func ParseQuery(q map[string][]string) (string, string, string) {
5162
query := ""
5263
if q["_filters"] != nil {

0 commit comments

Comments
 (0)