Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / December 2003

Tip: Looking for answers? Try searching our database.

ASN1 decoding of digital signatures

Thread view: 
Archaeopteryx - 09 Nov 2003 13:38 GMT
I finally found that SHA1withDSA signatures generated
by the Java security API are encoded using ASN1 rules,
which may lead to variable lenght of the encoded signature
(the signature itself is 40 bytes long but the length is variable
after ASN1 encoding).
The R and S values composing the signature are encoded using the ASN1 rule:
signature ::= SEQUENCE {INTEGER R, INTEGER S}.

Is there any java API allowing me to decode an ASN1 (DER/BER) encoded
string to get its components?

Thanks

Arch.
JK - 11 Nov 2003 09:42 GMT
Try www.bouncycastle.org. There are also some "hidden" classes shipped
with Sun's Java, but they are not documented and may therefore change
with java versions.

JK

> I finally found that SHA1withDSA signatures generated
> by the Java security API are encoded using ASN1 rules,
[quoted text clipped - 10 lines]
>
> Arch.
Hans Granqvist - 03 Dec 2003 18:32 GMT
> I finally found that SHA1withDSA signatures generated
> by the Java security API are encoded using ASN1 rules,
[quoted text clipped - 10 lines]
>
> Arch.

You could use bouncy castle's ASN.1 parser. It's pretty good.
If you know what you'r doing, you can parse ASN.1 DER like
maybe something like this:

   /** Returns an array of [b|s] */
   static byte[] convertToRfc2437(byte[] signatureValue)
   {
    // ----------------------------------------------------------------
    // | 0x30 len | 0x02 len(r) | ... r ... | 0x02 len(s) | ... s ... |
    // ----------------------------------------------------------------
    int start = 4;
    int len = signatureValue[start - 1];
   
    byte[] data = new byte[len];
    System.arraycopy(signatureValue, start, data, 0, len);
    byte[] r = fit(data, 20);

    start += len + 2;
    len = signatureValue[start - 1];
    data = new byte[len];
    System.arraycopy(signatureValue, start, data, 0, len);
    byte[] s = fit(data, 20);

    byte[] b = new byte[40];
    System.arraycopy(r, 0, b, 0, 20);
    System.arraycopy(s, 0, b, 20, 20);

    return b;
   }

   /**
    * Fit (stretch or shrink) b into an array without losing precision,
    * as required by I2OSP.
    */
   private static byte[] fit(byte[] b, int length)
   {
       int len = b.length;
       int offset = (b[0] == 0) ? 1 : 0;
       len -= offset;

    if (len > length){
      // DSA value too large to fit
          // into 'length' bytes -- this should probably
          // be handled . . .
      len = length;
    }
    byte[] bytes = new byte[length];
       System.arraycopy(b, offset, bytes, length - len, len);
       return bytes;
   }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.