-
Notifications
You must be signed in to change notification settings - Fork 1
/
x509.go
267 lines (241 loc) · 9.39 KB
/
x509.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright © 2020-present Artem V. Zaborskiy <[email protected]>. All rights reserved.
*
* This source code is licensed under the Apache 2.0 license found
* in the LICENSE file in the root directory of this source tree.
*/
package gost_crypto
import (
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"errors"
"fmt"
"math/big"
"strconv"
"time"
"github.com/ftomza/gogost/gost3410"
"golang.org/x/crypto/cryptobyte"
cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
)
var (
oidTc26Gost34102012256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 1}
oidTc26Gost34102012512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 2}
oidTc26Gost34112012256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 2}
oidTc26Gost34112012512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 3}
oidTc26signwithdigestgost341012256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 3, 2}
oidTc26signwithdigestgost341012512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 3, 3}
oidTc26agreementgost341012256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 6, 1}
oidTc26agreementgost341012512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 6, 2}
oidTc26Gost34102012256Signature = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 3, 2}
oidTc26Gost34102012512Signature = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 3, 3}
oidGostR34102001CryptoProAParamSet = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 35, 1}
oidGostR34102001CryptoProBParamSet = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 35, 2}
oidGostR34102001CryptoProCParamSet = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 35, 3}
oidGostR34102001CryptoProXchAParamSet = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 36, 0}
oidGostR34102001CryptoProXchBParamSet = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 36, 1}
oidTc26Gost34102012256ParamSetA = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 2, 1, 1, 1}
oidTc26Gost34102012256ParamSetB = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 2, 1, 1, 2}
oidTc26Gost34102012256ParamSetC = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 2, 1, 1, 3}
oidTc26Gost34102012256ParamSetD = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 2, 1, 1, 4}
oidTc26Gost34102012512ParamSetA = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 2, 1, 2, 1}
oidTc26Gost34102012512ParamSetB = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 2, 1, 2, 2}
oidTc26Gost34102012512ParamSetC = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 2, 1, 2, 3}
oidtc26gost341112256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 2}
oidtc26gost341112512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 3}
)
type PublicKeyAlgorithm int
const (
UnknownPublicKeyAlgorithm PublicKeyAlgorithm = iota
GOST
)
var publicKeyAlgoName = [...]string{
GOST: "GOST",
}
func (algo PublicKeyAlgorithm) String() string {
if 0 < algo && int(algo) < len(publicKeyAlgoName) {
return publicKeyAlgoName[algo]
}
return strconv.Itoa(int(algo))
}
type certificate struct {
Raw asn1.RawContent
TBSCertificate tbsCertificate
SignatureAlgorithm pkix.AlgorithmIdentifier
SignatureValue asn1.BitString
}
type tbsCertificate struct {
Raw asn1.RawContent
Version int `asn1:"optional,explicit,default:0,tag:0"`
SerialNumber *big.Int
SignatureAlgorithm pkix.AlgorithmIdentifier
Issuer asn1.RawValue
Validity validity
Subject asn1.RawValue
PublicKeyInfo publicKeyInfo
UniqueId asn1.BitString `asn1:"optional,tag:1"`
SubjectUniqueId asn1.BitString `asn1:"optional,tag:2"`
Extensions []pkix.Extension `asn1:"optional,explicit,tag:3"`
}
type publicKeyInfo struct {
Raw asn1.RawContent
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}
type validity struct {
NotBefore, NotAfter time.Time
}
type pkcs1PublicKey struct {
N *big.Int
E int
}
type GostR34102012PublicKeyParameters struct {
PublicKeyParamSet asn1.ObjectIdentifier
DigestParamSet asn1.ObjectIdentifier `asn1:"optional"`
}
func ParseCertificate(derBytes []byte) ([]byte, error) {
var cert certificate
rest, err := asn1.Unmarshal(derBytes, &cert)
if err != nil {
return nil, err
}
if len(rest) != 0 {
return nil, errors.New("x509: trailing data after ASN.1 of certificate")
}
return cert.TBSCertificate.PublicKeyInfo.Raw, nil
}
func ParsePKIXPublicKey(derBytes []byte) (pub *gost3410.PublicKey, err error) {
var pki publicKeyInfo
pki, err = getPublicKeyInfo(derBytes)
if err != nil {
return nil, err
}
algo := getPublicKeyAlgorithmFromOID(pki.Algorithm.Algorithm)
if algo == UnknownPublicKeyAlgorithm {
return nil, fmt.Errorf("x509: unknown public key algorithm: %v", pki.Algorithm.Algorithm)
}
return parsePublicKey(algo, &pki)
}
func getPublicKeyInfo(derBytes []byte) (publicKeyInfo, error) {
var pki publicKeyInfo
if rest, err := asn1.Unmarshal(derBytes, &pki); err != nil {
if _, err := asn1.Unmarshal(derBytes, &pkcs1PublicKey{}); err == nil {
return publicKeyInfo{}, errors.New("x509: failed to parse public key (use ParsePKCS1PublicKey instead for this key format)")
}
return publicKeyInfo{}, err
} else if len(rest) != 0 {
return publicKeyInfo{}, errors.New("x509: trailing data after ASN.1 of public-key")
}
return pki, nil
}
func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) PublicKeyAlgorithm {
switch {
case oid.Equal(oidTc26Gost34102012256):
return GOST
case oid.Equal(oidTc26Gost34102012512):
return GOST
case oid.Equal(oidTc26agreementgost341012256):
return GOST
case oid.Equal(oidTc26agreementgost341012512):
return GOST
case oid.Equal(oidTc26signwithdigestgost341012256):
return GOST
case oid.Equal(oidTc26signwithdigestgost341012512):
return GOST
}
return UnknownPublicKeyAlgorithm
}
func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo) (*gost3410.PublicKey, error) {
asn1Data := keyData.PublicKey.RightAlign()
switch algo {
case GOST:
var pubRaw []byte
s := cryptobyte.String(asn1Data)
if !s.ReadASN1Bytes(&pubRaw, cryptobyte_asn1.OCTET_STRING) {
return nil, errors.New("x509: can not decode GOST public key")
}
curve, err := getCurve(keyData.Algorithm)
if err != nil {
return nil, err
}
return gost3410.NewPublicKey(curve, pubRaw)
default:
return nil, nil
}
}
func getCurve(algoData pkix.AlgorithmIdentifier) (*gost3410.Curve, error) {
paramsData := algoData.Parameters.FullBytes
var publicKeyParams GostR34102012PublicKeyParameters
rest, err := asn1.Unmarshal(paramsData, &publicKeyParams)
if err != nil {
return nil, errors.New("x509: failed to parse GOST parameters")
}
if len(rest) != 0 {
return nil, errors.New("x509: trailing data after GOST parameters")
}
var curve *gost3410.Curve
switch {
case publicKeyParams.PublicKeyParamSet.Equal(oidGostR34102001CryptoProAParamSet):
curve = gost3410.CurveIdGostR34102001CryptoProAParamSet()
case publicKeyParams.PublicKeyParamSet.Equal(oidGostR34102001CryptoProBParamSet):
curve = gost3410.CurveIdGostR34102001CryptoProBParamSet()
case publicKeyParams.PublicKeyParamSet.Equal(oidGostR34102001CryptoProCParamSet):
curve = gost3410.CurveIdGostR34102001CryptoProCParamSet()
case publicKeyParams.PublicKeyParamSet.Equal(oidGostR34102001CryptoProXchAParamSet):
curve = gost3410.CurveIdGostR34102001CryptoProXchAParamSet()
case publicKeyParams.PublicKeyParamSet.Equal(oidGostR34102001CryptoProXchBParamSet):
curve = gost3410.CurveIdGostR34102001CryptoProXchBParamSet()
case publicKeyParams.PublicKeyParamSet.Equal(oidTc26Gost34102012256ParamSetA):
curve = gost3410.CurveIdtc26gost34102012256paramSetA()
case publicKeyParams.PublicKeyParamSet.Equal(oidTc26Gost34102012256ParamSetB):
curve = gost3410.CurveIdtc26gost34102012256paramSetB()
case publicKeyParams.PublicKeyParamSet.Equal(oidTc26Gost34102012256ParamSetC):
curve = gost3410.CurveIdtc26gost34102012256paramSetC()
case publicKeyParams.PublicKeyParamSet.Equal(oidTc26Gost34102012256ParamSetD):
curve = gost3410.CurveIdtc26gost34102012256paramSetD()
case publicKeyParams.PublicKeyParamSet.Equal(oidTc26Gost34102012512ParamSetA):
curve = gost3410.CurveIdtc26gost341012512paramSetA()
case publicKeyParams.PublicKeyParamSet.Equal(oidTc26Gost34102012512ParamSetB):
curve = gost3410.CurveIdtc26gost341012512paramSetB()
case publicKeyParams.PublicKeyParamSet.Equal(oidTc26Gost34102012512ParamSetC):
curve = gost3410.CurveIdtc26gost34102012512paramSetC()
default:
return nil, errors.New("x509: unknown GOST curve")
}
return curve, nil
}
type pkcs8 struct {
Version int
Algo pkix.AlgorithmIdentifier
PrivateKey []byte
// optional attributes omitted.
}
func ParsePKCS8PrivateKey(derBytes []byte) (key *gost3410.PrivateKey, err error) {
var privKey pkcs8
if _, err := asn1.Unmarshal(derBytes, &privKey); err != nil {
return nil, err
}
algo := getPublicKeyAlgorithmFromOID(privKey.Algo.Algorithm)
switch algo {
case GOST:
var privRaw []byte
s := cryptobyte.String(privKey.PrivateKey)
if !s.ReadASN1Bytes(&privRaw, cryptobyte_asn1.OCTET_STRING) {
return nil, errors.New("x509: can not decode GOST public key")
}
curve, err := getCurve(privKey.Algo)
if err != nil {
return nil, err
}
return gost3410.NewPrivateKey(curve, privRaw)
default:
return nil, fmt.Errorf("x509: PKCS#8 wrapping contained private key with unknown algorithm: %v", privKey.Algo.Algorithm)
}
}
func DerDecode(data []byte) (*pem.Block, error) {
block, _ := pem.Decode(data)
if block == nil {
return nil, errors.New("DER: content not found")
}
return block, nil
}