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