-
Notifications
You must be signed in to change notification settings - Fork 57
/
encryption.go
168 lines (139 loc) · 4.14 KB
/
encryption.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
// Copyright 2020 Readium Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
package xmlenc
import (
"encoding/xml"
"io"
"net/url"
"strings"
"golang.org/x/net/html/charset"
)
type Manifest struct {
//Keys []Key
Data []Data `xml:"http://www.w3.org/2001/04/xmlenc# EncryptedData"`
XMLName struct{} `xml:"urn:oasis:names:tc:opendocument:xmlns:container encryption"`
}
// ResourcePathEscape encodes a path for a URL but keep slashes unescaped
func ResourcePathEscape(path string) string {
segments := strings.Split(path, "/")
// Encode each segment individually
for i, segment := range segments {
segments[i] = url.PathEscape(segment)
}
// Join the encoded segments with slashes
return strings.Join(segments, "/")
}
// DataForFile returns the EncryptedData item corresponding to a given path
func (m Manifest) DataForFile(path string) (Data, bool) {
uri := URI(ResourcePathEscape(path))
for _, datum := range m.Data {
if datum.CipherData.CipherReference.URI == uri {
return datum, true
}
}
return Data{}, false
}
// Write writes the encryption XML structure
func (m Manifest) Write(w io.Writer) error {
w.Write([]byte(xml.Header))
enc := xml.NewEncoder(w)
enc.Indent("", " ")
return enc.Encode(m)
}
// Read parses the encryption XML structure
func Read(r io.Reader) (Manifest, error) {
var m Manifest
dec := xml.NewDecoder(r)
// deal with non utf-8 xml files
dec.CharsetReader = charset.NewReaderLabel
err := dec.Decode(&m)
return m, err
}
//<sequence>
//<element name="EncryptionMethod" type="xenc:EncryptionMethodType"
//minOccurs="0"/>
//<element ref="ds:KeyInfo" minOccurs="0"/>
//<element ref="xenc:CipherData"/>
//<element ref="xenc:EncryptionProperties" minOccurs="0"/>
//</sequence>
//<attribute name="Id" type="ID" use="optional"/>
//<attribute name="Type" type="anyURI" use="optional"/>
//<attribute name="MimeType" type="string" use="optional"/>
//<attribute name="Encoding" type="anyURI" use="optional"/>
type URI string
type Method struct {
KeySize int `xml:"KeySize,omitempty"`
//OAEPParams []byte `xml:"AOEParams,omitempty"`
Algorithm URI `xml:"Algorithm,attr,omitempty"`
}
type CipherReference struct {
URI URI `xml:"URI,attr"`
}
type CipherData struct {
CipherReference CipherReference `xml:"http://www.w3.org/2001/04/xmlenc# CipherReference"`
Value []byte `xml:"Value,omitempty"`
}
//type DSAKeyValue struct {
//P []byte
//Q []byte
//G []byte
//Y []byte
//J []byte
//Seed []byte
//PgenCounter []byte
//}
//type RSAKeyValue struct {
//Modulus []byte
//Exponent []byte
//}
//type KeyValue struct {
//DSAKeyValue
//RSAKeyValue
//}
type RetrievalMethod struct {
URI `xml:"URI,attr"`
Type string `xml:"Type,attr"`
}
type KeyInfo struct {
KeyName string `xml:"KeyName,attr,omitempty"`
//KeyValue
RetrievalMethod RetrievalMethod `xml:"http://www.w3.org/2000/09/xmldsig# RetrievalMethod"`
//X509Data
//PGPData
//SPKIData
//MgmtData
}
type encryptedType struct {
Method Method `xml:"http://www.w3.org/2001/04/xmlenc# EncryptionMethod"`
KeyInfo *KeyInfo `xml:"http://www.w3.org/2000/09/xmldsig# KeyInfo"`
CipherData CipherData `xml:"http://www.w3.org/2001/04/xmlenc# CipherData"`
ID string `xml:"Id,attr,omitempty"`
Type URI `xml:"Type,attr,omitempty"`
MimeType string `xml:"MimeType,omitempty"`
Encoding URI `xml:"Encoding,omitempty"`
}
type ReferenceList struct {
Key []string
Data []string
}
type Key struct {
encryptedType
References ReferenceList
CarriedKeyName string
Recipient string
}
type Compression struct {
Method int `xml:"Method,attr"`
OriginalLength uint64 `xml:"OriginalLength,attr"`
}
type EncryptionProperty struct {
Compression Compression `xml:"http://www.idpf.org/2016/encryption#compression Compression"`
}
type EncryptionProperties struct {
Properties []EncryptionProperty `xml:"http://www.w3.org/2001/04/xmlenc# EncryptionProperty"`
}
type Data struct {
encryptedType
Properties *EncryptionProperties `xml:"http://www.w3.org/2001/04/xmlenc# EncryptionProperties,omitempty"`
}