ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/util/buf/Asn1Parser.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/util/buf/Asn1Parser.java
Status
scanned
Type
file
Name
Asn1Parser.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
6134 (6.0 KB)
MD5
2f468ce5b45a5494954ccf88bcb5e5cc
SHA1
a389aa157ba6a806f94a2a8a7545cc85ff074a13
SHA256
ca1eda0755bcd8a42657251ebc0ee9fb694b8c50fced94624a74869d44f0a5a3
SHA512

      
    
SHA1_git
c05e5e9976daea4946151de012f16f866ef0c042
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
Asn1Parser.java | 6.0 KB |

/* * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.tomcat.util.buf; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.ArrayDeque; import java.util.Deque; import org.apache.tomcat.util.res.StringManager; /** * This is a very basic ASN.1 parser that provides the limited functionality required by Tomcat. It is a long way from a * complete parser. * <p> * TODO: Consider extending/re-writing this parser and refactoring the SpnegoTokenFixer to use it. */ public class Asn1Parser { private static final StringManager sm = StringManager.getManager(Asn1Parser.class); public static final int TAG_INTEGER = 0x02; public static final int TAG_OCTET_STRING = 0x04; public static final int TAG_NULL = 0x05; public static final int TAG_OID = 0x06; public static final int TAG_UTF8STRING = 0x0C; public static final int TAG_SEQUENCE = 0x30; public static final int TAG_ATTRIBUTE_BASE = 0xA0; private final byte[] source; private int pos = 0; /* * This is somewhat of a hack to work around the simplified design of the parsing API that could result in ambiguous * results when nested sequences were optional. Checking the current nesting level of sequence tags enables a user * of the parser to determine if an optional sequence is present or not. * * See https://bz.apache.org/bugzilla/show_bug.cgi?id=67675#c24 */ private final Deque<Integer> nestedSequenceEndPositions = new ArrayDeque<>(); public Asn1Parser(byte[] source) { this.source = source; } public boolean eof() { return pos == source.length; } public int peekTag() { return source[pos] & 0xFF; } public void parseTagSequence() { /* * Check to see if the parser has completely parsed, based on end position for the sequence, any previous * sequences and remove those sequences from the sequence nesting tracking mechanism if they have been * completely parsed. */ while (!nestedSequenceEndPositions.isEmpty()) { if (nestedSequenceEndPositions.peekLast().intValue() <= pos) { nestedSequenceEndPositions.pollLast(); } else { break; } } // Add the new sequence to the sequence nesting tracking mechanism. parseTag(TAG_SEQUENCE); nestedSequenceEndPositions.addLast(Integer.valueOf(-1)); } public void parseTag(int tag) { int value = next(); if (value != tag) { throw new IllegalArgumentException( sm.getString("asn1Parser.tagMismatch", Integer.valueOf(tag), Integer.valueOf(value))); } } public void parseFullLength() { int len = parseLength(); if (len + pos != source.length) { throw new IllegalArgumentException(sm.getString("asn1Parser.lengthInvalid", Integer.valueOf(len), Integer.valueOf(source.length - pos))); } } public int parseLength() { int len = next(); if (len > 127) { int bytes = len - 128; len = 0; for (int i = 0; i < bytes; i++) { len = len << 8; len = len + next(); } } /* * If this is the first length parsed after a sequence has been added to the sequence nesting tracking mechanism * it must be the length of the sequence so update the entry to record the end position of the sequence. Note * that position recorded is actually the start of the first element after the sequence ends. */ if (nestedSequenceEndPositions.peekLast() != null && nestedSequenceEndPositions.peekLast().intValue() == -1) { nestedSequenceEndPositions.pollLast(); nestedSequenceEndPositions.addLast(Integer.valueOf(pos + len)); } return len; } public BigInteger parseInt() { byte[] val = parseBytes(TAG_INTEGER); return new BigInteger(val); } public byte[] parseOctetString() { return parseBytes(TAG_OCTET_STRING); } public void parseNull() { parseBytes(TAG_NULL); } public byte[] parseOIDAsBytes() { return parseBytes(TAG_OID); } public String parseUTF8String() { byte[] val = parseBytes(TAG_UTF8STRING); return new String(val, StandardCharsets.UTF_8); } public byte[] parseAttributeAsBytes(int index) { return parseBytes(TAG_ATTRIBUTE_BASE + index); } private byte[] parseBytes(int tag) { parseTag(tag); int len = parseLength(); byte[] result = new byte[len]; System.arraycopy(source, pos, result, 0, result.length); pos += result.length; return result; } public void parseBytes(byte[] dest) { System.arraycopy(source, pos, dest, 0, dest.length); pos += dest.length; } private int next() { return source[pos++] & 0xFF; } public int getNestedSequenceLevel() { return nestedSequenceEndPositions.size(); } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
17.58
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9
https://bz.apache.org/bugzilla/show_bug.cgi?id=67675#c24 53 53