Skip to content

Commit e6fc257

Browse files
committed
x509 cert
1 parent 5656191 commit e6fc257

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

common/crypto/x509/x509.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,18 @@ func marshalPKCS8PrivateKey(key *rsa.PrivateKey) ([]byte, error) {
440440
}
441441

442442
func marshalGOSTPKCS8PrivateKey(key *gost3410.PrivateKey) ([]byte, error) {
443+
// Determine the correct OID based on the curve size
444+
var algorithmOID asn1.ObjectIdentifier
445+
rawKey := key.Raw()
446+
447+
if len(rawKey) == 32 {
448+
// GOST R 34.10-2012 256-bit
449+
algorithmOID = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 1}
450+
} else {
451+
// GOST R 34.10-2012 512-bit
452+
algorithmOID = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 2}
453+
}
454+
443455
// Create PKCS8 structure for GOST private key
444456
pkcs8 := struct {
445457
Version int
@@ -448,9 +460,9 @@ func marshalGOSTPKCS8PrivateKey(key *gost3410.PrivateKey) ([]byte, error) {
448460
}{
449461
Version: 0,
450462
Algorithm: pkix.AlgorithmIdentifier{
451-
Algorithm: asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 1}, // GOST R 34.10-2012 256-bit
463+
Algorithm: algorithmOID,
452464
},
453-
PrivateKey: key.Raw(),
465+
PrivateKey: rawKey,
454466
}
455467

456468
// Encode to ASN.1 DER

common/protocol/tls/cert/cert.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,29 +181,42 @@ func MustGenerateSM2(parent *Certificate, opts ...SM2Option) *Certificate {
181181

182182
// GenerateGOST2012_256 generates a certificate using GOST 2012-256 algorithm
183183
func GenerateGOST2012_256(parent *Certificate, opts ...SM2Option) (*Certificate, error) {
184-
// Собираем параметры
184+
// Extract parameters from options
185185
var (
186186
commonName string
187+
organization string
188+
dnsNames []string
187189
expireDays int = 365
188190
)
191+
189192
for _, opt := range opts {
190-
// Извлекаем CommonName и NotAfter из опций (если есть)
191-
optFunc := opt
193+
// Create a temporary certificate to extract options
192194
tmp := &sm2x509.Certificate{}
193-
optFunc(tmp)
195+
opt(tmp)
196+
194197
if tmp.Subject.CommonName != "" {
195198
commonName = tmp.Subject.CommonName
196199
}
200+
if len(tmp.Subject.Organization) > 0 {
201+
organization = tmp.Subject.Organization[0]
202+
}
203+
if len(tmp.DNSNames) > 0 {
204+
dnsNames = tmp.DNSNames
205+
}
197206
if !tmp.NotAfter.IsZero() {
198207
days := int(tmp.NotAfter.Sub(time.Now()).Hours() / 24)
199208
if days > 0 {
200209
expireDays = days
201210
}
202211
}
203212
}
213+
204214
if commonName == "" {
205215
commonName = "GOST2012-256"
206216
}
217+
if organization == "" {
218+
organization = "Test Organization"
219+
}
207220

208221
certPEM, keyPEM, err := gost.GenerateGOSTSelfSignedCert(
209222
gost3410.CurveIdtc26gost34102012256paramSetA(),

0 commit comments

Comments
 (0)