/
meta.go
102 lines (90 loc) · 3.02 KB
/
meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package cmd
import (
"encoding/base64"
"encoding/json"
"fmt"
"time"
"github.com/go-errors/errors"
irma "github.com/markuskreukniet/irmago-measurements"
"github.com/markuskreukniet/irmago-measurements/internal/common"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/gabi/big"
"github.com/spf13/cobra"
)
// metaCmd represents the meta command
var metaCmd = &cobra.Command{
Use: "meta <attribute>",
Short: "Parse an IRMA metadata attribute and print its contents",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
confpath, err := cmd.Flags().GetString("irmaconf")
if err != nil {
die("Failed to get irma_configuration flag", err)
}
metaint := new(big.Int)
_, ok := metaint.SetString(args[0], 10)
if !ok {
// Not a base-10 integer, try to parse as base64. This is safe:
// Since the first byte of a metadata attribute is its version, currently 0x03,
// the first letter of any baase64'd metadata attribute will be 'A'. So it can never happen
// that a base64'd metadata attribute consists of only digits.
bts, err := base64.StdEncoding.DecodeString(args[0])
if err != nil {
return errors.WrapPrefix(err, "Could not parse argument as decimal or base64 integer", 0)
}
metaint.SetBytes(bts)
}
if err := printMetadataAttr(metaint, confpath); err != nil {
die("", err)
}
return nil
},
}
func printMetadataAttr(metaint *big.Int, confpath string) error {
if err := common.AssertPathExists(confpath); err != nil {
return errors.WrapPrefix(err, "Cannot read irma_configuration", 0)
}
conf, err := irma.NewConfiguration(confpath, irma.ConfigurationOptions{ReadOnly: true})
if err != nil {
return errors.WrapPrefix(err, "Failed to parse irma_configuration", 0)
}
err = conf.ParseFolder()
if err != nil {
return errors.WrapPrefix(err, "Failed to parse irma_configuration", 0)
}
meta := irma.MetadataFromInt(metaint, conf)
typ := meta.CredentialType()
var key *gabi.PublicKey
if typ == nil {
fmt.Println("Unknown credential type, hash:", base64.StdEncoding.EncodeToString(meta.CredentialTypeHash()))
} else {
fmt.Println("Identifier :", typ.Identifier())
key, err = meta.PublicKey()
if err != nil {
fmt.Println("Failed to parse public key", err)
}
}
fmt.Println("Signed :", meta.SigningDate().String())
fmt.Println("Expires :", meta.Expiry().String())
fmt.Println("IsValid :", meta.IsValid())
fmt.Println("Version :", meta.Version())
fmt.Println("KeyCounter :", meta.KeyCounter())
if key != nil {
fmt.Println("KeyExpires :", time.Unix(key.ExpiryDate, 0))
fmt.Println("KeyModulusBitlen:", key.N.BitLen())
}
fmt.Println()
fmt.Println("CredentialType :", prettyprint(typ))
return nil
}
func prettyprint(ob interface{}) string {
b, err := json.MarshalIndent(ob, "", " ")
if err != nil {
fmt.Println("error:", err)
}
return string(b)
}
func init() {
RootCmd.AddCommand(metaCmd)
metaCmd.Flags().StringP("irmaconf", "i", irma.DefaultSchemesPath(), "path to irma_configuration")
}