Repository: dotnet/runtime
File: src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseMessage.cs (C#)

Code before:
                    CborReaderState.UnsignedInteger or CborReaderState.NegativeInteger => new CoseHeaderLabel(reader.ReadInt32()),
                    CborReaderState.TextString => new CoseHeaderLabel(reader.ReadTextString()),
                    _ => throw new CryptographicException(SR.Format(SR.DecodeErrorWhileDecoding, SR.DecodeSign1MapLabelWasIncorrect))
                };

                CoseHeaderValue value = CoseHeaderValue.FromEncodedValue(reader.ReadEncodedValue().Span);
                headerParameters.Add(label, value);
            }
            reader.ReadEndMap();
        }

        private static byte[]? DecodePayload(CborReader reader)
        {
            CborReaderState state = reader.PeekState();
            if (state == CborReaderState.Null)
            {
                reader.ReadNull();
                return null;
            }
            if (state == CborReaderState.ByteString)
            {
                return reader.ReadByteString();
            }

            throw new CryptographicException(SR.Format(SR.DecodeErrorWhileDecoding, SR.DecodeSign1PayloadWasIncorrect));
        }

        private static byte[] DecodeSignature(CborReader reader)
        {
            return reader.ReadByteString();
        }

        private static List<CoseSignature> DecodeCoseSignaturesArray(CborReader reader)
        {
            int? signaturesLength = reader.ReadStartArray();
            List<CoseSignature> signatures = new List<CoseSignature>(signaturesLength.GetValueOrDefault());

            while (reader.PeekState() == CborReaderState.StartArray)
            {
                CoseSignature signature = DecodeCoseSignature(reader.ReadEncodedValue());
                signatures.Add(signature);
            }

            reader.ReadEndArray();

            if (signatures.Count < 1)
            {
                throw new CryptographicException(SR.Format(SR.DecodeErrorWhileDecoding, SR.MultiSignMessageMustCarryAtLeastOneSignature));
            }

            return signatures;

Review comment (suggestion):
When translating the `ArgumentException` from `CoseHeaderMap.Add` into a `CryptographicException`, the inner exception is currently dropped (`new CryptographicException(e.Message)`). Some validation errors intentionally carry an inner exception (and the message may even say "see inner exception"). Preserve `e.InnerException` when rethrowing. Also consider comparing `e.ParamName` to `nameof(value)` instead of the string literal "value" for robustness.
```suggestion
                    if (e.ParamName == nameof(value))
                    {
                        throw new CryptographicException(e.Message, e.InnerException);
```

Code after:

                if (state == CborReaderState.EndMap)
                {
                    reader.ReadEndMap();
                    break;
                }

                CoseHeaderLabel label = state switch
                {
                    CborReaderState.UnsignedInteger or CborReaderState.NegativeInteger => new CoseHeaderLabel(reader.ReadInt32()),
                    CborReaderState.TextString => new CoseHeaderLabel(reader.ReadTextString()),
                    _ => throw new CryptographicException(SR.Format(SR.DecodeErrorWhileDecoding, SR.DecodeSign1MapLabelWasIncorrect))
                };

                CoseHeaderValue value = CoseHeaderValue.FromEncodedValue(reader.ReadEncodedValue().Span);

                try
                {
                    headerParameters.Add(label, value);
                }
                catch (ArgumentException e)
                {
                    // Lift the well-known header value validation into a CryptographicException.
                    if (e.ParamName == "value")
                    {
                        throw new CryptographicException(e.Message);
                    }

                    Debug.Fail("Unexpected ArgumentException from CoseHeaderMap.Add");
                    throw new CryptographicException(SR.DecodeErrorWhileDecodingSeeInnerEx, e);
                }
            }
        }

        private static byte[]? DecodePayload(CborReader reader)
        {
            CborReaderState state = reader.PeekState();
            if (state == CborReaderState.Null)
            {
                reader.ReadNull();
                return null;
            }
            if (state == CborReaderState.ByteString)
            {
                return reader.ReadByteString();
            }

            throw new CryptographicException(SR.Format(SR.DecodeErrorWhileDecoding, SR.DecodeSign1PayloadWasIncorrect));
        }

        private static byte[] DecodeSignature(CborReader reader)