-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathaudit.go
More file actions
124 lines (105 loc) · 3.3 KB
/
Copy pathaudit.go
File metadata and controls
124 lines (105 loc) · 3.3 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package crypto
import (
"context"
csp "github.com/IBM/idemix/bccsp/types"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/proto"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/encoding/json"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/idemix/schema"
)
// Schema represents the version identifier for the credential schema.
type Schema = string
// AuditInfo contains cryptographic audit data for an Idemix identity.
type AuditInfo struct {
// Enrollment ID pseudonym audit data
EidNymAuditData *csp.AttrNymAuditData
// Revocation handle pseudonym audit data
RhNymAuditData *csp.AttrNymAuditData
// Credential attributes (e.g. EnrollmentID, RevocationHandle strings)
Attributes [][]byte
// Cryptographic service provider
Csp csp.BCCSP `json:"-"`
// Credential issuer's public key
IssuerPublicKey csp.Key `json:"-"`
// Schema-specific operations manager
SchemaManager schema.Manager `json:"-"`
// Credential schema version
Schema string
}
// Bytes serializes the AuditInfo to JSON format.
func (a *AuditInfo) Bytes() ([]byte, error) {
return json.Marshal(a)
}
// FromBytes deserializes the AuditInfo from JSON format.
func (a *AuditInfo) FromBytes(raw []byte) error {
return json.Unmarshal(raw, a)
}
// EnrollmentID returns the enrollment ID from Attributes[2].
func (a *AuditInfo) EnrollmentID() string {
return string(a.Attributes[2])
}
// RevocationHandle returns the revocation handle from Attributes[3].
func (a *AuditInfo) RevocationHandle() string {
return string(a.Attributes[3])
}
// Match verifies the identity matches this audit info by checking EID and RH pseudonyms.
func (a *AuditInfo) Match(_ context.Context, id []byte) error {
serialized := new(SerializedIdemixIdentity)
err := proto.Unmarshal(id, serialized)
if err != nil {
return errors.Wrap(err, "could not deserialize a SerializedIdemixIdentity")
}
eidAuditOpts, err := a.SchemaManager.EidNymAuditOpts(a.Schema, a.Attributes)
if err != nil {
return errors.Wrap(err, "error while getting a RhNymAuditOpts")
}
eidAuditOpts.RNymEid = a.EidNymAuditData.Rand
// Audit EID
valid, err := a.Csp.Verify(
a.IssuerPublicKey,
serialized.Proof,
nil,
eidAuditOpts,
)
if err != nil {
return errors.Wrap(err, "error while verifying the nym eid")
}
if !valid {
return errors.New("invalid nym rh")
}
rhAuditOpts, err := a.SchemaManager.RhNymAuditOpts(a.Schema, a.Attributes)
if err != nil {
return errors.Wrap(err, "error while getting a RhNymAuditOpts")
}
rhAuditOpts.RNymRh = a.RhNymAuditData.Rand
// Audit RH
valid, err = a.Csp.Verify(
a.IssuerPublicKey,
serialized.Proof,
nil,
rhAuditOpts,
)
if err != nil {
return errors.Wrap(err, "error while verifying the nym rh")
}
if !valid {
return errors.New("invalid nym eid")
}
return nil
}
// DeserializeAuditInfo deserializes the audit information from JSON.
func DeserializeAuditInfo(raw []byte) (*AuditInfo, error) {
auditInfo := &AuditInfo{}
err := auditInfo.FromBytes(raw)
if err != nil {
return nil, err
}
if len(auditInfo.Attributes) == 0 {
return nil, errors.Errorf("failed to unmarshal, no attributes found [%s]", string(raw))
}
return auditInfo, nil
}