diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2023-03-14 10:31:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-14 10:31:01 +0100 |
commit | 4d2d3658c70347e9cffa049898b2b768bafe6174 (patch) | |
tree | 2b41ba728d15dace11d75a7d8b62976b69fcb379 /src/main/java/org/uic/barcode/utils | |
parent | check for inflater return code added (diff) | |
parent | update version number (diff) | |
download | UIC-barcode-1.4.0.tar UIC-barcode-1.4.0.tar.gz UIC-barcode-1.4.0.tar.bz2 UIC-barcode-1.4.0.tar.lz UIC-barcode-1.4.0.tar.xz UIC-barcode-1.4.0.tar.zst UIC-barcode-1.4.0.zip |
Diffstat (limited to 'src/main/java/org/uic/barcode/utils')
-rw-r--r-- | src/main/java/org/uic/barcode/utils/SecurityUtils.java | 129 | ||||
-rw-r--r-- | src/main/java/org/uic/barcode/utils/package.html | 21 |
2 files changed, 140 insertions, 10 deletions
diff --git a/src/main/java/org/uic/barcode/utils/SecurityUtils.java b/src/main/java/org/uic/barcode/utils/SecurityUtils.java index 5fdbda7..8c981af 100644 --- a/src/main/java/org/uic/barcode/utils/SecurityUtils.java +++ b/src/main/java/org/uic/barcode/utils/SecurityUtils.java @@ -1,5 +1,8 @@ package org.uic.barcode.utils;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
@@ -10,6 +13,7 @@ import java.security.Security; import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
+import java.util.Arrays;
/**
* The Class SecurityUtils.
@@ -182,4 +186,129 @@ public class SecurityUtils { return null;
}
+
+ /**
+ * Decode signature integer sequence.
+ *
+ * Support function to decode a DSA signature
+ * Provides the two DSA signature parameter encoded in a DSA signature
+ *
+ * @param bytes the bytes
+ * @return the big integer[]
+ * @throws Exception the exception
+ */
+ public static BigInteger[] decodeSignatureIntegerSequence(byte[] bytes) throws Exception {
+
+ int sequenceTag = (int) bytes[0];
+
+ if (sequenceTag != 48) throw new Exception("signature is not a sequence");
+
+ int sequenceLength = (int) bytes[1];
+
+ if (sequenceLength < 6) throw new Exception("signature sequence too short");
+
+ BigInteger[] result = new BigInteger[2];
+
+ int offset = 2;
+ int i = 0;
+ while (offset < bytes.length && i < 2) {
+ int integerTag = (int) bytes[offset];
+ if (integerTag != 2) throw new Exception("signature is not an integer sequence");
+ int integerLength = (int) bytes[offset + 1];
+ byte[] value = Arrays.copyOfRange(bytes, offset + 2, offset + 2 + integerLength);
+ result[i] = new BigInteger(+1, value);
+ offset = offset + integerLength + 2;
+ i++;
+ }
+
+ return result;
+ }
+
+ /**
+ * Encode signature integer sequence.
+ *
+ * Support function to format two parameters as DER encoded integer list
+ * to get a valid formated DSA signature from the signature parameter
+ *
+ * @param i1 the i 1
+ * @param i2 the i 2
+ * @return the byte[]
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+ public static byte[] encodeSignatureIntegerSequence(BigInteger i1, BigInteger i2) throws IOException {
+
+ //SEQUENCE OF --> tag 16
+ int sequenceTag = 16 + 32; // (bits 6 = 1 constructed)
+ //INTEGER --> tag 2
+ int integerTag = 2;
+
+ byte[] b1 = i1.toByteArray();
+ int lb1 = b1.length;
+ byte[] b2 = i2.toByteArray();
+ int lb2 = b2.length;
+
+ int sequenceLength = lb1 + lb2 + 4;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ out.write((byte) sequenceTag);
+ out.write((byte) sequenceLength);
+ out.write((byte) integerTag);
+ out.write((byte) lb1);
+ out.write(b1);
+ out.write((byte) integerTag);
+ out.write((byte) lb2);
+ out.write(b2);
+
+ return out.toByteArray();
+ }
+
+ public static byte[] recombineDsaSignature(byte[] sealdata) throws IOException {
+
+ //check whether the encoding is wrong and the sealdata contain a signature
+ //remove trailing zeroes from the signature
+ BigInteger[] bInts = null;
+ try {
+ bInts = decodeSignatureIntegerSequence(sealdata);
+ byte[] sig = encodeSignatureIntegerSequence(bInts[0],bInts[1]);
+ //decoding the entire signature was ok, so there was no split
+ return sig;
+ } catch (Exception e) {
+ //the signature is correctly implemented, continue with recombination
+ }
+
+ // split the data into two blocks
+ int length = sealdata.length / 2;
+ byte[] rBytes = Arrays.copyOfRange(sealdata, 0, length);
+ byte[] sBytes = Arrays.copyOfRange(sealdata, length, length + length);
+
+ //convert to BigInteger to get rid of leading zeroes
+ BigInteger r = new BigInteger(1,rBytes);
+ BigInteger s = new BigInteger(1,sBytes);
+
+ //encode as DSA signature structure
+ //SEQUENCE OF --> tag 16
+ int sequenceTag = 16 + 32; // (bits 6 = 1 constructed)
+ //INTEGER --> tag 2
+ int integerTag = 2;
+
+ byte[] b1 = r.toByteArray();
+ int lb1 = b1.length;
+
+ byte[] b2 = s.toByteArray();
+ int lb2 = b2.length;
+ int sequenceLength = lb1 + lb2 + 4;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ out.write((byte) sequenceTag);
+ out.write((byte) sequenceLength);
+ out.write((byte) integerTag);
+ out.write((byte) lb1);
+ out.write(b1);
+ out.write((byte) integerTag);
+ out.write((byte) lb2);
+ out.write(b2);
+ return out.toByteArray();
+
+ }
}
diff --git a/src/main/java/org/uic/barcode/utils/package.html b/src/main/java/org/uic/barcode/utils/package.html index 45a73a9..73917d7 100644 --- a/src/main/java/org/uic/barcode/utils/package.html +++ b/src/main/java/org/uic/barcode/utils/package.html @@ -3,18 +3,19 @@ <head></head>
<body>
-<h1>Implementation of the header frames to bundle all data for a bar code.</h1>
+ <h1>Implementation of the header frames to bundle all data for a
+ bar code.</h1>
-<p>
+ <p>
- Provides a decoding and encoding of the header data frames for:<br/><br/>
-
- <ul>
- <li> static header as defined in UIC IRS 90918-9</li>
- <li> dynamic header as drafted for the UIC IRS 90918-9</li>
+ Provides a decoding and encoding of the header data frames for:<br />
+ <br />
+ <ul>
+ <li>static header as defined in UIC IRS 90918-9</li>
+ <li>dynamic header as drafted for the UIC IRS 90918-9</li>
+
+ </ul>
+ </p>
- </ul>
-</p>
-
</body>
</html>
\ No newline at end of file |