updates.go 4.46 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
32
		return err
	},
}
33

34
// update performs any function from clientUpdates that has not
35
36
// already been executed in the past, keeping track of previously executed updates
// in the file at updatesFile.
37
func (client *Client) update() error {
38
39
	// Load and parse file containing info about already performed updates
	var err error
40
	if client.updates, err = client.storage.LoadUpdates(); err != nil {
41
42
43
44
		return err
	}

	// Perform all new updates
45
46
	for i := len(client.updates); i < len(clientUpdates); i++ {
		err = clientUpdates[i](client)
Sietse Ringers's avatar
Sietse Ringers committed
47
		u := update{
48
			When:    irma.Timestamp(time.Now()),
49
50
51
52
53
			Number:  i,
			Success: err == nil,
		}
		if err != nil {
			str := err.Error()
Sietse Ringers's avatar
Sietse Ringers committed
54
			u.Error = &str
55
		} // TODO: err is only stored but not passed on!
56
		client.updates = append(client.updates, u)
57
58
	}

59
	return client.storage.StoreUpdates(client.updates)
60
61
}

Sietse Ringers's avatar
Sietse Ringers committed
62
63
64
65
// 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.
66
67
func (client *Client) ParseAndroidStorage() (present bool, err error) {
	if client.androidStoragePath == "" {
68
69
70
		return false, nil
	}

71
	cardemuXML := client.androidStoragePath + "/shared_prefs/cardemu.xml"
72
	present, err = fs.PathExists(cardemuXML)
73
	if err != nil || !present {
Sietse Ringers's avatar
Sietse Ringers committed
74
75
		return
	}
76
	present = true
Sietse Ringers's avatar
Sietse Ringers committed
77

78
	bytes, err := ioutil.ReadFile(cardemuXML)
Sietse Ringers's avatar
Sietse Ringers committed
79
80
81
82
83
84
85
86
87
	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
88
89
90
	if err = xml.Unmarshal(bytes, &parsedxml); err != nil {
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
91
92
93
94
95
96
97

	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"`
	})
98
	client.keyshareServers = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
Sietse Ringers's avatar
Sietse Ringers committed
99
100
101
102
103
104
105
106
107
	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)
108
			if err = json.Unmarshal([]byte(jsontag), &client.keyshareServers); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
109
110
111
112
113
114
115
116
117
				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
			}
118
			client.paillierKeyCache = keys[0]
Sietse Ringers's avatar
Sietse Ringers committed
119
120
121
122
		}
	}

	for _, list := range parsedjson {
123
		client.secretkey = &secretKey{Key: list[0].Attributes[0]}
Sietse Ringers's avatar
Sietse Ringers committed
124
125
126
127
128
129
130
131
		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]
			}
132
			var cred *credential
133
			if cred, err = newCredential(gabicred, client.Configuration); err != nil {
134
				return
135
			}
Sietse Ringers's avatar
Sietse Ringers committed
136
			if cred.CredentialType() == nil {
137
138
				err = errors.New("cannot add unknown credential type")
				return
Sietse Ringers's avatar
Sietse Ringers committed
139
140
			}

141
			if err = client.addCredential(cred, false); err != nil {
142
				return
Sietse Ringers's avatar
Sietse Ringers committed
143
144
145
146
			}
		}
	}

147
148
	if len(client.credentials) > 0 {
		if err = client.storage.StoreAttributes(client.attributes); err != nil {
149
			return
Sietse Ringers's avatar
Sietse Ringers committed
150
		}
151
		if err = client.storage.StoreSecretKey(client.secretkey); err != nil {
152
			return
Sietse Ringers's avatar
Sietse Ringers committed
153
154
155
		}
	}

156
157
	if len(client.keyshareServers) > 0 {
		if err = client.storage.StoreKeyshareServers(client.keyshareServers); err != nil {
158
			return
Sietse Ringers's avatar
Sietse Ringers committed
159
160
		}
	}
161
	client.UnenrolledSchemeManagers = client.unenrolledSchemeManagers()
Sietse Ringers's avatar
Sietse Ringers committed
162

163
	if err = client.storage.StorePaillierKeys(client.paillierKeyCache); err != nil {
164
		return
Sietse Ringers's avatar
Sietse Ringers committed
165
	}
166
167
	if client.paillierKeyCache == nil {
		client.paillierKey(false) // trigger calculating a new one
Sietse Ringers's avatar
Sietse Ringers committed
168
	}
169
	return
Sietse Ringers's avatar
Sietse Ringers committed
170
}