Exemplo n.º 1
0
        public void PerformSignature()
        {
            CBORObject cborProtected = CBORObject.FromObject(new byte[0]);

            if ((ProtectedMap != null) && (ProtectedMap.Count > 0))
            {
                byte[] rgb = ProtectedMap.EncodeToBytes();
                cborProtected = CBORObject.FromObject(rgb);
            }

            if (_rgbSignature == null)
            {
                CBORObject signObj = CBORObject.NewArray();
                signObj.Add(_context);
                signObj.Add(cborProtected);
                signObj.Add(ExternalData); // External AAD
                signObj.Add(rgbContent);

                _rgbSignature = _Sign(toBeSigned());

#if FOR_EXAMPLES
                m_toBeSigned = signObj.EncodeToBytes();
#endif
            }
        }
Exemplo n.º 2
0
        public CBORObject EncodeToCBORObject(byte[] bodyAttributes, byte[] body)
        {
            CBORObject obj = CBORObject.NewArray();

            CBORObject cborProtected = CBORObject.FromObject(new byte[0]);

            if ((ProtectedMap != null) && (ProtectedMap.Count > 0))
            {
                byte[] rgb = ProtectedMap.EncodeToBytes();
                cborProtected = CBORObject.FromObject(rgb);
            }

            ProtectedBytes = cborProtected.GetByteString();
            obj.Add(cborProtected);

            if (rgbSignature == null)
            {
                rgbSignature = Sign(toBeSigned(body, bodyAttributes));
            }

            ProcessCounterSignatures();

            if ((UnprotectedMap == null))
            {
                obj.Add(CBORObject.NewMap());
            }
            else
            {
                obj.Add(UnprotectedMap); // Add unprotected attributes
            }

            obj.Add(rgbSignature);
            return(obj);
        }
Exemplo n.º 3
0
        private byte[] BuildContentBytes()
#endif
        {
            CBORObject obj = CBORObject.NewArray();

            obj.Add(_strContext);
            if (ProtectedBytes == null)
            {
                if (ProtectedMap.Count > 0)
                {
                    ProtectedBytes = ProtectedMap.EncodeToBytes();
                }
                else
                {
                    ProtectedBytes = new byte[0];
                }
            }
            obj.Add(ProtectedBytes);
            if (ExternalData != null)
            {
                obj.Add(CBORObject.FromObject(ExternalData));
            }
            else
            {
                obj.Add(CBORObject.FromObject(new byte[0]));
            }
            obj.Add(rgbContent);

            return(obj.EncodeToBytes());
        }
Exemplo n.º 4
0
        public override CBORObject Encode()
        {
            CBORObject obj;

            obj = CBORObject.NewArray();

            if ((ProtectedMap != null) && (ProtectedMap.Count > 0))
            {
                obj.Add(ProtectedMap.EncodeToBytes());
            }
            else
            {
                obj.Add(new byte[0]);
            }

            ProtectedBytes = obj[0].GetByteString();

            ProcessCounterSignatures();

            if ((UnprotectedMap == null) || (UnprotectedMap.Count == 0))
            {
                obj.Add(CBORObject.NewMap());
            }
            else
            {
                obj.Add(UnprotectedMap);  // Add unprotected attributes
            }
            obj.Add(rgbContent);

            PerformSignature();

            obj.Add(_rgbSignature);
            return(obj);
        }
Exemplo n.º 5
0
        protected byte[] toBeSigned(byte[] rgbContent, byte[] bodyAttributes)
        {
            CBORObject cborProtected = CBORObject.FromObject(new byte[0]);

            if ((ProtectedMap != null) && (ProtectedMap.Count > 0))
            {
                byte[] rgb = ProtectedMap.EncodeToBytes();
                cborProtected = CBORObject.FromObject(rgb);
            }

            if (rgbContent == null)
            {
                rgbContent = new byte[0];
            }

            CBORObject signObj = CBORObject.NewArray();

            signObj.Add(context);
            signObj.Add(bodyAttributes);
            signObj.Add(cborProtected);
            signObj.Add(ExternalData);
            signObj.Add(rgbContent);

#if FOR_EXAMPLES
            m_toBeSigned = signObj.EncodeToBytes();
#endif
            return(signObj.EncodeToBytes());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Encode the COSE Encrypt0 item to a CBOR tree.
        /// <see cref="Encrypt"/> must be done prior to calling this function.
        /// </summary>
        /// <returns></returns>
        public override CBORObject Encode()
        {
            CBORObject cbor;

            if (RgbEncrypted == null)
            {
                throw new CoseException("Must call Encrypt first");
            }

            ProcessCounterSignatures();

            cbor = CBORObject.NewArray();

            if (ProtectedMap.Count > 0)
            {
                cbor.Add(ProtectedMap.EncodeToBytes());
            }
            else
            {
                cbor.Add(CBORObject.FromObject(new byte[0]));
            }

            cbor.Add(UnprotectedMap); // Add unprotected attributes

            if (m_emitContent)
            {
                cbor.Add(RgbEncrypted);                     // Add ciphertext
            }
            else
            {
                cbor.Add(CBORObject.Null);
            }

            return(cbor);
        }
Exemplo n.º 7
0
        public override CBORObject Encode()
        {
            CBORObject obj;

            if (RgbTag == null)
            {
                MAC();
            }

            obj = CBORObject.NewArray();

            if (ProtectedMap.Count > 0)
            {
                obj.Add(ProtectedMap.EncodeToBytes());
            }
            else
            {
                obj.Add(new byte[0]);
            }

            if (UnprotectedMap.Count > 0)
            {
                obj.Add(UnprotectedMap);                           // Add unprotected attributes
            }
            else
            {
                obj.Add(CBORObject.NewMap());
            }

            obj.Add(rgbContent);      // Add ciphertext
            obj.Add(RgbTag);

            if ((!m_forceArray) && (_recipientList.Count == 1))
            {
                CBORObject recipient = _recipientList[0].Encode();

                for (int i = 0; i < recipient.Count; i++)
                {
                    obj.Add(recipient[i]);
                }
            }
            else if (_recipientList.Count > 0)
            {
                CBORObject recipients = CBORObject.NewArray();

                foreach (Recipient key in _recipientList)
                {
                    recipients.Add(key.Encode());
                }
                obj.Add(recipients);
            }
            else
            {
                obj.Add(null);      // No recipients - set to null
            }

            return(obj);
        }
Exemplo n.º 8
0
        public byte[] getAADBytes()
        {
            CBORObject obj = CBORObject.NewArray();

            obj.Add(_context);
            if (ProtectedMap.Count == 0)
            {
                ProtectedBytes = new byte[0];
            }
            else
            {
                ProtectedBytes = ProtectedMap.EncodeToBytes();
            }

            obj.Add(ProtectedBytes);

            obj.Add(CBORObject.FromObject(ExternalData));

            // Console.WriteLine("COSE AAD = " + BitConverter.ToString(obj.EncodeToBytes()));

            return(obj.EncodeToBytes());
        }
Exemplo n.º 9
0
        private byte[] toBeSigned()
        {
            CBORObject cborProtected = CBORObject.FromObject(new byte[0]);

            if ((ProtectedMap != null) && (ProtectedMap.Count > 0))
            {
                byte[] rgb = ProtectedMap.EncodeToBytes();
                cborProtected = CBORObject.FromObject(rgb);
            }

            CBORObject signObj = CBORObject.NewArray();

            signObj.Add(_context);
            signObj.Add(cborProtected);
            signObj.Add(ExternalData); // External AAD
            signObj.Add(rgbContent);

#if FOR_EXAMPLES
            m_toBeSigned = signObj.EncodeToBytes();
#endif

            return(signObj.EncodeToBytes());
        }
Exemplo n.º 10
0
        public override CBORObject Encode()
        {
            CBORObject obj;

            if (RgbTag == null)
            {
                throw new CoseException("Must call Compute before encoding");
            }
            ProcessCounterSignatures();

            obj = CBORObject.NewArray();

            if (ProtectedMap.Count > 0)
            {
                obj.Add(ProtectedMap.EncodeToBytes());
            }
            else
            {
                obj.Add(new byte[0]);
            }



            if (UnprotectedMap.Count > 0)
            {
                obj.Add(UnprotectedMap);                           // Add unprotected attributes
            }
            else
            {
                obj.Add(CBORObject.NewMap());
            }

            obj.Add(rgbContent);      // Add ciphertext
            obj.Add(RgbTag);

            return(obj);
        }
Exemplo n.º 11
0
        public override CBORObject Encode()
        {
            CBORObject obj;

            byte[] rgbProtected;

            obj = CBORObject.NewArray();

            if ((ProtectedMap != null) && (ProtectedMap.Count > 0))
            {
                rgbProtected = ProtectedMap.EncodeToBytes();
                obj.Add(rgbProtected);
            }
            else
            {
                rgbProtected = new byte[0];
                obj.Add(rgbProtected);
            }

            if (CounterSignerList.Count() != 0)
            {
                if (CounterSignerList.Count() == 1)
                {
                    AddAttribute(HeaderKeys.CounterSignature, CounterSignerList[0].EncodeToCBORObject(rgbProtected, rgbContent), UNPROTECTED);
                }
                else
                {
                    foreach (CounterSignature sig in CounterSignerList)
                    {
                        sig.EncodeToCBORObject(rgbProtected, rgbContent);
                    }
                }
            }

            if ((UnprotectedMap == null) || (UnprotectedMap.Count == 0))
            {
                obj.Add(CBORObject.NewMap());
            }
            else
            {
                obj.Add(UnprotectedMap);  // Add unprotected attributes
            }
            obj.Add(rgbContent);

            if ((signerList.Count == 1) && !m_forceArray)
            {
                CBORObject recipient = signerList[0].EncodeToCBORObject(obj[0].EncodeToBytes(), rgbContent);

                for (int i = 0; i < recipient.Count; i++)
                {
                    obj.Add(recipient[i]);
                }
            }
            else if (signerList.Count > 0)
            {
                CBORObject signers = CBORObject.NewArray();

                foreach (Signer key in signerList)
                {
                    signers.Add(key.EncodeToCBORObject(rgbProtected, rgbContent));
                }
                obj.Add(signers);
            }
            else
            {
                obj.Add(null);      // No recipients - set to null
            }
            return(obj);
        }