-
Notifications
You must be signed in to change notification settings - Fork 21
/
xml.go
227 lines (185 loc) · 5.24 KB
/
xml.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
package dsa
import (
"errors"
"math/big"
"crypto/dsa"
"encoding/xml"
"encoding/base64"
)
var (
errPublicKeyXMLValue = func(name string) error {
return errors.New("dsa xml: public key [" + name + "] value is error")
}
errPrivateKeyXMLValue = func(name string) error {
return errors.New("dsa xml: private key [" + name + "] value is error")
}
)
// 私钥
type xmlPrivateKey struct {
XMLName xml.Name `xml:"DSAKeyValue"`
P string `xml:"P"`
Q string `xml:"Q"`
G string `xml:"G"`
Y string `xml:"Y"`
J string `xml:"J,omitempty"`
Seed string `xml:"Seed,omitempty"`
PgenCounter string `xml:"PgenCounter,omitempty"`
X string `xml:"X"`
}
// 公钥
type xmlPublicKey struct {
XMLName xml.Name `xml:"DSAKeyValue"`
P string `xml:"P"`
Q string `xml:"Q"`
G string `xml:"G"`
Y string `xml:"Y"`
J string `xml:"J,omitempty"`
Seed string `xml:"Seed,omitempty"`
PgenCounter string `xml:"PgenCounter,omitempty"`
}
var defaultXMLKey = NewXMLKey()
/**
* dsa xml密钥
*
* @create 2023-6-5
* @author deatil
*/
type XMLKey struct {}
// 构造函数
func NewXMLKey() XMLKey {
return XMLKey{}
}
// 包装公钥
func (this XMLKey) MarshalPublicKey(key *dsa.PublicKey) ([]byte, error) {
publicKey := xmlPublicKey{
P: this.bigintToB64(key.P),
Q: this.bigintToB64(key.Q),
G: this.bigintToB64(key.G),
Y: this.bigintToB64(key.Y),
}
return xml.MarshalIndent(publicKey, "", " ")
}
func MarshalXMLPublicKey(key *dsa.PublicKey) ([]byte, error) {
return defaultXMLKey.MarshalPublicKey(key)
}
// 解析公钥
func (this XMLKey) ParsePublicKey(data []byte) (*dsa.PublicKey, error) {
var pub xmlPublicKey
err := xml.Unmarshal(data, &pub)
if err != nil {
return nil, err
}
p, err := this.b64ToBigint(pub.P)
if err != nil {
return nil, errPublicKeyXMLValue("P")
}
q, err := this.b64ToBigint(pub.Q)
if err != nil {
return nil, errPublicKeyXMLValue("Q")
}
g, err := this.b64ToBigint(pub.G)
if err != nil {
return nil, errPublicKeyXMLValue("G")
}
y, err := this.b64ToBigint(pub.Y)
if err != nil {
return nil, errPublicKeyXMLValue("Y")
}
if p.Sign() <= 0 || q.Sign() <= 0 || g.Sign() <= 0 || y.Sign() <= 0 {
return nil, errors.New("dsa xml: public key contains zero or negative value")
}
publicKey := &dsa.PublicKey{
Parameters: dsa.Parameters{
P: p,
Q: q,
G: g,
},
Y: y,
}
return publicKey, nil
}
func ParseXMLPublicKey(der []byte) (*dsa.PublicKey, error) {
return defaultXMLKey.ParsePublicKey(der)
}
// ====================
// 包装私钥
func (this XMLKey) MarshalPrivateKey(key *dsa.PrivateKey) ([]byte, error) {
// 构造私钥信息
priv := xmlPrivateKey{
P: this.bigintToB64(key.P),
Q: this.bigintToB64(key.Q),
G: this.bigintToB64(key.G),
Y: this.bigintToB64(key.Y),
X: this.bigintToB64(key.X),
}
return xml.MarshalIndent(priv, "", " ")
}
func MarshalXMLPrivateKey(key *dsa.PrivateKey) ([]byte, error) {
return defaultXMLKey.MarshalPrivateKey(key)
}
// 解析私钥
func (this XMLKey) ParsePrivateKey(data []byte) (*dsa.PrivateKey, error) {
var priv xmlPrivateKey
err := xml.Unmarshal(data, &priv)
if err != nil {
return nil, err
}
p, err := this.b64ToBigint(priv.P)
if err != nil {
return nil, errPrivateKeyXMLValue("P")
}
q, err := this.b64ToBigint(priv.Q)
if err != nil {
return nil, errPrivateKeyXMLValue("Q")
}
g, err := this.b64ToBigint(priv.G)
if err != nil {
return nil, errPrivateKeyXMLValue("G")
}
y, err := this.b64ToBigint(priv.Y)
if err != nil {
return nil, errPrivateKeyXMLValue("Y")
}
x, err := this.b64ToBigint(priv.X)
if err != nil {
return nil, errPrivateKeyXMLValue("X")
}
if p.Sign() <= 0 || q.Sign() <= 0 || g.Sign() <= 0 || y.Sign() <= 0 || x.Sign() <= 0 {
return nil, errors.New("dsa xml: private key contains zero or negative value")
}
privateKey := &dsa.PrivateKey{
PublicKey: dsa.PublicKey{
Parameters: dsa.Parameters{
P: p,
Q: q,
G: g,
},
Y: y,
},
X: x,
}
return privateKey, nil
}
func ParseXMLPrivateKey(der []byte) (*dsa.PrivateKey, error) {
return defaultXMLKey.ParsePrivateKey(der)
}
// ====================
func (this XMLKey) b64d(str string) ([]byte, error) {
decoded, err := base64.StdEncoding.DecodeString(str)
return []byte(decoded), err
}
func (this XMLKey) b64e(src []byte) string {
return base64.StdEncoding.EncodeToString(src)
}
func (this XMLKey) b64ToBigint(str string) (*big.Int, error) {
decoded, err := this.b64d(str)
if err != nil {
return nil, err
}
bInt := &big.Int{}
bInt.SetBytes(decoded)
return bInt, nil
}
func (this XMLKey) bigintToB64(encoded *big.Int) string {
return this.b64e(encoded.Bytes())
}