-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIST Elliptic Curve JWKs: field element byte array padding (#903)
* Ensured NIST Elliptic Curve JWKs pre-pad their X, Y and D byte arrays with zero bytes before Base64URL-encoding if necessary per length requirements defined in: - https://datatracker.ietf.org/doc/html/rfc7518#section-6.2.1.2 - https://datatracker.ietf.org/doc/html/rfc7518#section-6.2.1.3 - https://datatracker.ietf.org/doc/html/rfc7518#section-6.2.2.1 Fixes #901.
- Loading branch information
1 parent
3e8f8a8
commit 0763191
Showing
12 changed files
with
209 additions
and
28 deletions.
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
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
69 changes: 69 additions & 0 deletions
69
impl/src/main/java/io/jsonwebtoken/impl/security/FieldElementConverter.java
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,69 @@ | ||
/* | ||
* Copyright © 2024 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.impl.security; | ||
|
||
import io.jsonwebtoken.impl.io.Codec; | ||
import io.jsonwebtoken.impl.lang.Bytes; | ||
import io.jsonwebtoken.impl.lang.Converter; | ||
import io.jsonwebtoken.impl.lang.Converters; | ||
|
||
import java.math.BigInteger; | ||
|
||
/** | ||
* Hotfix for <a href="https://github.com/jwtk/jjwt/issues/901">JJWT Issue 901</a>. This is currently hard-coded | ||
* expecting field elements for NIST P-256, P-384, or P-521 curves. Ideally this should be refactored to work for | ||
* <em>any</em> curve based on its field size, not just for these NIST curves. However, the | ||
* {@link EcPublicJwkFactory} and {@link EcPrivateJwkFactory} implementations only work with JWA NIST curves, | ||
* so this implementation is acceptable until (and if) different Weierstrass elliptic curves (ever) need to be | ||
* supported. | ||
* | ||
* @since 0.12.4 | ||
*/ | ||
final class FieldElementConverter implements Converter<BigInteger, byte[]> { | ||
|
||
static final FieldElementConverter INSTANCE = new FieldElementConverter(); | ||
|
||
static final Converter<BigInteger, Object> B64URL_CONVERTER = Converters.forEncoded(BigInteger.class, | ||
Converters.compound(INSTANCE, Codec.BASE64URL)); | ||
|
||
private static int bytelen(ECCurve curve) { | ||
return Bytes.length(curve.toParameterSpec().getCurve().getField().getFieldSize()); | ||
} | ||
|
||
private static final int P256_BYTE_LEN = bytelen(ECCurve.P256); | ||
private static final int P384_BYTE_LEN = bytelen(ECCurve.P384); | ||
private static final int P521_BYTE_LEN = bytelen(ECCurve.P521); | ||
|
||
@Override | ||
public byte[] applyTo(BigInteger bigInteger) { | ||
byte[] bytes = Converters.BIGINT_UBYTES.applyTo(bigInteger); | ||
int len = bytes.length; | ||
if (len == P256_BYTE_LEN || len == P384_BYTE_LEN || len == P521_BYTE_LEN) return bytes; | ||
if (len < P256_BYTE_LEN) { | ||
bytes = Bytes.prepad(bytes, P256_BYTE_LEN); | ||
} else if (len < P384_BYTE_LEN) { | ||
bytes = Bytes.prepad(bytes, P384_BYTE_LEN); | ||
} else { // > P-384, so must be P-521: | ||
bytes = Bytes.prepad(bytes, P521_BYTE_LEN); | ||
} | ||
return bytes; | ||
} | ||
|
||
@Override | ||
public BigInteger applyFrom(byte[] bytes) { | ||
return Converters.BIGINT_UBYTES.applyFrom(bytes); | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
impl/src/test/groovy/io/jsonwebtoken/impl/security/FieldElementConverterTest.groovy
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,41 @@ | ||
/* | ||
* Copyright © 2024 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.impl.security | ||
|
||
import io.jsonwebtoken.impl.lang.Bytes | ||
import org.junit.Test | ||
|
||
import static org.junit.Assert.assertEquals | ||
|
||
/** | ||
* @since 0.12.4 | ||
*/ | ||
class FieldElementConverterTest { | ||
|
||
static FieldElementConverter converter = FieldElementConverter.INSTANCE | ||
|
||
@Test | ||
void p384CoordinateNeedsPadding() { | ||
def requiredByteLen = 48 | ||
def coordBytes = Bytes.random(requiredByteLen - 1) // one less to see if padding is applied | ||
def coord = new BigInteger(1, coordBytes) | ||
byte[] result = converter.applyTo(coord) | ||
assertEquals requiredByteLen, result.length | ||
assertEquals 0x00 as byte, result[0] | ||
//ensure roundtrip works: | ||
assertEquals coord, converter.applyFrom(result) | ||
} | ||
} |
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