updates.go 5.38 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
28
29
var clientUpdates = []func(client *Client) error{
	func(client *Client) error {
		_, err := client.ParseAndroidStorage()
30
31
		return err
	},
32
33
34
35
36
	func(client *Client) error {
		// 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
		conf := client.Configuration
37
38
39
		if len(conf.DisabledSchemeManagers) > 0 {
			return conf.CopyFromAssets(true)
		}
40
41
42
		for manager := range conf.SchemeManagers {
			valid, err := conf.VerifySignature(manager)
			if err != nil || !valid {
43
				return conf.CopyFromAssets(true)
44
45
46
47
			}
		}
		return nil
	},
48
49
50
51
52
53
54
55
56
57
58
59
60
61
	func(client *Client) error {
		oldStruct := &struct {
			SendCrashReports bool
		}{}
		// Load old file, convert to new struct, and save
		err := client.storage.load(oldStruct, "config")
		if err != nil {
			return err
		}
		client.Preferences = Preferences{
			EnableCrashReporting: oldStruct.SendCrashReports,
		}
		return client.storage.StorePreferences(client.Preferences)
	},
62
}
63

64
// update performs any function from clientUpdates that has not
65
66
// already been executed in the past, keeping track of previously executed updates
// in the file at updatesFile.
67
func (client *Client) update() error {
68
69
	// Load and parse file containing info about already performed updates
	var err error
70
	if client.updates, err = client.storage.LoadUpdates(); err != nil {
71
72
73
74
		return err
	}

	// Perform all new updates
75
76
	for i := len(client.updates); i < len(clientUpdates); i++ {
		err = clientUpdates[i](client)
Sietse Ringers's avatar
Sietse Ringers committed
77
		u := update{
78
			When:    irma.Timestamp(time.Now()),
79
80
81
82
83
			Number:  i,
			Success: err == nil,
		}
		if err != nil {
			str := err.Error()
Sietse Ringers's avatar
Sietse Ringers committed
84
			u.Error = &str
85
		}
86
		client.updates = append(client.updates, u)
87
88
89
		if err != nil {
			break
		}
90
91
	}

92
93
94
95
96
	storeErr := client.storage.StoreUpdates(client.updates)
	if storeErr != nil {
		return storeErr
	}
	return err
97
98
}

Sietse Ringers's avatar
Sietse Ringers committed
99
100
101
102
// 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.
103
104
func (client *Client) ParseAndroidStorage() (present bool, err error) {
	if client.androidStoragePath == "" {
105
106
107
		return false, nil
	}

108
	cardemuXML := client.androidStoragePath + "/shared_prefs/cardemu.xml"
109
	present, err = fs.PathExists(cardemuXML)
110
	if err != nil || !present {
Sietse Ringers's avatar
Sietse Ringers committed
111
112
		return
	}
113
	present = true
Sietse Ringers's avatar
Sietse Ringers committed
114

115
	bytes, err := ioutil.ReadFile(cardemuXML)
Sietse Ringers's avatar
Sietse Ringers committed
116
117
118
119
120
121
122
123
124
	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
125
126
127
	if err = xml.Unmarshal(bytes, &parsedxml); err != nil {
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
128
129
130
131
132
133
134

	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"`
	})
135
	client.keyshareServers = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
Sietse Ringers's avatar
Sietse Ringers committed
136
137
138
139
140
141
142
143
144
	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)
145
			if err = json.Unmarshal([]byte(jsontag), &client.keyshareServers); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
146
147
148
149
150
151
152
153
154
				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
			}
155
			client.paillierKeyCache = keys[0]
Sietse Ringers's avatar
Sietse Ringers committed
156
157
158
159
		}
	}

	for _, list := range parsedjson {
160
		client.secretkey = &secretKey{Key: list[0].Attributes[0]}
Sietse Ringers's avatar
Sietse Ringers committed
161
162
163
164
165
166
167
168
		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]
			}
169
			var cred *credential
170
			if cred, err = newCredential(gabicred, client.Configuration); err != nil {
171
				return
172
			}
Sietse Ringers's avatar
Sietse Ringers committed
173
			if cred.CredentialType() == nil {
174
175
				err = errors.New("cannot add unknown credential type")
				return
Sietse Ringers's avatar
Sietse Ringers committed
176
177
			}

178
			if err = client.addCredential(cred, false); err != nil {
179
				return
Sietse Ringers's avatar
Sietse Ringers committed
180
181
182
183
			}
		}
	}

184
185
	if len(client.credentials) > 0 {
		if err = client.storage.StoreAttributes(client.attributes); err != nil {
186
			return
Sietse Ringers's avatar
Sietse Ringers committed
187
		}
188
		if err = client.storage.StoreSecretKey(client.secretkey); err != nil {
189
			return
Sietse Ringers's avatar
Sietse Ringers committed
190
191
192
		}
	}

193
194
	if len(client.keyshareServers) > 0 {
		if err = client.storage.StoreKeyshareServers(client.keyshareServers); err != nil {
195
			return
Sietse Ringers's avatar
Sietse Ringers committed
196
197
		}
	}
198
	client.UnenrolledSchemeManagers = client.unenrolledSchemeManagers()
Sietse Ringers's avatar
Sietse Ringers committed
199

200
	if err = client.storage.StorePaillierKeys(client.paillierKeyCache); err != nil {
201
		return
Sietse Ringers's avatar
Sietse Ringers committed
202
	}
203
204
	if client.paillierKeyCache == nil {
		client.paillierKey(false) // trigger calculating a new one
Sietse Ringers's avatar
Sietse Ringers committed
205
	}
206
	return
Sietse Ringers's avatar
Sietse Ringers committed
207
}