103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package style
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"jasn1/x509"
|
|
)
|
|
|
|
type TimeSpan struct {
|
|
NotBefore string `json:"Not Before"`
|
|
NotAfter string `json:"Not After"`
|
|
}
|
|
|
|
type SubjectPublicKeyInfo struct {
|
|
Algorithm string `json:"Algorithm"`
|
|
KeySize uint64 `json:"Key Size"`
|
|
Modulus string
|
|
Exponent uint64
|
|
}
|
|
|
|
type Extension struct {
|
|
}
|
|
|
|
type CertificateData struct {
|
|
Version uint8
|
|
SerialNumber string `json:"Serial Number"` // []byte?
|
|
SignatureAlgorithm string `json:"Signature Algorithm"`
|
|
Issuer string
|
|
Validity TimeSpan
|
|
Subject string
|
|
SubjPubKeyInfo SubjectPublicKeyInfo `json:"Subject Public Key Info"`
|
|
Exts []Extension `json:"X509v3 extensions"`
|
|
}
|
|
|
|
type OpensslCertificate struct {
|
|
Data CertificateData
|
|
}
|
|
|
|
type OpensslFormat struct {
|
|
Certificate OpensslCertificate
|
|
}
|
|
|
|
var (
|
|
NAME_SHORTHAND = map[string]string {
|
|
"id-at-countryName": "C",
|
|
"id-at-organizationName": "O",
|
|
"id-at-commonName": "CN",
|
|
}
|
|
)
|
|
|
|
func FormatAsOpenssl(cert *x509.Certificate) *OpensslFormat {
|
|
ossl := OpensslFormat { Certificate: OpensslCertificate { }}
|
|
ossl.Certificate.Data.Version = cert.Data.Version + 1
|
|
ossl.Certificate.Data.SerialNumber = fmt.Sprintf("%02x", cert.Data.Serial[0])
|
|
|
|
for idx := 1; idx < len(cert.Data.Serial); idx += 1 {
|
|
ossl.Certificate.Data.SerialNumber += fmt.Sprintf(":%02x", cert.Data.Serial[idx])
|
|
}
|
|
|
|
ossl.Certificate.Data.SignatureAlgorithm = cert.Data.Signature.Name
|
|
|
|
for idx, piece := range cert.Data.Issuer {
|
|
sep := ", "
|
|
|
|
if idx == len(cert.Data.Issuer) - 1 {
|
|
sep = ""
|
|
}
|
|
|
|
shorthand, ok := NAME_SHORTHAND[piece.Name]
|
|
|
|
if ok {
|
|
ossl.Certificate.Data.Issuer += fmt.Sprintf("%s=%s%s", shorthand, string(piece.Value), sep)
|
|
}
|
|
}
|
|
|
|
ossl.Certificate.Data.Validity.NotBefore = cert.Data.Validity.NotBefore.String()
|
|
ossl.Certificate.Data.Validity.NotAfter = cert.Data.Validity.NotAfter.String()
|
|
|
|
for idx, piece := range cert.Data.Subject {
|
|
sep := ", "
|
|
|
|
if idx == len(cert.Data.Subject) - 1 {
|
|
sep = ""
|
|
}
|
|
|
|
shorthand, ok := NAME_SHORTHAND[piece.Name]
|
|
|
|
if ok {
|
|
ossl.Certificate.Data.Subject += fmt.Sprintf("%s=%s%s", shorthand, string(piece.Value), sep)
|
|
}
|
|
}
|
|
|
|
ossl.Certificate.Data.SubjPubKeyInfo.Algorithm = cert.Data.SubjectPublicKey.Algorithm.Name
|
|
ossl.Certificate.Data.SubjPubKeyInfo.KeySize = uint64(len(cert.Data.SubjectPublicKey.Modulus) - 1) * 8
|
|
ossl.Certificate.Data.SubjPubKeyInfo.Exponent = cert.Data.SubjectPublicKey.Exponent
|
|
ossl.Certificate.Data.SubjPubKeyInfo.Modulus = fmt.Sprintf("%02x", cert.Data.SubjectPublicKey.Modulus[0])
|
|
|
|
for _, key_byte := range cert.Data.SubjectPublicKey.Modulus[1:] {
|
|
ossl.Certificate.Data.SubjPubKeyInfo.Modulus += fmt.Sprintf(":%02x", key_byte)
|
|
}
|
|
|
|
return &ossl
|
|
}
|