@@ -166,7 +166,8 @@ public static JWK X25519PrivateKey_to_JWK(byte[] privateKey, String kid, String
166166 public static JWK P_256PrivateKey_to_JWK (ECPrivateKey privateKey , String kid , String use ) {
167167
168168 byte [] d = ByteArrayUtil .bigIntegertoByteArray (privateKey .getS ());
169- if (d .length != 32 ) throw new IllegalArgumentException ("Invalid 'd' value (not 32 bytes): private key, length=" + d .length + " (" + privateKey .getS ().bitLength () + " bits)" );
169+ if (d .length < 30 || d .length > 32 ) throw new IllegalArgumentException ("Invalid 'd' value (<30 or >32 bytes): private key, length=" + d .length + " (" + privateKey .getS ().bitLength () + " bits)" );
170+ d = ByteArrayUtil .padArrayZeros (d , 32 );
170171
171172 ECPoint publicKeyPoint ;
172173 try {
@@ -179,9 +180,11 @@ public static JWK P_256PrivateKey_to_JWK(ECPrivateKey privateKey, String kid, St
179180 }
180181
181182 byte [] x = ByteArrayUtil .bigIntegertoByteArray (publicKeyPoint .getAffineX ());
182- if (x .length != 32 ) throw new IllegalArgumentException ("Invalid 'x' value (not 32 bytes): " + Hex .encodeHexString (x ) + ", length=" + x .length + " (" + publicKeyPoint .getAffineX ().bitLength () + " bits)" );
183+ if (x .length < 30 || x .length > 32 ) throw new IllegalArgumentException ("Invalid 'x' value (<30 or >32 bytes): " + new String (Hex .encodeHex (x )) + ", length=" + x .length + " (" + publicKeyPoint .getAffineX ().bitLength () + " bits)" );
184+ x = ByteArrayUtil .padArrayZeros (x , 32 );
183185 byte [] y = ByteArrayUtil .bigIntegertoByteArray (publicKeyPoint .getAffineY ());
184- if (y .length != 32 ) throw new IllegalArgumentException ("Invalid 'y' value (not 32 bytes): " + Hex .encodeHexString (y ) + ", length=" + y .length + " (" + publicKeyPoint .getAffineY ().bitLength () + " bits)" );
186+ if (y .length < 30 || y .length > 32 ) throw new IllegalArgumentException ("Invalid 'y' value (<30 or >32 bytes): " + new String (Hex .encodeHex (y )) + ", length=" + y .length + " (" + publicKeyPoint .getAffineY ().bitLength () + " bits)" );
187+ y = ByteArrayUtil .padArrayZeros (y , 32 );
185188
186189 JWK jsonWebKey = new JWK ();
187190 jsonWebKey .setKty (KeyType .EC );
@@ -198,7 +201,8 @@ public static JWK P_256PrivateKey_to_JWK(ECPrivateKey privateKey, String kid, St
198201 public static JWK P_384PrivateKey_to_JWK (ECPrivateKey privateKey , String kid , String use ) {
199202
200203 byte [] d = ByteArrayUtil .bigIntegertoByteArray (privateKey .getS ());
201- if (d .length != 48 ) throw new IllegalArgumentException ("Invalid 'd' value (not 48 bytes): private key, length=" + d .length + " (" + privateKey .getS ().bitLength () + " bits)" );
204+ if (d .length < 46 || d .length > 48 ) throw new IllegalArgumentException ("Invalid 'd' value (<46 or >48 bytes): private key, length=" + d .length + " (" + privateKey .getS ().bitLength () + " bits)" );
205+ d = ByteArrayUtil .padArrayZeros (d , 48 );
202206
203207 ECPoint publicKeyPoint ;
204208 try {
@@ -211,9 +215,11 @@ public static JWK P_384PrivateKey_to_JWK(ECPrivateKey privateKey, String kid, St
211215 }
212216
213217 byte [] x = ByteArrayUtil .bigIntegertoByteArray (publicKeyPoint .getAffineX ());
214- if (x .length != 48 ) throw new IllegalArgumentException ("Invalid 'x' value (not 48 bytes): " + Hex .encodeHexString (x ) + ", length=" + x .length + " (" + publicKeyPoint .getAffineX ().bitLength () + " bits)" );
218+ if (x .length < 46 || x .length > 48 ) throw new IllegalArgumentException ("Invalid 'x' value (<46 or >48 bytes): " + new String (Hex .encodeHex (x )) + ", length=" + x .length + " (" + publicKeyPoint .getAffineX ().bitLength () + " bits)" );
219+ x = ByteArrayUtil .padArrayZeros (x , 48 );
215220 byte [] y = ByteArrayUtil .bigIntegertoByteArray (publicKeyPoint .getAffineY ());
216- if (y .length != 48 ) throw new IllegalArgumentException ("Invalid 'y' value (not 48 bytes): " + Hex .encodeHexString (y ) + ", length=" + y .length + " (" + publicKeyPoint .getAffineY ().bitLength () + " bits)" );
221+ if (y .length < 46 || y .length > 48 ) throw new IllegalArgumentException ("Invalid 'y' value (<46 or >48 bytes): " + new String (Hex .encodeHex (y )) + ", length=" + y .length + " (" + publicKeyPoint .getAffineY ().bitLength () + " bits)" );
222+ y = ByteArrayUtil .padArrayZeros (y , 48 );
217223
218224 JWK jsonWebKey = new JWK ();
219225 jsonWebKey .setKty (KeyType .EC );
@@ -230,7 +236,7 @@ public static JWK P_384PrivateKey_to_JWK(ECPrivateKey privateKey, String kid, St
230236 public static JWK P_521PrivateKey_to_JWK (ECPrivateKey privateKey , String kid , String use ) {
231237
232238 byte [] d = ByteArrayUtil .bigIntegertoByteArray (privateKey .getS ());
233- if (d .length != 64 && d .length != 65 && d . length != 66 ) throw new IllegalArgumentException ("Invalid 'd' value (not 64 or 65 or 66 bytes): private key, length=" + d .length + " (" + privateKey .getS ().bitLength () + " bits)" );
239+ if (d .length < 64 || d .length > 66 ) throw new IllegalArgumentException ("Invalid 'd' value (< 64 or > 66 bytes): private key, length=" + d .length + " (" + privateKey .getS ().bitLength () + " bits)" );
234240 d = ByteArrayUtil .padArrayZeros (d , 66 );
235241
236242 ECPoint publicKeyPoint ;
@@ -244,10 +250,10 @@ public static JWK P_521PrivateKey_to_JWK(ECPrivateKey privateKey, String kid, St
244250 }
245251
246252 byte [] x = ByteArrayUtil .bigIntegertoByteArray (publicKeyPoint .getAffineX ());
247- if (x .length != 64 && x .length != 65 && x . length != 66 ) throw new IllegalArgumentException ("Invalid 'x' value (not 64 or 65 or bytes): " + Hex .encodeHexString ( x ) + ", length=" + x .length + " (" + publicKeyPoint .getAffineX ().bitLength () + " bits)" );
253+ if (x .length < 64 || x .length > 66 ) throw new IllegalArgumentException ("Invalid 'x' value (< 64 or >66 bytes): " + new String ( Hex .encodeHex ( x ) ) + ", length=" + x .length + " (" + publicKeyPoint .getAffineX ().bitLength () + " bits)" );
248254 x = ByteArrayUtil .padArrayZeros (x , 66 );
249255 byte [] y = ByteArrayUtil .bigIntegertoByteArray (publicKeyPoint .getAffineY ());
250- if (y .length != 64 && y .length != 65 && y . length != 66 ) throw new IllegalArgumentException ("Invalid 'y' value (not 64 or 65 or 66 bytes): " + Hex .encodeHexString ( y ) + ", length=" + y .length + " (" + publicKeyPoint .getAffineY ().bitLength () + " bits)" );
256+ if (y .length < 64 || y .length > 66 ) throw new IllegalArgumentException ("Invalid 'y' value (< 64 or > 66 bytes): " + new String ( Hex .encodeHex ( y ) ) + ", length=" + y .length + " (" + publicKeyPoint .getAffineY ().bitLength () + " bits)" );
251257 y = ByteArrayUtil .padArrayZeros (y , 66 );
252258
253259 JWK jsonWebKey = new JWK ();
0 commit comments