1

I have a X509Certificate with SAN (GUID)

The following method is supposed to get it:

private static String getGUIDStringFromSequence(final ASN1Sequence seq)
    {
        if (seq != null)
        {
            // First in sequence is the object identifier, that we must check
            System.out.println(seq);
            final ASN1ObjectIdentifier id = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0));
            if (id != null && GUID_OBJECTID.equals(id.getId()))
            {
                final ASN1TaggedObject obj = (ASN1TaggedObject) seq.getObjectAt(1);
                ASN1Primitive prim = obj.getObject();

                // Due to bug in java cert.getSubjectAltName, it can be tagged an extra time
                if (prim instanceof ASN1TaggedObject)
                {
                    System.out.println("instance ASN1TaggedObject");
                    prim = ASN1TaggedObject.getInstance(((ASN1TaggedObject) prim)).getObject();
                }

                if (prim instanceof ASN1OctetString)
                {
                    System.out.println("instance ASN1OctetString");
                    return new String(((ASN1OctetString) prim).getOctets());
                }
                else if (prim instanceof ASN1String)
                {
                    System.out.println("instance ASN1String");
                    return ((ASN1String) prim).getString();
                }
                else
                {
                    return null;
                }
            }

            //System.out.println(id.getId());
        }
        return null;
    }

The output I get is the following:

[1.3.6.1.4.1.311.25.1, [0][0]#4d202cbf1b554732973b2ae849e7a70b]
instance ASN1TaggedObject
instance ASN1OctetString

And while 4d202cbf1b554732973b2ae849e7a70b is what I'm looking for, the string I get in the end M ,�UG2�;*�I� is definitely not

What's the problem? Does it have to do with encoding?

Thanks in advance

UPD

I've changed the line:

if (prim instanceof ASN1OctetString)
{
   System.out.println("instance ASN1OctetString");
   return new String(((ASN1OctetString) prim).getOctets());
}

to:

if (prim instanceof ASN1OctetString)
{
   System.out.println("instance ASN1OctetString");
   return prim.toString();
}

And I get #4d202cbf1b554732973b2ae849e7a70b. Yet I still believe that I should get the value differently, not just calling .toString() on ASN1OctetString object

Also, what is hash sign for?

Joe Doe
  • 11
  • 2
  • 1
    Check out https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java – vbezhenar May 02 '20 at 21:57
  • 1
    You have a byte array contain fairly random-looking bytes. It's not meant to be turned into a String instance, and since you say you're looking for the hex encoding of those bytes then that is how you should encode those bytes. – President James K. Polk May 03 '20 at 02:50
  • I've updated the question please see – Joe Doe May 03 '20 at 09:31

0 Answers0