@@ -119,6 +119,20 @@ private static function getNistCurve(string $curve): Curve
119
119
}
120
120
}
121
121
122
+ private static function getNistCurveSize (string $ curve ): int
123
+ {
124
+ switch ($ curve ) {
125
+ case 'P-256 ' :
126
+ return 256 ;
127
+ case 'P-384 ' :
128
+ return 384 ;
129
+ case 'P-521 ' :
130
+ return 521 ;
131
+ default :
132
+ throw new InvalidArgumentException (sprintf ('The curve "%s" is not supported. ' , $ curve ));
133
+ }
134
+ }
135
+
122
136
private static function createECKeyUsingPurePhp (string $ curve ): array
123
137
{
124
138
$ nistCurve = self ::getNistCurve ($ curve );
@@ -152,14 +166,14 @@ private static function createECKeyUsingOpenSSL(string $curve): array
152
166
throw new RuntimeException ('Unable to create the key ' );
153
167
}
154
168
$ details = openssl_pkey_get_details ($ res );
155
- $ nistCurve = self ::getNistCurve ($ curve );
169
+ $ nistCurveSize = self ::getNistCurveSize ($ curve );
156
170
157
171
return [
158
172
'kty ' => 'EC ' ,
159
173
'crv ' => $ curve ,
160
- 'd ' => Base64Url::encode (str_pad ($ details ['ec ' ]['d ' ], (int ) ceil ($ nistCurve -> getSize () / 8 ), "\0" , STR_PAD_LEFT )),
161
- 'x ' => Base64Url::encode (str_pad ($ details ['ec ' ]['x ' ], (int ) ceil ($ nistCurve -> getSize () / 8 ), "\0" , STR_PAD_LEFT )),
162
- 'y ' => Base64Url::encode (str_pad ($ details ['ec ' ]['y ' ], (int ) ceil ($ nistCurve -> getSize () / 8 ), "\0" , STR_PAD_LEFT )),
174
+ 'd ' => Base64Url::encode (str_pad ($ details ['ec ' ]['d ' ], (int ) ceil ($ nistCurveSize / 8 ), "\0" , STR_PAD_LEFT )),
175
+ 'x ' => Base64Url::encode (str_pad ($ details ['ec ' ]['x ' ], (int ) ceil ($ nistCurveSize / 8 ), "\0" , STR_PAD_LEFT )),
176
+ 'y ' => Base64Url::encode (str_pad ($ details ['ec ' ]['y ' ], (int ) ceil ($ nistCurveSize / 8 ), "\0" , STR_PAD_LEFT )),
163
177
];
164
178
}
165
179
@@ -281,8 +295,8 @@ private static function p521PrivateKey(JWK $jwk): string
281
295
282
296
private static function getKey (JWK $ jwk ): string
283
297
{
284
- $ nistCurve = self ::getNistCurve ($ jwk ->get ('crv ' ));
285
- $ length = (int ) ceil ($ nistCurve -> getSize () / 8 );
298
+ $ nistCurveSize = self ::getNistCurveSize ($ jwk ->get ('crv ' ));
299
+ $ length = (int ) ceil ($ nistCurveSize / 8 );
286
300
287
301
return
288
302
"\04"
0 commit comments