-
-
Notifications
You must be signed in to change notification settings - Fork 119
adds support for brainpoolP256r1 #646
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
Open
robertboeser
wants to merge
2
commits into
web-token:4.2.x
Choose a base branch
from
robertboeser:brainpool
base: 4.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ infection.txt | |
| .castor.stub.php | ||
| /tmp* | ||
| /.ci-tools/coverage | ||
| /var/cache | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,10 +7,12 @@ | |
| use InvalidArgumentException; | ||
| use Jose\Component\Core\JWK; | ||
| use RuntimeException; | ||
|
|
||
| use function extension_loaded; | ||
| use function is_array; | ||
| use function is_string; | ||
| use function sprintf; | ||
|
|
||
| use const OPENSSL_KEYTYPE_EC; | ||
| use const STR_PAD_LEFT; | ||
|
|
||
|
|
@@ -32,6 +34,7 @@ public static function convertPublicKeyToPEM(JWK $jwk): string | |
| { | ||
| $der = match ($jwk->get('crv')) { | ||
| 'P-256' => self::p256PublicKey(), | ||
| 'BP-256' => self::p256PublicKey(), | ||
| 'secp256k1' => self::p256KPublicKey(), | ||
| 'P-384' => self::p384PublicKey(), | ||
| 'P-521' => self::p521PublicKey(), | ||
|
|
@@ -48,6 +51,7 @@ public static function convertPrivateKeyToPEM(JWK $jwk): string | |
| { | ||
| $der = match ($jwk->get('crv')) { | ||
| 'P-256' => self::p256PrivateKey($jwk), | ||
| 'BP-256' => self::p256PrivateKey($jwk), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. The OID is different for BP private keys
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be as follows: private static function bp256PrivateKey(JWK $jwk): string
{
$d = $jwk->get('d');
if (!is_string($d)) {
throw new InvalidArgumentException('Unable to get the private key');
}
$d = unpack('H*', str_pad(Base64UrlSafe::decodeNoPadding($d), 32, "\0", STR_PAD_LEFT));
if (!is_array($d) || !isset($d[1])) {
throw new InvalidArgumentException('Unable to get the private key');
}
return pack(
'H*',
'3078' // SEQUENCE, length 120
. '020101' // INTEGER, 1
. '0420' // OCTET STRING, length 32
. $d[1]
. 'a00b' // TAGGED OBJECT #0, length 11
. '0609' // OID, length 9
. '2b2403030208010107' // 1.3.36.3.3.2.8.1.1.7
. 'a144' // TAGGED OBJECT #1, length 68
. '0342' // BIT STRING, length 66
. '00'
);
} |
||
| 'secp256k1' => self::p256KPrivateKey($jwk), | ||
| 'P-384' => self::p384PrivateKey($jwk), | ||
| 'P-521' => self::p521PrivateKey($jwk), | ||
|
|
@@ -77,7 +81,7 @@ public static function createECKey(string $curve, array $values = []): JWK | |
| private static function getNistCurveSize(string $curve): int | ||
| { | ||
| return match ($curve) { | ||
| 'P-256', 'secp256k1' => 256, | ||
| 'P-256', 'BP-256', 'secp256k1' => 256, | ||
| 'P-384' => 384, | ||
| 'P-521' => 521, | ||
| default => throw new InvalidArgumentException(sprintf('The curve "%s" is not supported.', $curve)), | ||
|
|
@@ -130,6 +134,7 @@ private static function getOpensslCurveName(string $curve): string | |
| { | ||
| return match ($curve) { | ||
| 'P-256' => 'prime256v1', | ||
| 'BP-256' => 'brainpoolP256r1', | ||
| 'secp256k1' => 'secp256k1', | ||
| 'P-384' => 'secp384r1', | ||
| 'P-521' => 'secp521r1', | ||
|
|
||
This file contains hidden or 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,48 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Jose\Component\Core\Util\Ecc; | ||
|
|
||
| use Brick\Math\BigInteger; | ||
|
|
||
| /** | ||
| * Copyright (C) 2024 Robert Böser. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
| * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
| * permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
| * Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
| * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
| * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class BrainpoolCurve | ||
| { | ||
| /** | ||
| * Returns an Brainpool BP-256 curve. | ||
| * RFC 5639 - brainpoolP256r1 | ||
| */ | ||
| public static function curve256(): Curve | ||
| { | ||
| $p = BigInteger::fromBase('A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377', 16); | ||
| $a = BigInteger::fromBase('7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9', 16); | ||
| $b = BigInteger::fromBase('26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6', 16); | ||
| $x = BigInteger::fromBase('8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262', 16); | ||
| $y = BigInteger::fromBase('547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997', 16); | ||
| $n = BigInteger::fromBase('A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7', 16); | ||
| $generator = Point::create($x, $y, $n); | ||
|
|
||
| return new Curve(256, $p, $a, $b, $generator); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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.
This is not correct as the OID is different for BP public keys
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.
Should be as follows: