83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package x509
|
|
|
|
import (
|
|
"jasn1/asn1"
|
|
"jasn1/helpers"
|
|
"jasn1/oid"
|
|
)
|
|
|
|
func LoadCertificate(tree *asn1.Tag) *Certificate {
|
|
cert := Certificate { }
|
|
cert.Data.Version = tree.At(0, 0, 0).Value[0]
|
|
cert.Data.SerialNumber = tree.At(0, 1).Value
|
|
encoded_id := tree.At(0, 2, 0).Value
|
|
obj, err := oid.FindObjectByEncodedId(encoded_id)
|
|
|
|
if err != nil {
|
|
// FIXME: return an error here
|
|
return nil
|
|
}
|
|
|
|
cert.Data.Signature.Algorithm = obj
|
|
// FIXME: Add parameters here
|
|
|
|
for _, branch := range tree.At(0, 3).Children {
|
|
// FIXME: handle this error
|
|
obj, _ = oid.FindObjectByEncodedId(branch.At(0, 0).Value)
|
|
value := branch.At(0, 1).Value
|
|
cert.Data.Issuer.Insert(obj, value)
|
|
}
|
|
|
|
cert.Data.Validity.NotBefore = tree.At(0, 4, 0).DateValue()
|
|
cert.Data.Validity.NotAfter = tree.At(0, 4, 1).DateValue()
|
|
|
|
for _, branch := range tree.At(0, 5).Children {
|
|
obj, _ = oid.FindObjectByEncodedId(branch.At(0, 0).Value)
|
|
value := branch.At(0, 1).Value
|
|
cert.Data.Subject.Insert(obj, value)
|
|
}
|
|
|
|
encoded_id = tree.At(0, 6, 0, 0).Value
|
|
// FIXME: handle this error
|
|
obj, _ = oid.FindObjectByEncodedId(encoded_id)
|
|
key_data := tree.At(0, 6, 1).Value
|
|
cert.Data.SubjectPublicKeyInfo.Algorithm.Algorithm = obj
|
|
// FIXME: Add parameters here
|
|
|
|
switch obj.Name {
|
|
case "rsaEncryption":
|
|
key_data = key_data[1 + key_data[0]:]
|
|
key, _ := asn1.DecodeByteString(key_data)
|
|
exp := helpers.BuildUint64(key.At(1).Value)
|
|
cert.Data.SubjectPublicKeyInfo.PublicKey.Modulus = key.At(0).Value
|
|
cert.Data.SubjectPublicKeyInfo.PublicKey.Exponent = exp
|
|
default:
|
|
cert.Data.SubjectPublicKeyInfo.PublicKey.Data = key_data
|
|
}
|
|
|
|
for idx := 7; idx < len(tree.At(0).Children); idx += 1 {
|
|
branch := tree.At(0, uint(idx))
|
|
|
|
switch branch.Class {
|
|
case 2:
|
|
for _, ext_data := range branch.At(0).Children {
|
|
ext := Extension { }
|
|
ext.ExtnID, _ = oid.FindObjectByEncodedId(ext_data.At(0).Value)
|
|
cert.Data.Extensions = append(cert.Data.Extensions, ext)
|
|
|
|
if len(ext_data.Children) > 2 {
|
|
ext.Critical = ext_data.At(1).BoolValue()
|
|
ext.ExtnValue = ext_data.At(2).Value
|
|
} else {
|
|
ext.Critical = false
|
|
ext.ExtnValue = ext_data.At(1).Value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
obj, _ = oid.FindObjectByEncodedId(tree.At(1, 0).Value)
|
|
cert.SignatureAlgo.Algorithm = obj
|
|
cert.SignatureValue = tree.At(2).Value
|
|
return &cert
|
|
}
|