/
verification.go
241 lines (196 loc) · 6.35 KB
/
verification.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
Copyright 2023 Codenotary Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package verification
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/sha256"
"fmt"
"github.com/codenotary/immudb/embedded/document"
"github.com/codenotary/immudb/embedded/htree"
"github.com/codenotary/immudb/embedded/sql"
"github.com/codenotary/immudb/embedded/store"
"github.com/codenotary/immudb/pkg/api/protomodel"
"github.com/codenotary/immudb/pkg/api/schema"
structpb "github.com/golang/protobuf/ptypes/struct"
"google.golang.org/protobuf/proto"
)
func VerifyDocument(ctx context.Context,
proof *protomodel.ProofDocumentResponse,
doc *structpb.Struct,
knownState *schema.ImmutableState,
serverSigningPubKey *ecdsa.PublicKey,
) (*schema.ImmutableState, error) {
if proof == nil || doc == nil {
return nil, store.ErrIllegalArguments
}
docID, ok := doc.Fields[proof.DocumentIdFieldName]
if !ok {
return nil, fmt.Errorf("%w: missing field '%s'", store.ErrIllegalArguments, proof.DocumentIdFieldName)
}
encDocKey, err := encodedKeyForDocument(proof.CollectionId, docID.GetStringValue())
if err != nil {
return nil, err
}
var keyFound int
for _, txEntry := range proof.VerifiableTx.Tx.Entries {
if bytes.Equal(txEntry.Key, encDocKey) {
hVal := sha256.Sum256(proof.EncodedDocument)
if !bytes.Equal(hVal[:], txEntry.HValue) {
return nil, store.ErrInvalidProof
}
keyFound++
}
}
if keyFound != 1 {
return nil, fmt.Errorf("%w: document entry was not found or it was found multiple times", store.ErrInvalidProof)
}
voff := sql.EncLenLen + sql.EncIDLen
// DocumentIDField
_, n, err := sql.DecodeValue(proof.EncodedDocument[voff:], sql.BLOBType)
if err != nil {
return nil, err
}
if n > document.MaxDocumentIDLength {
return nil, fmt.Errorf("%w: the proof contains invalid document data", store.ErrInvalidProof)
}
voff += n + sql.EncIDLen
// DocumentBLOBField
encodedDoc, _, err := sql.DecodeValue(proof.EncodedDocument[voff:], sql.BLOBType)
if err != nil {
return nil, err
}
proofDoc := &structpb.Struct{}
err = proto.Unmarshal(encodedDoc.RawValue().([]byte), proofDoc)
if err != nil {
return nil, err
}
if !proto.Equal(doc, proofDoc) {
return nil, fmt.Errorf("%w: proof is not valid for provided document", store.ErrInvalidProof)
}
entries := proof.VerifiableTx.Tx.Entries
htree, err := htree.New(len(entries))
if err != nil {
return nil, err
}
entrySpecDigest, err := store.EntrySpecDigestFor(int(proof.VerifiableTx.Tx.Header.Version))
if err != nil {
return nil, err
}
digests := make([][sha256.Size]byte, len(entries))
for i, e := range entries {
eSpec := &store.EntrySpec{
Key: e.Key,
Metadata: schema.KVMetadataFromProto(e.Metadata),
HashValue: schema.DigestFromProto(e.HValue),
IsValueTruncated: true,
}
digests[i] = entrySpecDigest(eSpec)
}
err = htree.BuildWith(digests)
if err != nil {
return nil, err
}
txHdr := schema.TxHeaderFromProto(proof.VerifiableTx.Tx.Header)
if htree.Root() != txHdr.Eh {
return nil, store.ErrInvalidProof
}
dualProof := schema.DualProofV2FromProto(proof.VerifiableTx.DualProof)
sourceID := proof.VerifiableTx.DualProof.SourceTxHeader.Id
targetID := proof.VerifiableTx.DualProof.TargetTxHeader.Id
if targetID < sourceID {
return nil, fmt.Errorf("%w: source tx is newer than target tx", store.ErrInvalidProof)
}
sourceAlh := schema.TxHeaderFromProto(proof.VerifiableTx.DualProof.SourceTxHeader).Alh()
targetAlh := schema.TxHeaderFromProto(proof.VerifiableTx.DualProof.TargetTxHeader).Alh()
if txHdr.ID == sourceID {
if txHdr.Alh() != sourceAlh {
return nil, fmt.Errorf("%w: tx must match source or target tx headers", store.ErrInvalidProof)
}
} else if txHdr.ID == targetID {
if txHdr.Alh() != targetAlh {
return nil, fmt.Errorf("%w: tx must match source or target tx headers", store.ErrInvalidProof)
}
} else {
return nil, fmt.Errorf("%w: tx must match source or target tx headers", store.ErrInvalidProof)
}
if knownState == nil || knownState.TxId == 0 {
if sourceID != 1 {
return nil, fmt.Errorf("%w: proof should start from the first transaction when no previous state was specified", store.ErrInvalidProof)
}
} else {
if knownState.TxId == sourceID {
if !bytes.Equal(knownState.TxHash, sourceAlh[:]) {
return nil, fmt.Errorf("%w: knownState alh must match source or target tx alh", store.ErrInvalidProof)
}
} else if knownState.TxId == targetID {
if !bytes.Equal(knownState.TxHash, targetAlh[:]) {
return nil, fmt.Errorf("%w: knownState alh must match source or target tx alh", store.ErrInvalidProof)
}
} else {
return nil, fmt.Errorf("%w: knownState alh must match source or target tx alh", store.ErrInvalidProof)
}
}
err = store.VerifyDualProofV2(
dualProof,
sourceID,
targetID,
sourceAlh,
targetAlh,
)
if err != nil {
return nil, err
}
state := &schema.ImmutableState{
Db: proof.Database,
TxId: targetID,
TxHash: targetAlh[:],
Signature: proof.VerifiableTx.Signature,
}
if serverSigningPubKey != nil {
ok, err := state.CheckSignature(serverSigningPubKey)
if err != nil {
return nil, err
}
if !ok {
return nil, store.ErrInvalidProof
}
}
return state, nil
}
func encodedKeyForDocument(collectionID uint32, documentID string) ([]byte, error) {
docID, err := document.NewDocumentIDFromHexEncodedString(documentID)
if err != nil {
return nil, err
}
valbuf := bytes.Buffer{}
rval := sql.NewBlob(docID[:])
encVal, _, err := sql.EncodeRawValueAsKey(rval.RawValue(), sql.BLOBType, document.MaxDocumentIDLength)
if err != nil {
return nil, err
}
_, err = valbuf.Write(encVal)
if err != nil {
return nil, err
}
pkEncVals := valbuf.Bytes()
return sql.MapKey(
[]byte{3}, // database.DocumentPrefix
sql.PIndexPrefix,
sql.EncodeID(1), // fixed database identifier
sql.EncodeID(collectionID),
sql.EncodeID(0), // pk index id
pkEncVals,
), nil
}