-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change jws tool #6
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1c8436b
Exploring changes to JWSTool
glatzert fcac0ce
Reordering Parameters
glatzert 96f99df
- Rename the "Tools" to JWSSigner
glatzert a69fa7e
Renames and consolidation
glatzert 2f7135c
Reacting to renames
glatzert 51ad3e5
Added Signature function for Strings
glatzert fb76361
Fix naming
glatzert 61b6ab2
Rename methods
glatzert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
|
||
namespace ACMESharp.Crypto.JOSE | ||
{ | ||
public class JwsAlgorithm | ||
{ | ||
private IJwsSigner _jwsTool; | ||
|
||
public JwsAlgorithm(string jwsAlgorithm) | ||
{ | ||
if (!int.TryParse(jwsAlgorithm.Substring(2), out int size)) | ||
throw new ArgumentException("Could not parse Key or Hash size", nameof(jwsAlgorithm)); | ||
|
||
if (jwsAlgorithm.StartsWith("ES")) | ||
{ | ||
var tool = new Impl.ESJwsSigner(size); | ||
|
||
_jwsTool = tool; | ||
} | ||
|
||
if (jwsAlgorithm.StartsWith("RS")) | ||
{ | ||
var tool = new Impl.RSJwsSigner(size); | ||
|
||
_jwsTool = tool; | ||
} | ||
|
||
if (_jwsTool == null) | ||
throw new InvalidOperationException("Unknown JwsAlgorithm"); | ||
} | ||
|
||
public JwsAlgorithm(JwsAlgorithmExport exported) | ||
:this(exported.Algorithm) | ||
{ | ||
_jwsTool.Import(exported.Export); | ||
} | ||
|
||
public string JwsAlg => _jwsTool.JwsAlg; | ||
|
||
public void Dispose() | ||
{ | ||
_jwsTool.Dispose(); | ||
} | ||
|
||
public JwsAlgorithmExport Export() | ||
{ | ||
var export = new JwsAlgorithmExport | ||
{ | ||
Algorithm = _jwsTool.JwsAlg, | ||
Export = _jwsTool.ExportAlgorithm() | ||
}; | ||
|
||
return export; | ||
} | ||
|
||
public object ExportPublicJwk() | ||
{ | ||
return _jwsTool.ExportPublicJwk(); | ||
} | ||
|
||
public byte[] Sign(string raw) | ||
{ | ||
// For Base64 Strings, UTF8 and ASCII will yield the same results, so we can safely use UTF8 | ||
return Sign(System.Text.Encoding.UTF8.GetBytes(raw)); | ||
} | ||
|
||
public byte[] Sign(byte[] raw) | ||
{ | ||
return _jwsTool.Sign(raw); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace ACMESharp.Crypto.JOSE | ||
{ | ||
|
||
public class JwsAlgorithmExport | ||
{ | ||
public string Algorithm { get; set; } | ||
|
||
public string Export { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if should derive the
Algorithm
value from the original constructor argument instead of what the IJwsSigner is giving you. In this case theJwsAlg
value is what is needed for a JWS payload to understand the contents and associated key type, but it may not exactly translate to the specific IJwsSigner implementation upon reload.And along those same lines, the
JwsAlgorithm(string)
constructor would actually be taking an implementation identifier of sorts, not actually a JWS algorithm ID.For example, in the current core code base, there are the 2 default JWS signer implementations, RSA and ECDSA, but I recently had to add a third one, an alternative ECDSA version because the 2 default implementations would not work in my target environment because they relied on the BCL crypto support which was missing, and I needed to add an alternative variation that was based on BouncyCastle. Even though it's a distinct implementation of it's own, it still implements the ES256 (and ES384 and ES521) algorithms which is what it returns in its
JwsAlg
property.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right - I reduced the extend-ability here, which should be there to swap out the actual JWS implementation. I was so fixated on making reuse of the key more "graspable" and straight forward, that I ignored that.
I have an idea, where you would get the possibility to define which JwsImplementation reacts on which name - something like a Dict<string, Func<string, IJwsSigner>> or something along those lines. Also instead of using the JwsAlg-Names i could use a more distinguished list like DefaultRSA374 or something like that.