Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add missing members to EncryptedXml. #23417

Merged
merged 1 commit into from
Sep 7, 2017
Merged

Add missing members to EncryptedXml. #23417

merged 1 commit into from
Sep 7, 2017

Conversation

vladimir-kazakov
Copy link
Contributor

While importing EncryptedXml into Mono (mono/mono#5273), it was noticed that its following members are missing:

This PR adds them. The implementation is taken from .NET Framework.

The behavior of the EncryptedXmlTest.DecryptData_CipherReference_InvalidUri test was changed. As far as I understand, both .NET Core and .NET Framework must behave the same way. According to the documentation, the SecurityManager.GetStandardSandbox must throw if the evidence is null. Previously, .NET Core wasn't throwing in this case, so this was changed as well.

[Fact]
public void GetStandardSandbox_WhenEvidenceIsNull_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => SecurityManager.GetStandardSandbox(null));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use AssertExtensions to also check for the name of the argument thrown (nameof(evidence))

Assert.Throws<ArgumentNullException>(decrypt);
else
Assert.Throws<CryptographicException>(decrypt);
Assert.Throws<ArgumentNullException>(() => exml.DecryptData(ed, aes));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. Check for name argument too.

@@ -47,28 +45,47 @@ public override void GenerateKey()
}
}

private readonly Encoding _defaultEncoding = Encoding.UTF8;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static

private const CipherMode DefaultCipherMode = CipherMode.CBC;
private const PaddingMode DefaultPaddingMode = PaddingMode.ISO10126;
private const string DefaultRecipient = "";
private readonly XmlResolver _defaultXmlResolver = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static

@ViktorHofer
Copy link
Member

I hope you are aware that Evidence is stubbed out in .NET Core: https://github.com/dotnet/corefx/blob/986ffdb1053d334b862f96b53c67ca3bae222219/src/System.Security.Permissions/src/System/Security/Policy/Evidence.cs

I guess you aren't using our sources where Evidence is defined (System.Security.Permissions)?

{
// maybe a network stream, make sure we allow just what is needed!!
PermissionSet ps = SecurityManager.GetStandardSandbox(_evidence);
ps.PermitOnly();
Copy link
Member

@ViktorHofer ViktorHofer Aug 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will throw in .NET Core:

public void PermitOnly() { throw new PlatformNotSupportedException(SR.PlatformNotSupported_CAS); }

Copy link
Member

@ViktorHofer ViktorHofer Aug 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danmosemsft what's the strategy for code that checks if permission is granted? I guess it's intentional that PermitOnly wasn't stubbed out silently instead of throwing?

If this code path is reached, which means the user passed an Evidence in the constructor and therefore _evidence is not null an exception is still throw in Core.

@@ -26,6 +26,7 @@
<Compile Include="SecurityElementTests.cs" />
<Compile Include="HostSecurityManagerTests.cs" />
<Compile Include="HostProtectionTests.cs" />
<Compile Include="SecurityManagerTests.cs" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't related to your change but please sort the files as we were supposed to do before we got lazy ;)


namespace System.Security.Permissions.Tests
{
public class SecurityManagerTests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also include a positive test?

@vladimir-kazakov
Copy link
Contributor Author

I reverted all changes related to System.Security.Permissions. It seems that in order to download a cipher value from a URI, this has to be implemented in .NET Core in a different way than it was done in .NET Framework. This is more like a topic for another PR. In this one, the goal is to simply add missing members to EncryptedXml, so that it would be compatible with .NET Framework and Mono.

Speaking of Mono, currently it's not able to download cipher values from URIs as well, so not having an implementation for this case doesn't really change anything.

@ianhays
Copy link
Contributor

ianhays commented Aug 21, 2017

It looks like we originally had these but removed them in ac17228#diff-8f0821d6d517dbaeb3868dab123e4238 as a part of #14662 because EncryptedXml..ctor(XmlDocument, Evidence)` was removed as a loosely CAS-ish notion.

And @bartonjs 's response: On the one hand, I think the type exists (System.Security.Permissions). On the other hand, it's dead code in this implementation... so... I'm content. We can add it back later if we decide that we need 100% API compatibility.

So the question then would be if it's worth adding these back even though they're empty variables and whether it would help or hinder in the long run since we're not using them anywhere.

@bartonjs
Copy link
Member

@vladimir-kazakov Is your goal here to make Mono no longer have a distinct implementation and just use the package from Core?

@danmoseley
Copy link
Member

System.Diagnostics.TraceSource.Tests is AV'ing.

@vladimir-kazakov
Copy link
Contributor Author

@bartonjs, the goal is to directly use .cs files from .NET Core in Mono. It looks like this: https://github.com/mono/mono/blob/master/mcs/class/System.Security/System.Security.dll.sources#L115. In other words, the code is the same, but frameworks are different.

According to comments in issues and PRs from this repository related to System.Security.Cryptography.Xml, people were discussing whether there should be a new API or the straight port from .NET Framework. If I understood everything correctly, the "new API" idea didn't receive much support due to its complexity. The decision was made to make a 100% API-compatible port of what .NET Framework has. Changes in this PR are adding missing APIs that I found while trying to use the code from .NET Core in Mono. Although I'm talking about Mono, these changes also make .NET Core compatible with .NET Framework, because Mono must be fully compatible with .NET Framework.

So the question then would be if it's worth adding these back even though they're empty variables and whether it would help or hinder in the long run since we're not using them anywhere.

@ianhays, empty right now, but perhaps someone in the future will decide to implement missing functionality. Someone who was using it in .NET Framework and who needs it in either .NET Core or Mono. But this is the future. Currently, only missing APIs are needed (without implementation) in order to make it possible to reuse code from .NET Core in Mono. Right now, the implementation is not important, because both .NET Core and Mono don't have the implementation of missing functionality (the ability to download cipher values from URIs). In the future, though, I'd expect this functionality to be implemented, in order to be fully compatible with .NET Framework.

@danmoseley
Copy link
Member

@dotnet-bot test this please (no log left)

@bartonjs
Copy link
Member

Seems fine to me. I'm happy with getting code reuse out of the effort.

@ericstj System.Security.Cryptography.Xml is a standalone package that has a netfx facade... is there anything tricky we need to do here when changing the ref to include members that netfx has that we didn't provide in the last stable package version?

@ericstj
Copy link
Member

ericstj commented Aug 28, 2017

@ericstj System.Security.Cryptography.Xml is a standalone package that has a netfx facade... is there anything tricky we need to do here when changing the ref to include members that netfx has that we didn't provide in the last stable package version?

So long as they are all present in the supported version of desktop then, you can update the ref and keep it building for all targeted frameworks (make sure you don't redist anything). Note that desktop isn't the only thing to think about when you expose more API from one of these. Also consider Mono/Xamarin to understand if they have the API as well. If you are exposing new types it's good to let folks like @marek-safar know so that they can ensure that they update the facades.

Contrast that to adding members that didn't exist in desktop: you'd have to make those additions exclusive to NETCoreApp.

Copy link
Contributor

@ianhays ianhays left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes LGTM assuming we want the api exposed for all targetgroups.

@karelz
Copy link
Member

karelz commented Sep 6, 2017

@dotnet-bot test Windows x64 Debug Build

@karelz
Copy link
Member

karelz commented Sep 6, 2017

@bartonjs can you please help get it through while @ianhays is OOF?

@bartonjs
Copy link
Member

bartonjs commented Sep 6, 2017

@dotnet-bot Test Windows x64 Debug Build please

@bartonjs bartonjs merged commit 46579e4 into dotnet:master Sep 7, 2017
@vladimir-kazakov vladimir-kazakov deleted the encrypted-xml-missing-apis branch September 7, 2017 20:57
@joperezr
Copy link
Member

This PR added a line that broke our builds. https://github.com/dotnet/corefx/pull/23417/files#diff-e86e9429bdfda04e0301ba8bd13932ebR13

Ref projects should have P2P references as opposed to just Reference items, since we can't really ensure that all dependencies have been built yet.

@joperezr
Copy link
Member

I created #23946 to fix it.

@kingces95 kingces95 mentioned this pull request Sep 11, 2017
@karelz karelz added this to the 2.1.0 milestone Oct 11, 2017
@bartonjs bartonjs removed their assignment Jan 29, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…d-xml-missing-apis

Add missing members to EncryptedXml.

Commit migrated from dotnet/corefx@46579e4
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants