Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CoseHeaderLabel"/> struct.
        /// </summary>
        /// <param name="label">The header label as a text string.</param>
        /// <exception cref="ArgumentNullException"><paramref name="label"/> is <see langword="null"/>.</exception>
        public CoseHeaderLabel(string label)
        {
            if (label is null)
            {
                throw new ArgumentNullException(nameof(label));
            }

            this          = default;
            LabelAsString = label;
            EncodedSize   = CoseHelpers.GetTextStringEncodedSize(label);
        }
Пример #2
0
        public bool VerifyEmbedded(AsymmetricAlgorithm key, byte[]?associatedData = null)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (Message.IsDetached)
            {
                throw new InvalidOperationException(SR.ContentWasDetached);
            }

            return(VerifyCore(key, Message.Content.Value.Span, null, associatedData, CoseHelpers.GetKeyType(key)));
        }
Пример #3
0
        public bool VerifyDetached(AsymmetricAlgorithm key, ReadOnlySpan <byte> detachedContent, ReadOnlySpan <byte> associatedData = default)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (!Message.IsDetached)
            {
                throw new InvalidOperationException(SR.ContentWasEmbedded);
            }

            return(VerifyCore(key, detachedContent, null, associatedData, CoseHelpers.GetKeyType(key)));
        }
Пример #4
0
        public CoseSigner(AsymmetricAlgorithm key, HashAlgorithmName hashAlgorithm, CoseHeaderMap?protectedHeaders = null, CoseHeaderMap?unprotectedHeaders = null)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (key is RSA)
            {
                throw new CryptographicException(SR.CoseSignerRSAKeyNeedsPadding);
            }

            Key           = key;
            HashAlgorithm = hashAlgorithm;

            _protectedHeaders     = protectedHeaders;
            _unprotectedHeaders   = unprotectedHeaders;
            _keyType              = CoseHelpers.GetKeyType(key);
            _algHeaderValueToSlip = ValidateOrSlipAlgorithmHeader();
        }
Пример #5
0
        private void ValidateAlgorithmHeader(ReadOnlyMemory <byte> encodedAlg, int expectedAlg)
        {
            int?alg = CoseHelpers.DecodeCoseAlgorithmHeader(encodedAlg);

            Debug.Assert(alg.HasValue, "Algorithm (alg) is a known header and should have been validated in Set[Encoded]Value()");

            if (expectedAlg != alg.Value)
            {
                string exMsg;
                if (_keyType == KeyType.RSA)
                {
                    exMsg = SR.Format(SR.Sign1SignCoseAlgorithmDoesNotMatchSpecifiedKeyHashAlgorithmAndPadding, alg.Value, _keyType, HashAlgorithm.Name, RSASignaturePadding);
                }
                else
                {
                    exMsg = SR.Format(SR.Sign1SignCoseAlgorithmDoesNotMatchSpecifiedKeyAndHashAlgorithm, alg.Value, _keyType, HashAlgorithm.Name);
                }

                throw new CryptographicException(exMsg);
            }
        }
Пример #6
0
        public CoseSigner(RSA key, RSASignaturePadding signaturePadding, HashAlgorithmName hashAlgorithm, CoseHeaderMap?protectedHeaders = null, CoseHeaderMap?unprotectedHeaders = null)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (signaturePadding is null)
            {
                throw new ArgumentNullException(nameof(signaturePadding));
            }

            Key                 = key;
            HashAlgorithm       = hashAlgorithm;
            RSASignaturePadding = signaturePadding;

            _protectedHeaders     = protectedHeaders;
            _unprotectedHeaders   = unprotectedHeaders;
            _keyType              = CoseHelpers.GetKeyType(key);
            _algHeaderValueToSlip = ValidateOrSlipAlgorithmHeader();
        }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CoseHeaderLabel"/> struct.
 /// </summary>
 /// <param name="label">The header label as an integer.</param>
 public CoseHeaderLabel(int label)
 {
     this         = default;
     LabelAsInt32 = label;
     EncodedSize  = CoseHelpers.GetIntegerEncodedSize(label);
 }