michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.browserid; michael@0: michael@0: michael@0: /** michael@0: * Java produces signature in ASN.1 format. Here's some hard-coded encoding and decoding michael@0: * code, courtesy of a comment in michael@0: * http://stackoverflow.com/questions/10921733/how-sign-method-of-the-digital-signature-combines-the-r-s-values-in-to-array. michael@0: */ michael@0: public class ASNUtils { michael@0: /** michael@0: * Decode two short arrays from ASN.1 bytes. michael@0: * @param input to extract. michael@0: * @return length 2 array of byte arrays. michael@0: */ michael@0: public static byte[][] decodeTwoArraysFromASN1(byte[] input) throws IllegalArgumentException { michael@0: if (input == null) { michael@0: throw new IllegalArgumentException("input must not be null"); michael@0: } michael@0: if (input.length <= 3) michael@0: throw new IllegalArgumentException("bad length"); michael@0: if (input[0] != 0x30) michael@0: throw new IllegalArgumentException("bad encoding"); michael@0: if ((input[1] & ((byte) 0x80)) != 0) michael@0: throw new IllegalArgumentException("bad length encoding"); michael@0: if (input[2] != 0x02) michael@0: throw new IllegalArgumentException("bad encoding"); michael@0: if ((input[3] & ((byte) 0x80)) != 0) michael@0: throw new IllegalArgumentException("bad length encoding"); michael@0: byte rLength = input[3]; michael@0: if (input.length <= 5 + rLength) michael@0: throw new IllegalArgumentException("bad length"); michael@0: if (input[4 + rLength] != 0x02) michael@0: throw new IllegalArgumentException("bad encoding"); michael@0: if ((input[5 + rLength] & (byte) 0x80) !=0) michael@0: throw new IllegalArgumentException("bad length encoding"); michael@0: byte sLength = input[5 + rLength]; michael@0: if (input.length != 6 + sLength + rLength) michael@0: throw new IllegalArgumentException("bad length"); michael@0: byte[] rArr = new byte[rLength]; michael@0: byte[] sArr = new byte[sLength]; michael@0: System.arraycopy(input, 4, rArr, 0, rLength); michael@0: System.arraycopy(input, 6 + rLength, sArr, 0, sLength); michael@0: return new byte[][] { rArr, sArr }; michael@0: } michael@0: michael@0: /** michael@0: * Encode two short arrays into ASN.1 bytes. michael@0: * @param first array to encode. michael@0: * @param second array to encode. michael@0: * @return array. michael@0: */ michael@0: public static byte[] encodeTwoArraysToASN1(byte[] first, byte[] second) throws IllegalArgumentException { michael@0: if (first == null) { michael@0: throw new IllegalArgumentException("first must not be null"); michael@0: } michael@0: if (second == null) { michael@0: throw new IllegalArgumentException("second must not be null"); michael@0: } michael@0: byte[] output = new byte[6 + first.length + second.length]; michael@0: output[0] = 0x30; michael@0: if (4 + first.length + second.length > 255) michael@0: throw new IllegalArgumentException("bad length"); michael@0: output[1] = (byte) (4 + first.length + second.length); michael@0: if ((output[1] & ((byte) 0x80)) != 0) michael@0: throw new IllegalArgumentException("bad length encoding"); michael@0: output[2] = 0x02; michael@0: output[3] = (byte) first.length; michael@0: if ((output[3] & ((byte) 0x80)) != 0) michael@0: throw new IllegalArgumentException("bad length encoding"); michael@0: System.arraycopy(first, 0, output, 4, first.length); michael@0: output[4 + first.length] = 0x02; michael@0: output[5 + first.length] = (byte) second.length; michael@0: if ((output[5 + first.length] & ((byte) 0x80)) != 0) michael@0: throw new IllegalArgumentException("bad length encoding"); michael@0: System.arraycopy(second, 0, output, 6 + first.length, second.length); michael@0: return output; michael@0: } michael@0: }