Beispiel #1
0
 /// <summary>
 /// Create a generic DER node with the given nodeType. This is a private
 /// constructor used by one of the public DerNode subclasses defined below.
 /// </summary>
 ///
 /// <param name="nodeType">The DER node type, a value from DerNodeType.</param>
 private DerNode(int nodeType)
 {
     this.parent_ = null;
     this.header_ = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(0);
     this.payload_ = new DynamicByteBuffer(0);
     nodeType_ = nodeType;
 }
Beispiel #2
0
            /// <summary>
            /// Override the base encode to return raw data encoding for this node and
            /// its children
            /// </summary>
            ///
            /// <returns>The raw data encoding.</returns>
            public override Blob encode()
            {
                DynamicByteBuffer temp = new DynamicByteBuffer(10);
                updateSize();
                encodeHeader(size_);
                temp.ensuredPut(header_);

                for (int i = 0; i < nodeList_.Count; ++i) {
                    DerNode n = nodeList_[i];
                    Blob encodedChild = n.encode();
                    temp.ensuredPut(encodedChild.buf());
                }

                return new Blob(temp.flippedBuffer(), false);
            }
Beispiel #3
0
            /// <summary>
            /// Encode a sequence of integers into an OID object and set the payload.
            /// </summary>
            ///
            /// <param name="value">The array of integers.</param>
            public void prepareEncoding(int[] value_ren)
            {
                int firstNumber = 0;
                if (value_ren.Length == 0)
                    throw new DerEncodingException("No integer in OID");
                else {
                    if (value_ren[0] >= 0 && value_ren[0] <= 2)
                        firstNumber = value_ren[0] * 40;
                    else
                        throw new DerEncodingException(
                                "First integer in OID is out of range");
                }

                if (value_ren.Length >= 2) {
                    if (value_ren[1] >= 0 && value_ren[1] <= 39)
                        firstNumber += value_ren[1];
                    else
                        throw new DerEncodingException(
                                "Second integer in OID is out of range");
                }

                DynamicByteBuffer encodedBuffer = new DynamicByteBuffer(10);
                encodedBuffer.ensuredPut(encode128(firstNumber));

                if (value_ren.Length > 2) {
                    for (int i = 2; i < value_ren.Length; ++i)
                        encodedBuffer.ensuredPut(encode128(value_ren[i]));
                }

                encodeHeader(encodedBuffer.position());
                payload_.ensuredPut(encodedBuffer.flippedBuffer());
            }
Beispiel #4
0
            /// <summary>
            /// Compute the encoding for one part of an OID, where values greater than 128 must be encoded as multiple bytes.
            /// </summary>
            ///
            /// <param name="value">A component of an OID.</param>
            /// <returns>The encoded buffer.</returns>
            public static ByteBuffer encode128(int value_ren)
            {
                int mask = (1 << 7) - 1;
                DynamicByteBuffer outBytes = new DynamicByteBuffer(10);
                // We encode backwards from the back.
                outBytes.position(outBytes.limit());

                if (value_ren < 128)
                    outBytes.ensuredPutFromBack((byte) (value_ren & mask));
                else {
                    outBytes.ensuredPutFromBack((byte) (value_ren & mask));
                    value_ren >>= 7;
                    while (value_ren != 0) {
                        outBytes.ensuredPutFromBack((byte) ((value_ren & mask) | (1 << 7)));
                        value_ren >>= 7;
                    }
                }

                return outBytes.buffer().slice();
            }
Beispiel #5
0
        /// <summary>
        /// Encode the given size and update the header.
        /// </summary>
        ///
        /// <param name="size">The payload size to encode.</param>
        protected internal void encodeHeader(int size)
        {
            DynamicByteBuffer buffer = new DynamicByteBuffer(10);
            buffer.ensuredPut((byte) nodeType_);
            if (size < 0)
                // We don't expect this to happen since this is a protected method and
                // always called with the non-negative size() of some buffer.
                throw new Exception("encodeHeader: DER object has negative length");
            else if (size <= 127)
                buffer.ensuredPut((byte) (size & 0xff));
            else {
                DynamicByteBuffer tempBuf = new DynamicByteBuffer(10);
                // We encode backwards from the back.
                tempBuf.position(tempBuf.limit());

                int val = size;
                int n = 0;
                while (val != 0) {
                    tempBuf.ensuredPutFromBack((byte) (val & 0xff));
                    val >>= 8;
                    n += 1;
                }
                tempBuf.ensuredPutFromBack((byte) (((1 << 7) | n) & 0xff));

                buffer.ensuredPut(tempBuf.buffer());
            }

            header_ = buffer.flippedBuffer();
        }
Beispiel #6
0
            /// <summary>
            /// Create a new DerInteger for the value.
            /// </summary>
            ///
            /// <param name="integer">The value to encode.</param>
            public DerInteger(int integer)
                : base(net.named_data.jndn.encoding.der.DerNodeType.Integer)
            {
                if (integer < 0)
                    throw new DerEncodingException(
                            "DerInteger: Negative integers are not currently supported");

                // Convert the integer to bytes the easy/slow way.
                DynamicByteBuffer temp = new DynamicByteBuffer(10);
                // We encode backwards from the back.
                temp.position(temp.limit());
                while (true) {
                    temp.ensuredPutFromBack((byte) (integer & 0xff));
                    integer >>= 8;

                    if (integer <= 0)
                        // We check for 0 at the end so we encode one byte if it is 0.
                        break;
                }

                if ((((int) temp.buffer().get(temp.position())) & 0xff) >= 0x80)
                    // Make it a non-negative integer.
                    temp.ensuredPutFromBack((byte) 0);

                payload_.ensuredPut(temp.buffer().slice());
                encodeHeader(payload_.position());
            }
Beispiel #7
0
        /// <summary>
        /// Extract the header from an input buffer and return the size.
        /// </summary>
        ///
        /// <param name="inputBuf">position.</param>
        /// <param name="startIdx">The offset into the buffer.</param>
        /// <returns>The parsed size in the header.</returns>
        protected internal int decodeHeader(ByteBuffer inputBuf, int startIdx)
        {
            int idx = startIdx;

            int nodeType = ((int) inputBuf.get(idx)) & 0xff;
            idx += 1;

            nodeType_ = nodeType;

            int sizeLen = ((int) inputBuf.get(idx)) & 0xff;
            idx += 1;

            DynamicByteBuffer header = new DynamicByteBuffer(10);
            header.ensuredPut((byte) nodeType);
            header.ensuredPut((byte) sizeLen);

            int size = sizeLen;
            bool isLongFormat = (sizeLen & (1 << 7)) != 0;
            if (isLongFormat) {
                int lenCount = sizeLen & ((1 << 7) - 1);
                size = 0;
                while (lenCount > 0) {
                    byte b = inputBuf.get(idx);
                    idx += 1;
                    header.ensuredPut(b);
                    size = 256 * size + (((int) b) & 0xff);
                    lenCount -= 1;
                }
            }

            header_ = header.flippedBuffer();
            return size;
        }
Beispiel #8
0
        /// <summary>
        /// Get the raw data encoding for this node.
        /// </summary>
        ///
        /// <returns>The raw data encoding.</returns>
        public virtual Blob encode()
        {
            DynamicByteBuffer buffer = new DynamicByteBuffer(getSize());

            buffer.ensuredPut(header_);
            buffer.ensuredPut(payload_.flippedBuffer());

            return new Blob(buffer.flippedBuffer(), false);
        }
 /// <summary>
 /// Create a new ElementReader with the elementListener.
 /// </summary>
 ///
 /// <param name="elementListener">The ElementListener used by onReceivedData.</param>
 public ElementReader(ElementListener elementListener)
 {
     this.tlvStructureDecoder_ = new TlvStructureDecoder();
     this.partialData_ = new DynamicByteBuffer(1000);
     elementListener_ = elementListener;
 }
Beispiel #10
0
 /// <summary>
 /// Create a new TlvEncoder with a default DynamicByteBuffer.
 /// When done, you should call getOutput().
 /// </summary>
 ///
 public TlvEncoder()
 {
     output_ = new DynamicByteBuffer(16);
     // We will start encoding from the back.
     output_.position(output_.limit());
 }
Beispiel #11
0
 /// <summary>
 /// Create a new TlvEncoder to use a DynamicByteBuffer with the initialCapacity.
 /// When done, you should call getOutput().
 /// </summary>
 ///
 /// <param name="initialCapacity">The initial capacity of buffer().</param>
 public TlvEncoder(int initialCapacity)
 {
     output_ = new DynamicByteBuffer(initialCapacity);
     // We will start encoding from the back.
     output_.position(output_.limit());
 }