How to get the correct extension value in golang?

Viewed 301

I have a piece of code to extract extension value using OID. After extracting the value, the following is done in Java using Bouncy Castle:

private DEROctetString bytes;

public KeyInfo(byte[] bytes) {
    this.bytes = new DEROctetString(bytes);
}

public static KeyInfo valueOf(byte[] asn1) throws IOException {
    ASN1Object object = ASN1Primitive.fromByteArray(asn1);
    DEROctetString der = (DEROctetString)object.toASN1Primitive();
    return new KeyInfo(der.getOctets());
}

public byte[] getBytes() {
    return bytes.getOctets();
}

Finally, I use the getBytes() method to use the value for further manipulation.

Can someone explain, what is happening in the above piece of code?

I have added the piece of code, to extract extension for a particular OID from certificate in Golang. I am not sure how to implement the above part in Golang. Can anyone share any sample code?

This is what I have so far:

extensions = cert.Extensions
    ID := asn1.ObjectIdentifier{2,6,7,8}
    for _, ext := range extensions {
        if reflect.DeepEqual(ext.Id, ID) {
            testextValue = ext.Value
        }

Thank in advance.

Update

So, I tried this ,

var n []byte
    asn1.Unmarshal(value, &n)
    fmt.Println("After marshal/unmarshal: ", n)

where value, is the extension.value of that particular extension.OID. But the print statement prints [].

I need to do the equivaluent of

 ASN1Object object = ASN1Primitive.fromByteArray(asn1);
    DEROctetString der = (DEROctetString)object.toASN1Primitive();
    return new KeyInfo(der.getOctets());

in golang.

I am trying to verify the key and signature using VerifyPKCS1v15. If i pass ext.Value directly, it gives me verification error. I guess i need to do the above conversion first.

Update (30/8)

type BitString struct {
        Bytes     []byte // bits packed into bytes.
        BitLength int    // length in bits.
    }

    var bitstring BitString
    _, err = asn1.Unmarshal(value, &bitstring)
    fmt.Println("error", err)
    fmt.Println("After marshal/unmarshal: ", bitstring.Bytes)

This shows error as structure error:

tags don't match (16 vs {class:3 tag:84 length:67 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} BitString @3

It will be helpful if anyone can share a sample code.

0 Answers
Related