views:

54

answers:

2

In bouncycastle I can create a DEROctetString starting from a KeyUsage.

How can I obtain KeyUsage starting from a DEROctetString then?

Example:

DEROctetString derString = new DEROctetString(new KeyUsage(KeyUsage.digitalSignature));
KeyUsage ku = ...(some code to get back KeyUsage starting from derString)...

I need this because I'm able to create Certificate Requests with KeyUsage extension request, but then, given the Certificate Request alone, I'm not able to get back the KeyUsage extension.

A: 

KeyUsage is defined as this in X.509,

 id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }

 KeyUsage ::= BIT STRING {
      digitalSignature        (0),
      nonRepudiation          (1),
      keyEncipherment         (2),
      dataEncipherment        (3),
      keyAgreement            (4),
      keyCertSign             (5),
      cRLSign                 (6),
      encipherOnly            (7),
      decipherOnly            (8) }

So it's wrong to create Octet String for it. If you create DERBitString, KeyUsage has a constructor for it.

ZZ Coder
I understand your point, but I have to create an OctetString because I need to pass it to the second parameter of the X509Extension constructor and then I need to get back the KeyUsage from that. Anyway I've found the solution, check my answer ;)
Andrea Zilio
A: 

I've found the solution using ASN1InputStream:

ASN1InputStream ais = new ASN1InputStream(derOctetString.getOctetStream());
KeyUsage ku = new KeyUsage((DERBitString) ais.readObject());

That works!

Andrea Zilio
I guess you meant "That compiles!". Encoding Bit String as Octet String is not going to work. As soon as you have more than one usage, you will see the problem.
ZZ Coder
No, it does compile and it works too (I mean that I get back the right KeyUsage.intValue()).
Andrea Zilio