updates.go 5.87 KB
Newer Older
1
package irmaclient
Sietse Ringers's avatar
Sietse Ringers committed
2
3
4
5
6
7
8

import (
	"encoding/json"
	"encoding/xml"
	"html"
	"io/ioutil"
	"math/big"
9
	"time"
Sietse Ringers's avatar
Sietse Ringers committed
10

Sietse Ringers's avatar
Sietse Ringers committed
11
	"github.com/go-errors/errors"
Sietse Ringers's avatar
Sietse Ringers committed
12
	"github.com/mhe/gabi"
13
14
	"github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/internal/fs"
Sietse Ringers's avatar
Sietse Ringers committed
15
16
)

17
// This file contains the update mechanism for Client
Sietse Ringers's avatar
Sietse Ringers committed
18
19
// as well as updates themselves.

20
type update struct {
21
	When    irma.Timestamp
22
23
24
	Number  int
	Success bool
	Error   *string
25
26
}

27
var clientUpdates = []func(client *Client) error{
28
	// Convert old cardemu.xml Android storage to our own storage format
29
30
	func(client *Client) error {
		_, err := client.ParseAndroidStorage()
31
32
		return err
	},
33
34
35
36

	// Adding scheme manager index, signature and public key
	// Check the signatures of all scheme managers, if any is not ok,
	// copy the entire irma_configuration folder from assets
37
38
	func(client *Client) error {
		conf := client.Configuration
39
40
41
		if len(conf.DisabledSchemeManagers) > 0 {
			return conf.CopyFromAssets(true)
		}
42
43
44
		for manager := range conf.SchemeManagers {
			valid, err := conf.VerifySignature(manager)
			if err != nil || !valid {
45
				return conf.CopyFromAssets(true)
46
47
48
49
			}
		}
		return nil
	},
50
51

	// Rename config -> preferences
52
53
54
55
56
	func(client *Client) (err error) {
		exists, err := fs.PathExists(client.storage.path("config"))
		if !exists || err != nil {
			return
		}
57
58
59
60
		oldStruct := &struct {
			SendCrashReports bool
		}{}
		// Load old file, convert to new struct, and save
61
		err = client.storage.load(oldStruct, "config")
62
63
64
65
66
67
68
69
		if err != nil {
			return err
		}
		client.Preferences = Preferences{
			EnableCrashReporting: oldStruct.SendCrashReports,
		}
		return client.storage.StorePreferences(client.Preferences)
	},
70
71
72
73
74
75
76
77

	// For each keyshare server, include in its struct the identifier of its scheme manager
	func(client *Client) (err error) {
		for smi, kss := range client.keyshareServers {
			kss.SchemeManagerIdentifier = smi
		}
		return client.storage.StoreKeyshareServers(client.keyshareServers)
	},
78
}
79

80
// update performs any function from clientUpdates that has not
81
82
// already been executed in the past, keeping track of previously executed updates
// in the file at updatesFile.
83
func (client *Client) update() error {
84
85
	// Load and parse file containing info about already performed updates
	var err error
86
	if client.updates, err = client.storage.LoadUpdates(); err != nil {
87
88
89
90
		return err
	}

	// Perform all new updates
91
92
	for i := len(client.updates); i < len(clientUpdates); i++ {
		err = clientUpdates[i](client)
Sietse Ringers's avatar
Sietse Ringers committed
93
		u := update{
94
			When:    irma.Timestamp(time.Now()),
95
96
97
98
99
			Number:  i,
			Success: err == nil,
		}
		if err != nil {
			str := err.Error()
Sietse Ringers's avatar
Sietse Ringers committed
100
			u.Error = &str
101
		}
102
		client.updates = append(client.updates, u)
103
104
105
		if err != nil {
			break
		}
106
107
	}

108
109
110
111
112
	storeErr := client.storage.StoreUpdates(client.updates)
	if storeErr != nil {
		return storeErr
	}
	return err
113
114
}

Sietse Ringers's avatar
Sietse Ringers committed
115
116
117
118
// ParseAndroidStorage parses an Android cardemu.xml shared preferences file
// from the old Android IRMA app, parsing its credentials into the current instance,
// and saving them to storage.
// CAREFUL: this method overwrites any existing secret keys and attributes on storage.
119
120
func (client *Client) ParseAndroidStorage() (present bool, err error) {
	if client.androidStoragePath == "" {
121
122
123
		return false, nil
	}

124
	cardemuXML := client.androidStoragePath + "/shared_prefs/cardemu.xml"
125
	present, err = fs.PathExists(cardemuXML)
126
	if err != nil || !present {
Sietse Ringers's avatar
Sietse Ringers committed
127
128
		return
	}
129
	present = true
Sietse Ringers's avatar
Sietse Ringers committed
130

131
	bytes, err := ioutil.ReadFile(cardemuXML)
Sietse Ringers's avatar
Sietse Ringers committed
132
133
134
135
136
137
138
139
140
	if err != nil {
		return
	}
	parsedxml := struct {
		Strings []struct {
			Name    string `xml:"name,attr"`
			Content string `xml:",chardata"`
		} `xml:"string"`
	}{}
Sietse Ringers's avatar
Sietse Ringers committed
141
142
143
	if err = xml.Unmarshal(bytes, &parsedxml); err != nil {
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
144
145
146
147
148
149
150

	parsedjson := make(map[string][]*struct {
		Signature    *gabi.CLSignature `json:"signature"`
		Pk           *gabi.PublicKey   `json:"-"`
		Attributes   []*big.Int        `json:"attributes"`
		SharedPoints []*big.Int        `json:"public_sks"`
	})
151
	client.keyshareServers = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
Sietse Ringers's avatar
Sietse Ringers committed
152
153
154
155
156
157
158
159
160
	for _, xmltag := range parsedxml.Strings {
		if xmltag.Name == "credentials" {
			jsontag := html.UnescapeString(xmltag.Content)
			if err = json.Unmarshal([]byte(jsontag), &parsedjson); err != nil {
				return
			}
		}
		if xmltag.Name == "keyshare" {
			jsontag := html.UnescapeString(xmltag.Content)
161
			if err = json.Unmarshal([]byte(jsontag), &client.keyshareServers); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
162
163
164
165
166
167
168
169
170
				return
			}
		}
		if xmltag.Name == "KeyshareKeypairs" {
			jsontag := html.UnescapeString(xmltag.Content)
			keys := make([]*paillierPrivateKey, 0, 3)
			if err = json.Unmarshal([]byte(jsontag), &keys); err != nil {
				return
			}
171
			client.paillierKeyCache = keys[0]
Sietse Ringers's avatar
Sietse Ringers committed
172
173
174
175
		}
	}

	for _, list := range parsedjson {
176
		client.secretkey = &secretKey{Key: list[0].Attributes[0]}
Sietse Ringers's avatar
Sietse Ringers committed
177
178
179
180
181
182
183
184
		for _, oldcred := range list {
			gabicred := &gabi.Credential{
				Attributes: oldcred.Attributes,
				Signature:  oldcred.Signature,
			}
			if oldcred.SharedPoints != nil && len(oldcred.SharedPoints) > 0 {
				gabicred.Signature.KeyshareP = oldcred.SharedPoints[0]
			}
185
			var cred *credential
186
			if cred, err = newCredential(gabicred, client.Configuration); err != nil {
187
				return
188
			}
Sietse Ringers's avatar
Sietse Ringers committed
189
			if cred.CredentialType() == nil {
190
191
				err = errors.New("cannot add unknown credential type")
				return
Sietse Ringers's avatar
Sietse Ringers committed
192
193
			}

194
			if err = client.addCredential(cred, false); err != nil {
195
				return
Sietse Ringers's avatar
Sietse Ringers committed
196
197
198
199
			}
		}
	}

200
201
	if len(client.credentials) > 0 {
		if err = client.storage.StoreAttributes(client.attributes); err != nil {
202
			return
Sietse Ringers's avatar
Sietse Ringers committed
203
		}
204
		if err = client.storage.StoreSecretKey(client.secretkey); err != nil {
205
			return
Sietse Ringers's avatar
Sietse Ringers committed
206
207
208
		}
	}

209
210
	if len(client.keyshareServers) > 0 {
		if err = client.storage.StoreKeyshareServers(client.keyshareServers); err != nil {
211
			return
Sietse Ringers's avatar
Sietse Ringers committed
212
213
		}
	}
214
	client.UnenrolledSchemeManagers = client.unenrolledSchemeManagers()
Sietse Ringers's avatar
Sietse Ringers committed
215

216
	if err = client.storage.StorePaillierKeys(client.paillierKeyCache); err != nil {
217
		return
Sietse Ringers's avatar
Sietse Ringers committed
218
	}
219
220
	if client.paillierKeyCache == nil {
		client.paillierKey(false) // trigger calculating a new one
Sietse Ringers's avatar
Sietse Ringers committed
221
	}
222
	return
Sietse Ringers's avatar
Sietse Ringers committed
223
}