mobile/android/base/browserid/ASNUtils.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.browserid;
     8 /**
     9  * Java produces signature in ASN.1 format. Here's some hard-coded encoding and decoding
    10  * code, courtesy of a comment in
    11  * <a href="http://stackoverflow.com/questions/10921733/how-sign-method-of-the-digital-signature-combines-the-r-s-values-in-to-array">http://stackoverflow.com/questions/10921733/how-sign-method-of-the-digital-signature-combines-the-r-s-values-in-to-array</a>.
    12  */
    13 public class ASNUtils {
    14   /**
    15    * Decode two short arrays from ASN.1 bytes.
    16    * @param input to extract.
    17    * @return length 2 array of byte arrays.
    18    */
    19   public static byte[][] decodeTwoArraysFromASN1(byte[] input) throws IllegalArgumentException {
    20     if (input == null) {
    21       throw new IllegalArgumentException("input must not be null");
    22     }
    23     if (input.length <= 3)
    24       throw new IllegalArgumentException("bad length");
    25     if (input[0] != 0x30)
    26       throw new IllegalArgumentException("bad encoding");
    27     if ((input[1] & ((byte) 0x80)) != 0)
    28       throw new IllegalArgumentException("bad length encoding");
    29     if (input[2] != 0x02)
    30       throw new IllegalArgumentException("bad encoding");
    31     if ((input[3] & ((byte) 0x80)) != 0)
    32       throw new IllegalArgumentException("bad length encoding");
    33     byte rLength = input[3];
    34     if (input.length <= 5 + rLength)
    35       throw new IllegalArgumentException("bad length");
    36     if (input[4 + rLength] != 0x02)
    37       throw new IllegalArgumentException("bad encoding");
    38     if ((input[5 + rLength] & (byte) 0x80) !=0)
    39       throw new IllegalArgumentException("bad length encoding");
    40     byte sLength = input[5 + rLength];
    41     if (input.length != 6 + sLength + rLength)
    42       throw new IllegalArgumentException("bad length");
    43     byte[] rArr = new byte[rLength];
    44     byte[] sArr = new byte[sLength];
    45     System.arraycopy(input, 4, rArr, 0, rLength);
    46     System.arraycopy(input, 6 + rLength, sArr, 0, sLength);
    47     return new byte[][] { rArr, sArr };
    48   }
    50   /**
    51    * Encode two short arrays into ASN.1 bytes.
    52    * @param first array to encode.
    53    * @param second array to encode.
    54    * @return array.
    55    */
    56   public static byte[] encodeTwoArraysToASN1(byte[] first, byte[] second) throws IllegalArgumentException {
    57     if (first == null) {
    58       throw new IllegalArgumentException("first must not be null");
    59     }
    60     if (second == null) {
    61       throw new IllegalArgumentException("second must not be null");
    62     }
    63     byte[] output = new byte[6 + first.length + second.length];
    64     output[0] = 0x30;
    65     if (4 + first.length + second.length > 255)
    66       throw new IllegalArgumentException("bad length");
    67     output[1] = (byte) (4 + first.length + second.length);
    68     if ((output[1] & ((byte) 0x80)) != 0)
    69       throw new IllegalArgumentException("bad length encoding");
    70     output[2] = 0x02;
    71     output[3] = (byte) first.length;
    72     if ((output[3] & ((byte) 0x80)) != 0)
    73       throw new IllegalArgumentException("bad length encoding");
    74     System.arraycopy(first, 0, output, 4, first.length);
    75     output[4 + first.length] = 0x02;
    76     output[5 + first.length] = (byte) second.length;
    77     if ((output[5 + first.length] & ((byte) 0x80)) != 0)
    78       throw new IllegalArgumentException("bad length encoding");
    79     System.arraycopy(second, 0, output, 6 + first.length, second.length);
    80     return output;
    81   }
    82 }

mercurial