Commit 4b0c6374 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Move/rename some methods

parent 78d2cd8a
......@@ -193,7 +193,7 @@ func TestMetadataAttribute(t *testing.T) {
}
func TestMetadataCompatibility(t *testing.T) {
store := newConfigurationStore()
store := NewConfigurationStore()
require.NoError(t, store.ParseFolder("testdata/irma_configuration"))
// An actual metadata attribute of an IRMA credential extracted from the IRMA app
......@@ -214,7 +214,7 @@ func TestMetadataCompatibility(t *testing.T) {
}
func TestAttributeDisjunctionMarshaling(t *testing.T) {
store := newConfigurationStore()
store := NewConfigurationStore()
store.ParseFolder("testdata/irma_configuration")
disjunction := AttributeDisjunction{}
......
package irmago
import (
"encoding/json"
"encoding/xml"
"errors"
"html"
"io/ioutil"
"math/big"
"github.com/mhe/gabi"
)
// 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.
func (cm *CredentialManager) ParseAndroidStorage() (err error) {
exists, err := PathExists(cm.path(cardemuXML))
if err != nil {
return
}
if !exists {
return errors.New("cardemu.xml not found at " + cardemuXML)
}
bytes, err := ioutil.ReadFile(cm.path(cardemuXML))
if err != nil {
return
}
parsedxml := struct {
Strings []struct {
Name string `xml:"name,attr"`
Content string `xml:",chardata"`
} `xml:"string"`
}{}
xml.Unmarshal(bytes, &parsedxml)
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"`
})
cm.keyshareServers = make(map[SchemeManagerIdentifier]*keyshareServer)
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)
if err = json.Unmarshal([]byte(jsontag), &cm.keyshareServers); err != nil {
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
}
cm.paillierKeyCache = keys[0]
}
}
for _, list := range parsedjson {
cm.secretkey = list[0].Attributes[0]
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]
}
cred := newCredential(gabicred, cm.Store)
if cred.CredentialType() == nil {
return errors.New("cannot add unknown credential type")
}
err = cm.addCredential(cred, false)
if err != nil {
return err
}
}
}
if len(cm.credentials) > 0 {
err = cm.storeAttributes()
if err != nil {
return err
}
err = cm.storeSecretKey(cm.secretkey)
if err != nil {
return err
}
}
if len(cm.keyshareServers) > 0 {
err = cm.storeKeyshareServers()
if err != nil {
return err
}
}
err = cm.storePaillierKeys()
if err != nil {
return err
}
if cm.paillierKeyCache == nil {
cm.paillierKey(false) // trigger calculating a new one
}
return
}
......@@ -2,9 +2,7 @@ package irmago
import (
"encoding/json"
"encoding/xml"
"errors"
"html"
"io/ioutil"
"os"
"strconv"
......@@ -51,7 +49,7 @@ func NewCredentialManager(
keyshareServers: make(map[SchemeManagerIdentifier]*keyshareServer),
}
cm.Store = newConfigurationStore()
cm.Store = NewConfigurationStore()
if err = cm.Store.ParseFolder(irmaConfigurationPath); err != nil {
return nil, err
}
......@@ -90,112 +88,6 @@ func NewCredentialManager(
return cm, nil
}
// 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.
func (cm *CredentialManager) ParseAndroidStorage() (err error) {
exists, err := PathExists(cm.path(cardemuXML))
if err != nil {
return
}
if !exists {
return errors.New("cardemu.xml not found at " + cardemuXML)
}
bytes, err := ioutil.ReadFile(cm.path(cardemuXML))
if err != nil {
return
}
parsedxml := struct {
Strings []struct {
Name string `xml:"name,attr"`
Content string `xml:",chardata"`
} `xml:"string"`
}{}
xml.Unmarshal(bytes, &parsedxml)
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"`
})
cm.keyshareServers = make(map[SchemeManagerIdentifier]*keyshareServer)
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)
if err = json.Unmarshal([]byte(jsontag), &cm.keyshareServers); err != nil {
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
}
cm.paillierKeyCache = keys[0]
}
}
for _, list := range parsedjson {
cm.secretkey = list[0].Attributes[0]
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]
}
cred := newCredential(gabicred, cm.Store)
if cred.CredentialType() == nil {
return errors.New("cannot add unknown credential type")
}
err = cm.addCredential(cred, false)
if err != nil {
return err
}
}
}
if len(cm.credentials) > 0 {
err = cm.storeAttributes()
if err != nil {
return err
}
err = cm.storeSecretKey(cm.secretkey)
if err != nil {
return err
}
}
if len(cm.keyshareServers) > 0 {
err = cm.storeKeyshareServers()
if err != nil {
return err
}
}
err = cm.storePaillierKeys()
if err != nil {
return err
}
if cm.paillierKeyCache == nil {
cm.paillierKey(false) // trigger calculating a new one
}
return
}
func (cm *CredentialManager) path(file string) string {
return cm.storagePath + "/" + file
}
......
......@@ -13,7 +13,6 @@ import (
)
// ConfigurationStore keeps track of scheme managers, issuers, credential types and public keys.
// Use the global MetaStore instance.
type ConfigurationStore struct {
SchemeManagers map[SchemeManagerIdentifier]*SchemeManager
Issuers map[IssuerIdentifier]*Issuer
......@@ -24,7 +23,7 @@ type ConfigurationStore struct {
initialized bool
}
func newConfigurationStore() (store *ConfigurationStore) {
func NewConfigurationStore() (store *ConfigurationStore) {
store = &ConfigurationStore{
SchemeManagers: make(map[SchemeManagerIdentifier]*SchemeManager),
Issuers: make(map[IssuerIdentifier]*Issuer),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment