You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Merge all of the inbox S.S.C.* libraries into one #55690
Currently, the PemEncoding and PemFields types live in System.Security.Cryptography.Encoding. This made sense at the time. However, it makes it difficult to use the PEM functionality from things in System.Security.Cryptography.Primitives, specifically AsymmetricAlgorithm, because that would be a circular dependency.
This has already impacted our ability to do something we wanted to at #34086 (comment).
With #51630, we’re going to run in to this again, with a stronger argument that the PEM-PKCS8 exports should be done on AsymmetricAlgorithm directly. This leaves us with two options:
Move the types we need in to the Primitives assembly.
Use reflection to call the PEM APIs. This is doable, but we will need to call the allocating APIs since working with Span<T> with reflection is not doable today.
but we will need to call the allocating APIs since working with Span with reflection is not doable today
I think that it is, it's just more complicated 😄.
privatedelegateintTryWriteDelegate(ReadOnlySpan<char>label,ReadOnlySpan<byte>data,Span<char>destination,outintcharsWritten);...Type t =...;MethodInfomethod=t.GetMethod("TryWrite",BindingFlags.Static|BindingFlags.Public);TryWriteDelegatetwd=(TryWriteDelegate)method.CreateDelegate(typeof(TryWriteDelegate));// squirrel that off into a field somewhere.
There've been a few things that make me feel like we might want to just merge everything except Pkcs and Xml into System.Private.Security.Cryptography.dll, but maybe it doesn't need to be quite that drastic, and this one type is all we should move.
There've been a few things that make me feel like we might want to just merge everything except Pkcs and Xml into System.Private.Security.Cryptography.dll
If that's on the table I'd very much prefer that. I can't recall the specifics but on multiple occasions I wished it were a big assembly. I'm sure if I went digging through some of my abandoned PRs I could jog my memory. 😄
with a stronger argument that the PKCS8 exports should be done on SymmetricAlgorithm directly
On Symmetric? PKCS#8 doesn't make sense there.
On Asymmetric? We define the methods there, but since each algorithm has unique ways for doing the algorithm identifier (IIRC that's where DSA shoves the domain parameters and EC shoves the curve declaration) and the payload, there's not a lot that the base class can really do without there being two protected virtuals... so we let the algorithms do it with the one.
Yeah typed the wrong thing there. It seems some part of my brain is still working on one shots.
On Asymmetric?
For export, the only thing on AsymmetricAlgorithm were PKCS8 / SPKI PEM exports. Since those are just PUBLIC / PRIVATE key (where PRIVATE could be ENCRYPTED PRIVATE) it doesn't make sense to override them on their subclasses - we didn't even define them as virtual on the proposal.
Yeah, I could have been more clear what I was talking about 😄
Maybe a place to start then is to make a new System.Security.Cryptography project and begin with moving .Encoding and .Primitives in there. We can move the remaining later, if it makes sense to move them. .Xml and .Pkcs should probably stay out, and probably .ProtectedData?
There are some "reflection-y" things that happen with CryptoConfig and various formatters in S.S.C.Xml. Do type forwards work with assembly qualified type names, like System.Security.Cryptography.RSA, System.Security.Cryptography.Algorithms?
Keep in mind TypeForwardedFrom for any serializable types (hopefully not an issue). You will of course need to keep the old assemblies as facades for compatibility but those can be a one-time cost and effectively ignored after that.
You still may want to enforce some layering in your own codebase but that can be done through convention and code review.
You may end up with PNSEs for surface area that’s smaller than the assembly. Today you benefit from the PNSE generator but you’ll need to either check that in or we may need a better partial PNSE solution.
Are there any resource on the benefits of [using a System.Private.*]?
I chatted with Eric via a different medium, and concluded that System.Private.* isn't the route we want to take.
The System.Private.* libraries started when we had the small contract assemblies in .NET Core 1.0. Whenever we felt like it was important (or maybe just expedient) to have multiple contracts backed by one implementation assembly a System.Private was born. The feeling back then, IIRC, was "if you can avoid it, please avoid it".
So the System.Private.* assemblies seem to, largely, represent "we wish we could sensibly split these apart, and want to make less work on ourselves when we do so". Since we're instead saying "we don't think it makes sense to keep these split apart", we don't need the "Private" artifact.
(And if I misunderstood, hopefully someone corrects me)
changed the title [-]Consider moving PemEncoding and PemFields to S.S.Cryptography.Primitives[/-][+]Merge all of the inbox S.S.C.* libraries into one[/+]on Nov 1, 2021
Note that sometimes Apple is split into macOS and iOS (and in one case tvOS), but not in these examples.
In addition to getting rid of "Internal\Cryptography" as part of this move, it feels like we should try to come up with a better path/name scheme.
I think my strawman is going to be
Namespace\As\Path: AnyOS stuff that isn't involved in platform splits. Additionally, the common portion of any type that is public and is involved in a platform split.
Namespace\As\Path\Pal: ("Pal" and subdirectories not contributing to the namespace): Auxiliary types included by every platform that just set up the concept of PAL-ling.
Namespace\As\Path\Pal\Managed: Any files, like ManagedCertificateFinder.cs, that are contributing to a "non-platform" implementation. Probably included across multiple non-Windows systems.
Namespace\As\Path\Pal\Windows: Windows versions of files. e.g. OidLookup.Windows.cs goes here as just OidLookup.cs.
Namespace\As\Path\Pal\OpenSsl: Non-"interop" files that are involved in interop logic to OpenSSL via libSystem.Security.Cryptography.Native.so
Namespace\As\Path\Pal\Apple: "Apple common non-interop"
Namespace\As\Path\Pal\macOS: When macOS and iOS disagree, the macOS version.
Namespace\As\Path\Pal\iOS: When macOS and iOS disagree, the iOS version.
Namespace\As\Path\Pal\Android: Android
iOS/macOS could also easily be subdirectories under Apple, or extension variants (e.g. System\Security\Cryptography\Pal\Apple\RSASecurityTransforms.macOS.cs)
The idea behind this strawman being that we name the directories less about where they run "Unix" and more about what crypto provider they're working with "Windows", "OpenSSL", "Apple" (Security.framework), "Android" (Android JDK/JNI), and "Managed" (not PAL'd).
I really struggle with files with the same name in different directories. It makes code navigation harder (Cmd+P) and it when looking at a tab row of many open files, they all have the same name so I just end up lost. I greatly prefer file-based suffixes instead of directories that contain a handful of files with lots of overlapping names. If that is not "the way", so be it, but it's one of the things that has bugged me quite a bit.
Once the files have unique names, I guess I should question whether there's value in PAL directories at all. The problem I have with them a bit right now is that "Android" can be in both "Android" and "Unix", but Linux is only in "Unix", though not necessarily all of Unix (since that could also be something shared by Apple and Android); so if they're present it feels like they should be about the provider, but I'm open to no-directory, too.
No extra directories would then end up with something like
@vcsjones: Since Encodings just got folded in to the new library, AsymmetricAlgorithm and PemEncoding are in the same library now. Since that was the catalyst for the squishification, thought I'd point that out explicitly.
I really struggle with files with the same name in different directories.
+1. This is a key reason we went with *.Suffix.cs.
Can we get rid of the "\Pal" directory as well? Doesn't seem to be adding much benefit. Ideally we end up with all the partials right next to each other, e.g.
OidLookup.cs
OidLookup.Windows.cs
OidLookup.OpenSsl.cs
etc.
Can we get rid of the "\Pal" directory as well? Doesn't seem to be adding much benefit. Ideally we end up with all the partials right next to each other, e.g.
Oh, good, that's the approach I'm currently pursuing with squishing Algorithms down into the unified library 😄.
Activity
bartonjs commentedon Jul 15, 2021
I think that it is, it's just more complicated 😄.
There've been a few things that make me feel like we might want to just merge everything except Pkcs and Xml into System.Private.Security.Cryptography.dll, but maybe it doesn't need to be quite that drastic, and this one type is all we should move.
vcsjones commentedon Jul 15, 2021
If that's on the table I'd very much prefer that. I can't recall the specifics but on multiple occasions I wished it were a big assembly. I'm sure if I went digging through some of my abandoned PRs I could jog my memory. 😄
bartonjs commentedon Jul 15, 2021
On Symmetric? PKCS#8 doesn't make sense there.
On Asymmetric? We define the methods there, but since each algorithm has unique ways for doing the algorithm identifier (IIRC that's where DSA shoves the domain parameters and EC shoves the curve declaration) and the payload, there's not a lot that the base class can really do without there being two protected virtuals... so we let the algorithms do it with the one.
vcsjones commentedon Jul 15, 2021
Yeah typed the wrong thing there. It seems some part of my brain is still working on one shots.
For export, the only thing on
AsymmetricAlgorithmwere PKCS8 / SPKI PEM exports. Since those are just PUBLIC / PRIVATE key (where PRIVATE could be ENCRYPTED PRIVATE) it doesn't make sense to override them on their subclasses - we didn't even define them as virtual on the proposal.bartonjs commentedon Jul 15, 2021
Ooooh, not the real PKCS8, but the PEM-PKCS8.
Yeah, since that's just "call the existing PKCS8 virtual, PEM-ify the result".
vcsjones commentedon Jul 15, 2021
Yeah, I could have been more clear what I was talking about 😄
Maybe a place to start then is to make a new System.Security.Cryptography project and begin with moving .Encoding and .Primitives in there. We can move the remaining later, if it makes sense to move them. .Xml and .Pkcs should probably stay out, and probably .ProtectedData?
bartonjs commentedon Jul 15, 2021
@ericstj Anything special that should be kept in mind while considering collapsing the non-NuGet crypto libraries?
I feel like there are probably two approaches:
I don't think, off the top of my head, we need the layering for any co-dependency reasons. It was mostly a Core 1.0 artifact.
In extremis:
I believe that all 7 are included in the shared framework on all OSes.
vcsjones commentedon Jul 15, 2021
There are some "reflection-y" things that happen with
CryptoConfigand various formatters in S.S.C.Xml. Do type forwards work with assembly qualified type names, likeSystem.Security.Cryptography.RSA, System.Security.Cryptography.Algorithms?bartonjs commentedon Jul 15, 2021
Yep, it works.
vcsjones commentedon Jul 15, 2021
Ah, thanks.
Are there any resource on the benefits of the first approach? I know that's done in some places like CoreLib, Uri, and Xml, but I don't quite see why.
vcsjones commentedon Jul 17, 2021
Another issue we may be able to close if we merge these assemblies is #29555.
ericstj commentedon Jul 21, 2021
Keep in mind TypeForwardedFrom for any serializable types (hopefully not an issue). You will of course need to keep the old assemblies as facades for compatibility but those can be a one-time cost and effectively ignored after that.
You still may want to enforce some layering in your own codebase but that can be done through convention and code review.
You may end up with PNSEs for surface area that’s smaller than the assembly. Today you benefit from the PNSE generator but you’ll need to either check that in or we may need a better partial PNSE solution.
bartonjs commentedon Jul 21, 2021
I chatted with Eric via a different medium, and concluded that System.Private.* isn't the route we want to take.
The System.Private.* libraries started when we had the small contract assemblies in .NET Core 1.0. Whenever we felt like it was important (or maybe just expedient) to have multiple contracts backed by one implementation assembly a System.Private was born. The feeling back then, IIRC, was "if you can avoid it, please avoid it".
So the System.Private.* assemblies seem to, largely, represent "we wish we could sensibly split these apart, and want to make less work on ourselves when we do so". Since we're instead saying "we don't think it makes sense to keep these split apart", we don't need the "Private" artifact.
(And if I misunderstood, hopefully someone corrects me)
[-]Consider moving PemEncoding and PemFields to S.S.Cryptography.Primitives[/-][+]Merge all of the inbox S.S.C.* libraries into one[/+]bartonjs commentedon Nov 1, 2021
With the stage 1 PR I got to avoid platform-differentiated code. Stage 2 is being less nice.
We've evolved ourselves into having a couple of different "standards" in file pathing for how we have platform splits. For example:
Note that sometimes Apple is split into macOS and iOS (and in one case tvOS), but not in these examples.
In addition to getting rid of "Internal\Cryptography" as part of this move, it feels like we should try to come up with a better path/name scheme.
I think my strawman is going to be
iOS/macOS could also easily be subdirectories under Apple, or extension variants (e.g. System\Security\Cryptography\Pal\Apple\RSASecurityTransforms.macOS.cs)
The idea behind this strawman being that we name the directories less about where they run "Unix" and more about what crypto provider they're working with "Windows", "OpenSSL", "Apple" (Security.framework), "Android" (Android JDK/JNI), and "Managed" (not PAL'd).
So for the earlier example files:
Not sure I'm happy with that, though, so definitely open to feedback.
@stephentoub I'd be happy to get your insight/opinion here.
vcsjones commentedon Nov 1, 2021
"open mic" style feedback...
Having a managed "PAL" seems odd.
I really struggle with files with the same name in different directories. It makes code navigation harder (Cmd+P) and it when looking at a tab row of many open files, they all have the same name so I just end up lost. I greatly prefer file-based suffixes instead of directories that contain a handful of files with lots of overlapping names. If that is not "the way", so be it, but it's one of the things that has bugged me quite a bit.
bartonjs commentedon Nov 1, 2021
That’s a good point. It has also frustrated me, but somehow that didn’t occur to me when I was strawmanning.
bartonjs commentedon Nov 1, 2021
Once the files have unique names, I guess I should question whether there's value in PAL directories at all. The problem I have with them a bit right now is that "Android" can be in both "Android" and "Unix", but Linux is only in "Unix", though not necessarily all of Unix (since that could also be something shared by Apple and Android); so if they're present it feels like they should be about the provider, but I'm open to no-directory, too.
No extra directories would then end up with something like
bartonjs commentedon Nov 4, 2021
@vcsjones: Since Encodings just got folded in to the new library, AsymmetricAlgorithm and PemEncoding are in the same library now. Since that was the catalyst for the squishification, thought I'd point that out explicitly.
stephentoub commentedon Nov 9, 2021
+1. This is a key reason we went with *.Suffix.cs.
Can we get rid of the "\Pal" directory as well? Doesn't seem to be adding much benefit. Ideally we end up with all the partials right next to each other, e.g.
OidLookup.cs
OidLookup.Windows.cs
OidLookup.OpenSsl.cs
etc.
bartonjs commentedon Nov 9, 2021
Oh, good, that's the approach I'm currently pursuing with squishing Algorithms down into the unified library 😄.
bartonjs commentedon Jan 31, 2022
Closed via #64307.