示例#1
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);
            }
示例#2
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());
            }
示例#3
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;
        }
示例#4
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();
        }
示例#5
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);
        }