jrihtarsic commented on code in PR #234:
URL: 
https://github.com/apache/santuario-xml-security-java/pull/234#discussion_r1431807387


##########
src/main/java/org/apache/xml/security/utils/DERDecoderUtils.java:
##########
@@ -0,0 +1,250 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.xml.security.utils;
+
+import org.apache.xml.security.exceptions.DERDecodingException;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigInteger;
+import java.security.PublicKey;
+
+/**
+ * Provides the means to navigate through a DER-encoded byte array, to help
+ * in decoding the contents.
+ * <p>
+ * It maintains a "current position" in the array that advances with each
+ * operation, providing a simple means to handle the type-length-value
+ * encoding of DER. For example
+ * <pre>
+ *   decoder.expect(TYPE);
+ *   int length = decoder.getLength();
+ *   byte[] value = decoder.getBytes(len);
+ * </pre>
+ */
+public class DERDecoderUtils {
+    private static final System.Logger LOG = 
System.getLogger(DERDecoderUtils.class.getName());
+
+    /**
+     * DER type identifier for a bit string value
+     */
+    public static final byte TYPE_BIT_STRING = 0x03;
+    /**
+     * DER type identifier for a octet string value
+     */
+    public static final byte TYPE_OCTET_STRING = 0x04;
+    /**
+     * DER type identifier for a sequence value
+     */
+    public static final byte TYPE_SEQUENCE = 0x30;
+    /**
+     * DER type identifier for ASN.1 "OBJECT IDENTIFIER" value.
+     */
+    public static final byte TYPE_OBJECT_IDENTIFIER = 0x06;
+
+    /**
+     * Simple method parses an ASN.1 encoded byte array. The encoding uses 
"DER", a BER/1 subset, that means a triple { typeId, length, data }.
+     * with the following structure:
+     * <p>
+     * PublicKeyInfo ::= SEQUENCE {
+     * algorithm   AlgorithmIdentifier,
+     * PublicKey   BIT STRING
+     * }
+     * <p>
+     * Where AlgorithmIdentifier is formatted as:
+     * AlgorithmIdentifier ::= SEQUENCE {
+     * algorithm   OBJECT IDENTIFIER,
+     * parameters  ANY DEFINED BY algorithm OPTIONAL
+     * }
+     *
+     * @param derEncodedIS the DER-encoded input stream to decode.
+     * @throws DERDecodingException if the given array is null.
+     */
+    public static byte[] getAlgorithmIdBytes(InputStream derEncodedIS) throws 
DERDecodingException, IOException {
+        if (derEncodedIS == null || derEncodedIS.available() <= 0) {
+            throw new DERDecodingException("DER decoding error: Null data");
+        }
+
+        validateType(derEncodedIS.read(), TYPE_SEQUENCE);
+        readLength(derEncodedIS);
+        validateType(derEncodedIS.read(), TYPE_SEQUENCE);
+        readLength(derEncodedIS);
+
+        return readObjectIdentifier(derEncodedIS);
+    }
+
+    /**
+     * Read the next object identifier from the given DER-encoded input stream.
+     * <p>
+     * @param derEncodedIS the DER-encoded input stream to decode.
+     * @return the object identifier as a byte array.
+     * @throws DERDecodingException if the given array is null.

Review Comment:
   The "throws" Description is now updated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to