Commit Graph
517 Commits
Author SHA1 Message Date
Jeremy Barton df5b118c6f Seed the unified System.Security.Cryptography.dll
* All of System.Security.Cryptography.Primitives has been moved
* AsymmericKeyExchangeFormatter moved from S.S.C.Algorithms
* This just starts the process to keep the infrastructure pieces separate from code layout pieces.
2021-11-01 22:30:26 -07:00
Viktor Hofer 36b6247cc7 Update solution files for correct ordering of elements (#58655)
With the newest udpate of slngen, the order of elements (configuration, folders, etc.) is in sync with how Visual Studio arranges them.
Also the order is now deterministic.

Based on that, updating all solution files to have the correct ordering of elements. This change also unblocks adding CI protection for
making sure that solution files are up-to-date in regards to their dependencies as slngen is now generating the files deterministically.
2021-09-04 00:16:15 +02:00
Viktor Hofer a6b9a63f35 Update libraries solution and NuGet.config files (#56430)
* Update slngen and invocation

* Update NuGet.config files

* Update solution files with slngen
2021-07-28 18:44:17 +02:00
Stephen Toub 59a5610e8b Tighten up some ArrayPool.Return usage (#56229)
Avoid potential problems if Return were to throw an exception after having already put the array back into the pool.
2021-07-27 07:40:36 -04:00
Jeremy Barton 058a531581 Use checked math with CryptoStream buffer calculations
In .NET Framework, .NET 5, and .NET Core the block scaling logic was used for
`new byte[inputCount / inputBlockSize * outputBlockSize]` (or swap in/out).
That expression throws an OverflowException when the computed size
overflows.

Earlier in .NET 6 we changed to use ArrayPool.Rent.  When ArrayPool.Rent gets a
negative (overflowed) number it instead throws an ArgumentException.

This change restores the OverflowException, so that we're not leaking an
ArgumentException back to the caller.  (While the caller can control the
behavior with the arguments they pass to CryptoStream.Read/Write, the
description and paramName don't map in an obvious manner)

Making the scenario work has virtue, but requires a larger test investment than
we can make at this time.
2021-07-22 17:04:17 -07:00
Kevin JonesandJeremy Barton 2d3acc6a3d Add one-shot CFB encrypt/decrypt to SymmetricAlgorithm
Adds the CFB versions similar to the already-added ECB and CBC versions.

Co-authored-by: Jeremy Barton <[email protected]>
2021-07-13 11:01:31 -07:00
Kevin Jones bea43044f8 Fix one-shot CBC documentation (#55432)
* Fix one-shot CBC documentation.

The CBC one-shot documentation made a few references to the ECB
one shots. This removes them and uses consistent language that is
used on the other one-shot methods.

* Change ECB to be consistent
2021-07-09 20:01:05 -04:00
Kevin Jones 847cc5645d Implement CBC one shots on SymmetricAlgorithm 2021-07-07 11:12:12 -07:00
Kevin Jones 7298a534a0 Fix differences from S.S.C.Primitives autogenerated ref file. 2021-07-01 10:16:30 -07:00
Kevin JonesandJeremy Barton 6b5dbf6bdb Add one-shot ECB methods
This change adds SymmetricAlgorithm.EncryptEcb, SymmetricAlgorithm.DecryptEcb, their
respective Try- and -Core methods, derived type implementations thereof, and tests.

There's an open question of should these members on on the base class throw or
"succeed if the Mode property is in agreement with the algorithm".  While the latter is "nicer",
just throwing  is easier to reason about, and that's the current behavior.

Co-authored-by: Jeremy Barton <[email protected]>
2021-06-28 08:55:27 -07:00
Stephen Toub 68dec6ac17 Fix Deflate/Brotli/CryptoStream handling of partial and zero-byte reads (#53644)
Stream.Read{Async} is supposed to return once at least a byte of data is available, and in particular, if there's any data already available, it shouldn't block.  But Read{Async} on DeflateStream (and thus also GZipStream and ZLibStream), BrotliStream, and CryptoStream won't return until either it hits the end of the stream or the caller's buffer is filled.  This makes it behave very unexpectedly when used in a context where the app is using a large read buffer but expects to be able to process data as it's available, e.g. in networked streaming scenarios where messages are being sent as part of bidirectional communication.

This fixes that by stopping looping once any data is consumed.  Just doing that, though, caused problems for zero-byte reads.  Zero-byte reads are typically used by code that's trying to delay-allocate a buffer for the read data until data will be available to read.  At present, however, zero-byte reads return immediately regardless of whether data is available to be consumed.  I've changed the flow to make it so that zero-byte reads don't return until there's at least some data available as input to the inflater/transform (this, though, doesn't 100% guarantee the inflater/transform will be able to produce output data).

Note that both of these changes have the potential to introduce breaks into an app that erroneously depended on these implementation details:
- If an app passing in a buffer of size N to Read{Async} depended on that call always producing the requested number of bytes (rather than what the Stream contract defines), they might experience behavioral changes.
- If an app passing in a zero-byte buffer expected it to return immediately, it might instead end up waiting until data was actually available.
2021-06-11 17:31:15 -04:00
Levi Broderick b759ac9716 Expose Array.Clear(Array) as public API (#53388) 2021-05-27 21:08:53 -07:00
Filip Navara 5c80887836 Make DSA.Create, AesCcm, AesGcm, ChaCha20Poly1305 throw PNSE on iOS (#52978) 2021-05-21 08:56:05 +02:00
Jeff Handley 7ea26e68f9 Obsolete the Rijndael and RijndaelManaged classes 2021-05-06 09:28:41 -07:00
Elinor Fung 9c9eb90fae Switch to ProjectExclusions for assembly-level ActiveIssue tests on browser (#50891) 2021-04-08 18:37:21 -07:00
Jose Perez Rodriguez 7b0075d19e Moving Suppress message to assembly level and adding Scope in order to get right suppressing behavior (#50626)
* Moving Suppress message to assembly level and adding Scope in order to get right suppressing behavior

* Fixing indentation
2021-04-02 09:23:28 -07:00
Geoff KizerandGeoffrey Kizer 027c093de1 Improve zero-byte read handling in SslStream to reduce memory usage (#49123)
* add ZeroByteRead_PerformsZeroByteReadOnUnderlyingStreamWhenDataNeeded to stream conformance tests

* remove dead code

* change SslStream zero byte read implementation to issue zero-byte read on underlying stream when appropriate

* ensure buffer is always freed when not needed, and assert this

* create StreamConformanceTests assembly and reference from appropriate test projects

Co-authored-by: Geoffrey Kizer <[email protected]>
2021-03-07 13:02:28 -08:00
a7669a796c CryptoStream Memory-based ReadAsync/WriteAsync overrides
Co-authored-by: Stephen Toub <[email protected]>
Co-authored-by: Jeremy Barton <[email protected]>
2021-02-26 08:33:10 -08:00
Eric Erhardt ea0d37c53f Resolve ILLink warnings in System.Security.Cryptography.Primitives (#47853)
* Resolve ILLink warnings in System.Security.Cryptography.Primitives

Contributes to #45623
2021-02-04 14:47:56 -06:00
bbf9b676bd [master] Update dependencies from mono/linker (#47709)
* Update dependencies from https://github.com/mono/linker build 20210201.1

Microsoft.NET.ILLink.Tasks
 From Version 6.0.0-alpha.1.21079.1 -> To Version 6.0.0-alpha.1.21101.1

* Update dependencies from https://github.com/mono/linker build 20210201.2

Microsoft.NET.ILLink.Tasks
 From Version 6.0.0-alpha.1.21079.1 -> To Version 6.0.0-alpha.1.21101.2

* Update dependencies from https://github.com/mono/linker build 20210203.1

Microsoft.NET.ILLink.Tasks
 From Version 6.0.0-alpha.1.21079.1 -> To Version 6.0.0-alpha.1.21103.1

* Add warnings to baseline

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Mateo Torres Ruiz <[email protected]>
Co-authored-by: Marek Safar <[email protected]>
2021-02-04 14:05:26 +01:00
Stephen Toub 2b0e2785e5 Use [NotNullWhen(true)] in more places (#47598)
* Use [NotNullWhen(true)] in more places

Did a quick search/audit for methods that returned bool and took nullable object as the first input, and added [NotNullWhen(true)] where it was obviously correct.

* Address PR feedback
2021-01-30 06:09:28 -05:00
d448461805 Implement and use MultiArrayBuffer (#44980)
* add MultiArrayBuffer and use in StreamBuffer and Http2Stream

* rework EnsureAvailableSpace and add tests for it

* make dispose handling more robust, plus minor tweaks

* add AssertExtensions.SequenceEqual for providing more useful information about sequence differences, and use in StreamConformanceTests

* tweaks

* replace _blockCount with _allocatedEnd

* use uint instead of int internally to assure bitop optimizations

* fix _allocatedEnd calculation

* address PR feedback

* more PR feedback and some minor fixes/improvements

* small test improvements

* Apply suggestions from code review

Co-authored-by: Günther Foidl <[email protected]>

* address review feedback

Co-authored-by: Geoffrey Kizer <[email protected]>
Co-authored-by: Günther Foidl <[email protected]>
2021-01-06 11:06:15 -08:00
Stephen Toub 93cbc0974a Remove ForceAsync from CryptoStream (#45150) 2020-11-24 20:25:27 -05:00
Stephen Toub e2099f62f1 Use Type.EmptyTypes consistently (#45112)
We currently have several hundred uses of `Array.Empty<Type>()` and several hundred uses of `Type.EmptyTypes`.  This just changes the repo to use the latter consistently.
2020-11-23 15:48:27 -05:00
Geoff KizerandGeoffrey Kizer e8c4f227a3 disable ReadWrite_Success_Large test for CryptoStream because it takes too long to run (#45081)
Co-authored-by: Geoffrey Kizer <[email protected]>
2020-11-23 09:19:09 -05:00
Kevin Jones 028cde2931 Add GetCiphertextLength for CBC, CFB, and ECB. 2020-11-20 16:02:53 -08:00
Viktor Hofer ed295782bf Add workflow to update solution files with SlnGen and update them initially (#44703)
* Add slngen infra

* Update all solution files
2020-11-17 18:23:30 +01:00
Stephen Toub 7de7895e37 Add stream conformance tests for standalone streams (#44069)
* Nullable-enable stream conformance tests

* Add standalone stream conformance tests

* Address PR feedback
2020-11-02 20:53:00 -05:00
Stephen Toub a112379a67 Add Stream argument validation helpers and use throughout dotnet/runtime (#43968)
* Add Stream argument validation helpers and use throughout dotnet/runtime

* Delete unnecessary code in Stream

Stream.Null was using BlockingBegin/EndRead/Write, but the operations are nops (they don't even do argument validation), so the functionality was unnecessary complication.  We can just use TaskToApm instead, with already completed tasks, and then also delete the SynchronousAsyncResult that was used by these operations.

Also, the Begin/EndRead/Write methods were checking CanRead/Write and throwing if they return false, but this is a private sealed type with CanRead/Write hardcoded to return true! Delete.

Finally added consistency across Stream.Null to checking for cancellation.

* Clean up some style on Stream

Also consolidate a few resources and use a few existing ThrowHelpers where appropriate.

* Fix a few issues, and remove duplicative tests missed in conformance tests rollout

* Address PR feedback
2020-10-30 05:50:49 -04:00
Stephen Toub 0acbafe061 Add initial set of conformance tests for Stream (#43834)
* Fix invalid assert in StreamBuffer

This can be hit by calling code and thus shouldn't be an assert.  As an assert it prevents testing.

* Add ConnectedStreams.CreateUnidirectional

* Add initial set of Stream conformance tests

These primarily focus on "connected streams", ones that can be arranged to communicate with each other in a producer/consumer pattern or as a bidirectional communication mechanism, e.g. NetworkStream, PipeStream, SslStream wrapped around a NetworkStream, FileStream created around pipes, etc.  Later we can add more tests focused on standalone streams, e.g. FileStream created for an on-disk file, MemoryStream, etc.

* Add ConnectedStreams tests

These are currently helpers used by many other tests.  At some point they could become public API as well.

* Fix NetworkStream argument names

Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes).

* Add stream conformance tests for NetworkStream

* Add stream conformance tests for QuicStream

* Fix several CryptoStream behaviors

1. Flushing a stream that wraps another stream for writing should always flush that underlying stream, even if no additional data was written as part of the flush.
2. Argument validation should validate buffers are not null rather than null ref'ing on a null buffer.
3. Checks for the CryptoStream mode should come after argument validation.

* Add stream conformance tests for CryptoStream

* Fix FileStream argument names

Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes).

* Fix BufferedStream argument names

Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes).

* Add stream conformance tests for FileStream

Specifically when used in a connected fashion, wrapped around an anonymous or named pipe.

* Add stream conformance tests for BufferedStream

Specifically when used in a connected fashion, wrapped around some other connected stream.

* Add stream conformance tests for SslStream and NegotiateStream

* Fix PipeStream.Flush to not fail on readable streams

Consumers may expect Stream.Flush to be a nop if the stream is readable only, but PipeStream.Flush is throwing in that case.  Stop doing that.

* Add stream conformance tests for PipeStream

* Fix several BrotliStream behaviors

1. When passed a null buffer, it's throwing an exception with the argument name "array", even though the parameter's name is "buffer".
2. Even if there's no data written as part of the Flush{Async} call on a writeable stream, it should be calling flush on the wrapped stream.

* Fix several DeflateStream (and friends) issues

1. DeflateStream.Flush{Async} when writing needs to always flush the underlying stream, even if there's no data written as part of the flush call itself.
2. Several byte[] array arguments should be byte[] buffer. Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes).
3. DeflateStream.EndRead/Write needs to do additional state validation.
4. Not a bug, but simplify ReadAsync to match the sync Read implementation flow.

* Add stream conformance tests for Deflate/ZLib/GZip/BrotliStream

* Fix PipeReader/WriterStream argument validation

* Add stream conformance tests for PipeReader/WriterStream

* Remove erroneous asserts from Stream.ReadWriteTask

* Address PR feedback

* Fix a few tests in CI
2020-10-28 09:36:23 -04:00
Kenneth Pouncey 117dd1cd73 [browser][crypto] Remove restraining not supported attribute Primitives (#43387)
* [browser][crypto] Remove restraining not supported attribute

- The modules included within the System.Security.Cryptography.Primitives module should still be available for use outside of browser os.

* Address review comments.  Remove `<IncludePlatformAttributes>` attribute as well
2020-10-15 13:55:01 +02:00
Kevin Jones f0d7404c2d Remove EBS.Never from CipherMode.CFB. (#43310)
This was removed from src/ but ref/ was not updated to match.
2020-10-13 08:53:13 -04:00
Anirudh Agnihotry 94ec04c946 Cleaning up unused TargetsBrowser conditions (#41062)
* minor cleanup

* remove unused string
2020-08-19 19:35:23 -07:00
Maxim Lipnin 196fd1804d [browser] Mark APIs as unsupported on browser (#40612)
* Mark System.Security.Cryptography.OpenSsl as unsupported on browser

* Mark other System.Security.Cryptography APIs as unsupported on browser

* Mark System.Data.Odbc.* as unsupported on browser

* Mark System.DirectoryServices.Protocols APIs as unsupported on browser

* Mark System.Drawing.Common APIs as unsupported on browser

* Mark System.IO.Ports as unsupported on browser
2020-08-13 12:55:30 -04:00
Víťa Tauer 219c60e19d Added support for CFB
This change brings the Cipher Feedback (CFB) mode to .NET 5 with the same behaviors as .NET Framework around the non-standard application of PKCS#7 padding based on the feedback block size and decryption the data be algorithm blocksize-aligned.

* AES: CFB8 and CFB128
* TripleDES: CFB8 and CFB64
* DES: CFB8
* RC2: Not supported

Additionally, due to a lack of support in CNG, CFB is not supported on Windows 7.
2020-08-13 05:38:24 -07:00
Anirudh Agnihotry 621ed57461 Correcting Mismatch between ref and src (#39741)
* minor formatting

* ref changes
2020-07-30 14:52:46 -07:00
Viktor Hofer 809a06f451 Remove depprojs in favor of PackageReferences (#35606)
- Remove depprojs which currently binplace external references into the RefPath folders in favor of PackageReference and PackageDownload items.
- Build all configurations by default when building an individual project (either on the CLI or inside VS) same as with the official SDK. This enables .NETFramework Test Explorer support.
- Centrally define libraries that compose the shared framework instead of in each Directory.Build.props file to be able to build the targeting pack first and consume it in the OOB libraries.
- Use ProjectReferences to reference OOB projects. Compile against the reference assembly but use the implementation assembly app-local during runtime.
- Remove OOBs from the testhost and remove the testhost folder for .NETFramework as it isn't required anymore.
- Only binplace for $(NetCoreAppCurrent) to compose a) the targeting pack, b) the runtime pack, c) the testhost, d) a full closure for the shims.
- Use Targeting Packs for OOB projects (with their implicit assembly references) but still explicitly define granular references for .NETCoreApp configurations (DisableImplicitAssemblyReferences switch). Use the implicit targeting pack references in some Microsoft.Extensions.* cases.
- Remove placeholder configurations as they aren't needed anymore with explicit P2Ps vs Targeting Pack references.
- Remove implicit assembly references (ie for .NETFramework, mscorlib)
- Remove AssemblySearchPath hacks that were introduced with b7c4cb7 as the targeting pack is now used by default.
- Reduce unnecessary .NETFramework configurations that were added to run tests in favor of the already existing ref&src configurations.
- Stop hardcoding the paths for wasm assemblies and use the returned TargetPath of the ProjectReferences.
- Addressed formatting (ItemGroups, References at the bottom of the project file, ordering of references, use LibrariesProjectRoot instead of a relative path, unnecessary AssemblyName and RootNamespace properties which are identical to the project name, ordering of tfms)
- Revert "fix clean (#33758)"
2020-07-23 17:20:26 +02:00
Kevin Jones d129af65b2 Add CryptoStream.FlushFinalBlockAsync 2020-07-21 06:58:36 -07:00
Alexander Köplinger 6998f4c575 Unify resource strings used for Browser PNSE (#39637)
We sometimes used `... is not supported on Browser.` and elsewhere `... is not supported on this platform.`

Standardize on the latter one as it can potentially be reused for other targets.
2020-07-20 21:22:55 +02:00
Jeff Handley 329f0dbfa3 Mark Obsoletions for .NET 5 (#39269)
* Mark new obsoletions for .NET 5; update existing obsoletions

* Exclude DirectoryServicesPermission/DirectoryServicesPermissionAttribute from obsoletions for now

* No need to restore SYSLIB0003 at end of Forwards files

* Use a common Obsoletions.cs file for all SYSLIB obsoletions

* Resurrect CER code behind #ifdef in EventSource

* Mark GlobalAssemblyCache overrides as Obsolete

* Suppress Thread.Abort() error in ImageAnimator.Unix.cs

* Unconditionally mark RoAssembly.GlobalAssemblyCache as obsolete

* Rename MSLIB0001 to SYSLIB0001 in ref assembly

* Suppress obsoletion warnings in tests

* Adopt a uniform #ifdef for NET50_OBSOLETIONS

* Fix BinaryFormatter obsoletion message after the rebase

* Use SYSTEM_PRIVATE_CORELIB constant instead of NETCOREAPP

* Remove the code comment on the ifdef

* Apply feedback related to CER obsoletions

* Apply feedback related to CAS

* Update ref assembly to reflect SecurityManager obsoletion

* Fix regression in System.IO.Pipes.NamedPipeServerStream.Windows.RunAsClient
2020-07-17 17:09:29 -07:00
Stephen Toub 6072e4d3a7 Update license headers (#38793) 2020-07-08 09:23:11 -04:00
Maxim Lipnin 522d73e64c Add throwing PNSE to System.Security.Cryptography on WASM (#38379)
Since the entire set of System.Security.Cryptography.* tests has been disabled on WASM recently in https://github.com/dotnet/runtime/pull/37723, we need to let everyone know that those areas are not quite supported on WASM at the moment. In that case System.Security.Cryptography classes could throw PlatformNotSupportedException until some solution comes (https://github.com/dotnet/runtime/issues/37669)
2020-06-29 20:55:43 +02:00
Stephen Toub ed78fadab6 Fix CA1805 for unnecessary initialization of fields (#38410)
This has three primary benefits:
1. In some cases, in particular for static fields where other static fields have non-default initialization, the C# compiler can't elide the default initialization, resulting in more IL.
2. It highlights places where the field is otherwise unused and can be deleted, but where the presence of the initialization was silencing the C# compiler's warnings about the unused field.
3. It increases consistency and decreases maintenance / code size / etc.
2020-06-26 07:52:35 -04:00
9ccfc40ec9 [libraries] Properly skip thread failing WASM tests (#38089)
Co-authored-by: Mitchell Hwang <[email protected]>
Co-authored-by: Alexander Köplinger <[email protected]>
2020-06-20 01:49:29 +02:00
Mitchell HwangandMitchell Hwang 480c49b241 [libraries] Add ConditionalFact to wasm failing tests (#37822)
* [libraries] Add IsThreadingSupported

* [libraries] Add IsThreadingSupported attribute to failing wasm tests

* [libraries] Add IsObdcNotAvailableAndThreadingSupported property to DependencyCheckTest

* [libraries] Include system namespace in RedactedLogValueIntegrationTest and fix DependencyCheckTest ConditionalFact

* [libraries] Revert System.Console IsThreadingSupported changes

* [libraries] Remove IsThreadingSupported attribute from EncodingCodePages

Co-authored-by: Mitchell Hwang <[email protected]>
2020-06-17 13:48:00 -04:00
Steve Pfister 8484d6c181 Disable System.Security.Cryptography.* tests for wasm. (#37723)
Since we aren't shipping openssl and browser crypto interop requires sync over async, we need to disable the tests until a viable solution is found.
2020-06-11 07:58:55 -04:00
Ivan Diaz Sanchez 144e514545 Added CreateDelegate<T> overloads to MethodInfo (#36680)
* Added generic overloads for MethodInfo.CreateDelegate<>()

* Added full support of CreateDelegate<T> and the necessary tests.

* Refactored to apply the new CreateDelegate overloads as necessary throughout runtime.

* Reverted Linq.Expressions changes and fully qualified Delegate to System.Delegate
2020-05-21 22:43:08 -07:00
Marcus Turewicz 890fab0a64 Use attribute Link syntax for Common files (#35436)
* Change <Link>...</Link> to Link="..."

* Format existing Link="..."
2020-04-25 05:26:50 -07:00
Kevin Jones 51a443a5fe Add API docs for HashAlgorithmName.{Try}FromOid. 2020-04-21 09:07:09 -07:00
Kevin Jones 29df078d1b Enable importing PEM-formatted keys into AsymmetricAlgorithm values 2020-04-09 09:23:17 -07:00