本文整理汇总了Golang中encoding/asn1.BitString类的典型用法代码### 示例。如果您正苦于以下问题:Golang BitString类的具体用法?Golang BitString怎么用?Golang BitString使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了BitString类的2个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: parseCertificate

func parseCertificate(in *certificate) (*Certificate, error) {
    out := new(Certificate)
    out.Raw = in.Raw
    out.RawTBSCertificate = in.TBSCertificate.Raw
    out.RawSubjectPublicKeyInfo = in.TBSCertificate.PublicKey.Raw
    out.RawSubject = in.TBSCertificate.Subject.FullBytes
    out.RawIssuer = in.TBSCertificate.Issuer.FullBytes

    out.Signature = in.SignatureValue.RightAlign()
    out.SignatureAlgorithm =
        getSignatureAlgorithmFromOID(in.TBSCertificate.SignatureAlgorithm.Algorithm)

    out.PublicKeyAlgorithm =
        getPublicKeyAlgorithmFromOID(in.TBSCertificate.PublicKey.Algorithm.Algorithm)
    var err error
    out.PublicKey, err = parsePublicKey(out.PublicKeyAlgorithm, &in.TBSCertificate.PublicKey)
    if err != nil {
        return nil, err
    }

    if in.TBSCertificate.SerialNumber.Sign() < 0 {
        return nil, errors.New("negative serial number")
    }

    out.Version = in.TBSCertificate.Version + 1
    out.SerialNumber = in.TBSCertificate.SerialNumber

    var issuer, subject pkix.RDNSequence
    if _, err := asn1.Unmarshal(in.TBSCertificate.Subject.FullBytes, &subject); err != nil {
        return nil, err
    }
    if _, err := asn1.Unmarshal(in.TBSCertificate.Issuer.FullBytes, &issuer); err != nil {
        return nil, err
    }

    out.Issuer.FillFromRDNSequence(&issuer)
    out.Subject.FillFromRDNSequence(&subject)

    out.NotBefore = in.TBSCertificate.Validity.NotBefore
    out.NotAfter = in.TBSCertificate.Validity.NotAfter

    for _, e := range in.TBSCertificate.Extensions {
        if len(e.Id) == 4 && e.Id[0] == 2 && e.Id[1] == 5 && e.Id[2] == 29 {
            switch e.Id[3] {
            case 15:
                // RFC 5280, 4.2.1.3
                var usageBits asn1.BitString
                _, err := asn1.Unmarshal(e.Value, &usageBits)

                if err == nil {
                    var usage int
                    for i := 0; i < 9; i++ {
                        if usageBits.At(i) != 0 {
                            usage |= 1 << uint(i)
                        }
                    }
                    out.KeyUsage = KeyUsage(usage)
                    continue
                }
            case 19:
                // RFC 5280, 4.2.1.9
                var constraints basicConstraints
                _, err := asn1.Unmarshal(e.Value, &constraints)

                if err == nil {
                    out.BasicConstraintsValid = true
                    out.IsCA = constraints.IsCA
                    out.MaxPathLen = constraints.MaxPathLen
                    continue
                }
            case 17:
                // RFC 5280, 4.2.1.6

                // SubjectAltName ::= GeneralNames
                //
                // GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
                //
                // GeneralName ::= CHOICE {
                //      otherName                       [0]     OtherName,
                //      rfc822Name                      [1]     IA5String,
                //      dNSName                         [2]     IA5String,
                //      x400Address                     [3]     ORAddress,
                //      directoryName                   [4]     Name,
                //      ediPartyName                    [5]     EDIPartyName,
                //      uniformResourceIdentifier       [6]     IA5String,
                //      iPAddress                       [7]     OCTET STRING,
                //      registeredID                    [8]     OBJECT IDENTIFIER }
                var seq asn1.RawValue
                _, err := asn1.Unmarshal(e.Value, &seq)
                if err != nil {
                    return nil, err
                }
                if !seq.IsCompound || seq.Tag != 16 || seq.Class != 0 {
                    return nil, asn1.StructuralError{Msg: "bad SAN sequence"}
                }

                parsedName := false

                rest := seq.Bytes
                for len(rest) > 0 {
//.........这里部分代码省略.........

开发者ID:jmcadden,项目名称:EbbRT-gcc,代码行数:101,代码来源:x509.go

示例2: parseCertificate

func parseCertificate(in *certificate) (*Certificate, error) {
    out := new(Certificate)
    out.Raw = in.Raw
    out.RawTBSCertificate = in.TBSCertificate.Raw
    out.RawSubjectPublicKeyInfo = in.TBSCertificate.PublicKey.Raw
    out.RawSubject = in.TBSCertificate.Subject.FullBytes
    out.RawIssuer = in.TBSCertificate.Issuer.FullBytes

    out.Signature = in.SignatureValue.RightAlign()
    out.SignatureAlgorithm =
        getSignatureAlgorithmFromOID(in.TBSCertificate.SignatureAlgorithm.Algorithm)

    out.PublicKeyAlgorithm =
        getPublicKeyAlgorithmFromOID(in.TBSCertificate.PublicKey.Algorithm.Algorithm)
    var err error
    out.PublicKey, err = parsePublicKey(out.PublicKeyAlgorithm, &in.TBSCertificate.PublicKey)
    if err != nil {
        return nil, err
    }

    if in.TBSCertificate.SerialNumber.Sign() < 0 {
        return nil, errors.New("x509: negative serial number")
    }

    out.Version = in.TBSCertificate.Version + 1
    out.SerialNumber = in.TBSCertificate.SerialNumber

    var issuer, subject pkix.RDNSequence
    if _, err := asn1.Unmarshal(in.TBSCertificate.Subject.FullBytes, &subject); err != nil {
        return nil, err
    }
    if _, err := asn1.Unmarshal(in.TBSCertificate.Issuer.FullBytes, &issuer); err != nil {
        return nil, err
    }

    out.Issuer.FillFromRDNSequence(&issuer)
    out.Subject.FillFromRDNSequence(&subject)

    out.NotBefore = in.TBSCertificate.Validity.NotBefore
    out.NotAfter = in.TBSCertificate.Validity.NotAfter

    for _, e := range in.TBSCertificate.Extensions {
        out.Extensions = append(out.Extensions, e)

        if len(e.Id) == 4 && e.Id[0] == 2 && e.Id[1] == 5 && e.Id[2] == 29 {
            switch e.Id[3] {
            case 15:
                // RFC 5280, 4.2.1.3
                var usageBits asn1.BitString
                _, err := asn1.Unmarshal(e.Value, &usageBits)

                if err == nil {
                    var usage int
                    for i := 0; i < 9; i++ {
                        if usageBits.At(i) != 0 {
                            usage |= 1 << uint(i)
                        }
                    }
                    out.KeyUsage = KeyUsage(usage)
                    continue
                }
            case 19:
                // RFC 5280, 4.2.1.9
                var constraints basicConstraints
                _, err := asn1.Unmarshal(e.Value, &constraints)

                if err == nil {
                    out.BasicConstraintsValid = true
                    out.IsCA = constraints.IsCA
                    out.MaxPathLen = constraints.MaxPathLen
                    continue
                }
            case 17:
                // RFC 5280, 4.2.1.6

                // SubjectAltName ::= GeneralNames
                //
                // GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
                //
                // GeneralName ::= CHOICE {
                //      otherName                       [0]     OtherName,
                //      rfc822Name                      [1]     IA5String,
                //      dNSName                         [2]     IA5String,
                //      x400Address                     [3]     ORAddress,
                //      directoryName                   [4]     Name,
                //      ediPartyName                    [5]     EDIPartyName,
                //      uniformResourceIdentifier       [6]     IA5String,
                //      iPAddress                       [7]     OCTET STRING,
                //      registeredID                    [8]     OBJECT IDENTIFIER }
                var seq asn1.RawValue
                _, err := asn1.Unmarshal(e.Value, &seq)
                if err != nil {
                    return nil, err
                }
                if !seq.IsCompound || seq.Tag != 16 || seq.Class != 0 {
                    return nil, asn1.StructuralError{Msg: "bad SAN sequence"}
                }

                parsedName := false

//.........这里部分代码省略.........

开发者ID:ArtemL,项目名称:GCC,代码行数:101,代码来源:x509.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng