Skip to content

Don't rely on StrongNameKeyPair#548

Merged
jbevain merged 6 commits into
jbevain:masterfrom
atykhyy:issue/548
Aug 5, 2019
Merged

Don't rely on StrongNameKeyPair#548
jbevain merged 6 commits into
jbevain:masterfrom
atykhyy:issue/548

Conversation

@atykhyy

@atykhyy atykhyy commented Nov 5, 2018

Copy link
Copy Markdown
Contributor

Improving on netstandard1.3, netstandard2.0 provides the type System.Reflection.StrongNameKeyPair, but on .NET Core it is merely a stub that throws PlatformNotSupportedException from all methods and properties. This pull request extends WriterParameters with two new properties for strong name key blob and strong name key container, using them in preference to StrongNameKeyPair when set, ports a couple of crypto routines from Mono, and marks WriterParameters.StrongNameKeyPair as obsolete as it is not usable on .NET Core. The code in this PR has been in use on .NET Core 2.0.

@SimonCropp

Copy link
Copy Markdown
Contributor

nice :)

Comment thread Mono.Cecil/AssemblyWriter.cs
@SimonCropp

Copy link
Copy Markdown
Contributor

@jbevain thoughts on this one? want me to rebase it?

@jbevain

jbevain commented Mar 18, 2019

Copy link
Copy Markdown
Owner

@SimonCropp yeah I like it, I think it should go in 0.11.

@pcshrosbree

Copy link
Copy Markdown

Is any work happening on this? Or is another approach recommended?

@ADD-Juan-Perez

Copy link
Copy Markdown

@jbevain When do you plan to publish '0.11' version with this change?

@jbevain
jbevain merged commit 12da1bf into jbevain:master Aug 5, 2019
@clairernovotny

Copy link
Copy Markdown

Are there any docs on how to use this? I'm trying to stumble my way through this. Even sample code that shows its use...

@atykhyy

atykhyy commented Nov 3, 2019

Copy link
Copy Markdown
Contributor Author

When you want to write a strong-named module in a portable manner, put either the content of the .snk file as byte[] into writerParameters.StrongNameKeyBlob or the name of the key container into writerParameters.StrongNameKeyContainer and pass those writerParameters to the module/assembly writer method. That's all there is to it.

@clairernovotny

Copy link
Copy Markdown

Do we need to also still set assembly.Name.HasPublicKey and assembly.Name.PublicKey?

Also would be good to update the wiki: https://github.com/jbevain/cecil/wiki/StrongName

assembly.Name.PublicKey is a byte[], but I couldn't find any external way to get that public key from a snk. I had to copy/paste code from this PR to do it. Is there a public method to get the public key from an snk that is available (or could be exposed)? I had to copy the GetPublicKey from // https://github.com/atykhyy/cecil/blob/291a779d473e9c88e597e2c9f86e47e23b49be1e/Mono.Security.Cryptography/CryptoService.cs and CryptoConvert to get the RSA from the snk.

@clairernovotny

Copy link
Copy Markdown

FWIW, this came up in my PR here for StrongNamer while adding support for .NET Core: dsplaisted/strongnamer#41. You can see the files I had to add/code I had to copy.

@jbevain

jbevain commented Nov 3, 2019

Copy link
Copy Markdown
Owner

@onovotny the PublicKey will be computed from the pair if you pass a set of pair to the parameters:

name.PublicKey = CryptoService.GetPublicKey (parameters);

@clairernovotny

Copy link
Copy Markdown

@jbevain thanks. Is there an API to get the public key from an snk if I need it for something else? In the PR, I need to get the full public key in .NET Core so that it can rewrite the InternalsVisibleTo attribute. If there's an API for that, I wouldn't have to duplicate any of the code from here.

@jbevain

jbevain commented Nov 3, 2019

Copy link
Copy Markdown
Owner

@onovotny not that I know of. You should be able to extract what you need from CryptoService.cs and CryptoConvert.cs without too much troubles.

@SimonCropp

Copy link
Copy Markdown
Contributor

i have this is the fody fork of cecil Fody@e541e2b

@clairernovotny

Copy link
Copy Markdown

@SimonCropp it would be useful to have that directly in Cecil. @jbevain I do have the code copied, but given that .NET Core has no other way to deal with this data structure, it speaks to a need for it elsewhere.

@SimonCropp

Copy link
Copy Markdown
Contributor

it would be useful to have that directly in Cecil

shoudnt that code exist in netcore and not cecil?

@clairernovotny

Copy link
Copy Markdown

Probably, but Microsoft doesn't seem to care about supporting anything to do with Strong Names in .NET Core. It's explicitly all PNSE's. Any support will have to come externally.

@SimonCropp

Copy link
Copy Markdown
Contributor

agreed. happy for someone to take the code and merge into cecil, if @jbevain is ok with that. i wont have the capacity in the short term

@Zastai

Zastai commented Nov 25, 2020

Copy link
Copy Markdown
Contributor

Also would be good to update the wiki: https://github.com/jbevain/cecil/wiki/StrongName

I second this - I came here to report the StrongNameKeyPair approach being broken, because nothing pointed me at the new properties.

@Zastai

Zastai commented Nov 25, 2020

Copy link
Copy Markdown
Contributor

Also note that using any non-null value for StrongNameKeyContainer currently results in a strong named assembly; if there is no matching container, a random (or default; not entirely sure now) key pair gets used. So you get no error, but a potentially unusable assembly (due to wrong public key).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants