updates.go 4.88 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

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

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
37
38
39
40
41
42
43
44
	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
		for manager := range conf.SchemeManagers {
			valid, err := conf.VerifySignature(manager)
			if err != nil || !valid {
				return conf.CopyFromAssets(false)
			}
		}
		return nil
	},
45
}
46

47
// update performs any function from clientUpdates that has not
48
49
// already been executed in the past, keeping track of previously executed updates
// in the file at updatesFile.
50
func (client *Client) update() error {
51
52
	// Load and parse file containing info about already performed updates
	var err error
53
	if client.updates, err = client.storage.LoadUpdates(); err != nil {
54
55
56
57
		return err
	}

	// Perform all new updates
58
59
	for i := len(client.updates); i < len(clientUpdates); i++ {
		err = clientUpdates[i](client)
Sietse Ringers's avatar
Sietse Ringers committed
60
		u := update{
61
			When:    irma.Timestamp(time.Now()),
62
63
64
65
66
			Number:  i,
			Success: err == nil,
		}
		if err != nil {
			str := err.Error()
Sietse Ringers's avatar
Sietse Ringers committed
67
			u.Error = &str
68
		} // TODO: err is only stored but not passed on!
69
		client.updates = append(client.updates, u)
70
71
	}

72
	return client.storage.StoreUpdates(client.updates)
73
74
}

Sietse Ringers's avatar
Sietse Ringers committed
75
76
77
78
// 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.
79
80
func (client *Client) ParseAndroidStorage() (present bool, err error) {
	if client.androidStoragePath == "" {
81
82
83
		return false, nil
	}

84
	cardemuXML := client.androidStoragePath + "/shared_prefs/cardemu.xml"
85
	present, err = fs.PathExists(cardemuXML)
86
	if err != nil || !present {
Sietse Ringers's avatar
Sietse Ringers committed
87
88
		return
	}
89
	present = true
Sietse Ringers's avatar
Sietse Ringers committed
90

91
	bytes, err := ioutil.ReadFile(cardemuXML)
Sietse Ringers's avatar
Sietse Ringers committed
92
93
94
95
96
97
98
99
100
	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
101
102
103
	if err = xml.Unmarshal(bytes, &parsedxml); err != nil {
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
104
105
106
107
108
109
110

	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"`
	})
111
	client.keyshareServers = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
Sietse Ringers's avatar
Sietse Ringers committed
112
113
114
115
116
117
118
119
120
	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)
121
			if err = json.Unmarshal([]byte(jsontag), &client.keyshareServers); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
122
123
124
125
126
127
128
129
130
				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
			}
131
			client.paillierKeyCache = keys[0]
Sietse Ringers's avatar
Sietse Ringers committed
132
133
134
135
		}
	}

	for _, list := range parsedjson {
136
		client.secretkey = &secretKey{Key: list[0].Attributes[0]}
Sietse Ringers's avatar
Sietse Ringers committed
137
138
139
140
141
142
143
144
		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]
			}
145
			var cred *credential
146
			if cred, err = newCredential(gabicred, client.Configuration); err != nil {
147
				return
148
			}
Sietse Ringers's avatar
Sietse Ringers committed
149
			if cred.CredentialType() == nil {
150
151
				err = errors.New("cannot add unknown credential type")
				return
Sietse Ringers's avatar
Sietse Ringers committed
152
153
			}

154
			if err = client.addCredential(cred, false); err != nil {
155
				return
Sietse Ringers's avatar
Sietse Ringers committed
156
157
158
159
			}
		}
	}

160
161
	if len(client.credentials) > 0 {
		if err = client.storage.StoreAttributes(client.attributes); err != nil {
162
			return
Sietse Ringers's avatar
Sietse Ringers committed
163
		}
164
		if err = client.storage.StoreSecretKey(client.secretkey); err != nil {
165
			return
Sietse Ringers's avatar
Sietse Ringers committed
166
167
168
		}
	}

169
170
	if len(client.keyshareServers) > 0 {
		if err = client.storage.StoreKeyshareServers(client.keyshareServers); err != nil {
171
			return
Sietse Ringers's avatar
Sietse Ringers committed
172
173
		}
	}
174
	client.UnenrolledSchemeManagers = client.unenrolledSchemeManagers()
Sietse Ringers's avatar
Sietse Ringers committed
175

176
	if err = client.storage.StorePaillierKeys(client.paillierKeyCache); err != nil {
177
		return
Sietse Ringers's avatar
Sietse Ringers committed
178
	}
179
180
	if client.paillierKeyCache == nil {
		client.paillierKey(false) // trigger calculating a new one
Sietse Ringers's avatar
Sietse Ringers committed
181
	}
182
	return
Sietse Ringers's avatar
Sietse Ringers committed
183
}