@@ -249,14 +249,6 @@ public void testSetPublicKeyFile()
249249 /* RSA */
250250 req .setPublicKey (cliKeyPubDer , WolfSSL .RSAk ,
251251 WolfSSL .SSL_FILETYPE_ASN1 );
252- req .setPublicKey (cliKeyPem , WolfSSL .RSAk ,
253- WolfSSL .SSL_FILETYPE_PEM );
254-
255- /* ECC */
256- req .setPublicKey (cliEccKeyDer , WolfSSL .ECDSAk ,
257- WolfSSL .SSL_FILETYPE_ASN1 );
258- req .setPublicKey (cliEccKeyPem , WolfSSL .ECDSAk ,
259- WolfSSL .SSL_FILETYPE_PEM );
260252
261253 /* Test bad key type */
262254 try {
@@ -304,7 +296,14 @@ public void testSetPublicKeyFile()
304296 @ Test
305297 public void testSetPublicKeyArray ()
306298 throws WolfSSLException , WolfSSLJNIException , IOException ,
307- CertificateException {
299+ CertificateException , NoSuchAlgorithmException {
300+
301+ KeyPairGenerator rsaKpg ;
302+ KeyPairGenerator eccKpg ;
303+ KeyPair rsaKeyPair ;
304+ KeyPair eccKeyPair ;
305+ byte [] rsaPubKeyDer ;
306+ byte [] eccPubKeyDer ;
308307
309308 System .out .print ("\t setPublicKey(array)" );
310309
@@ -317,26 +316,29 @@ public void testSetPublicKeyArray()
317316 WolfSSLCertRequest req = new WolfSSLCertRequest ();
318317 assertNotNull (req );
319318
320- byte [] cliKeyRSADer = Files .readAllBytes (Paths .get (cliKeyDer ));
321- byte [] cliKeyRSAPem = Files .readAllBytes (Paths .get (cliKeyPem ));
322- byte [] cliKeyECCDer = Files .readAllBytes (Paths .get (cliEccKeyDer ));
323- byte [] cliKeyECCPem = Files .readAllBytes (Paths .get (cliEccKeyPem ));
319+ /* Generate RSA key pair and get public key bytes (DER encoded). */
320+ rsaKpg = KeyPairGenerator .getInstance ("RSA" );
321+ rsaKpg .initialize (2048 );
322+ rsaKeyPair = rsaKpg .generateKeyPair ();
323+ rsaPubKeyDer = rsaKeyPair .getPublic ().getEncoded ();
324324
325- /* RSA */
326- req .setPublicKey (cliKeyRSADer , WolfSSL .RSAk ,
325+ /* Generate ECC key pair and get public key bytes (DER encoded) */
326+ eccKpg = KeyPairGenerator .getInstance ("EC" );
327+ eccKpg .initialize (256 );
328+ eccKeyPair = eccKpg .generateKeyPair ();
329+ eccPubKeyDer = eccKeyPair .getPublic ().getEncoded ();
330+
331+ /* RSA public key DER */
332+ req .setPublicKey (rsaPubKeyDer , WolfSSL .RSAk ,
327333 WolfSSL .SSL_FILETYPE_ASN1 );
328- req .setPublicKey (cliKeyRSAPem , WolfSSL .RSAk ,
329- WolfSSL .SSL_FILETYPE_PEM );
330334
331- /* ECC */
332- req .setPublicKey (cliKeyECCDer , WolfSSL .ECDSAk ,
335+ /* ECC public key DER */
336+ req .setPublicKey (eccPubKeyDer , WolfSSL .ECDSAk ,
333337 WolfSSL .SSL_FILETYPE_ASN1 );
334- req .setPublicKey (cliKeyECCPem , WolfSSL .ECDSAk ,
335- WolfSSL .SSL_FILETYPE_PEM );
336338
337339 /* Test bad key type */
338340 try {
339- req .setPublicKey (cliKeyRSADer , 12345 ,
341+ req .setPublicKey (rsaPubKeyDer , 12345 ,
340342 WolfSSL .SSL_FILETYPE_ASN1 );
341343 System .out .println ("\t \t ... failed" );
342344 fail ("bad key type should throw exception" );
@@ -346,7 +348,7 @@ public void testSetPublicKeyArray()
346348
347349 /* Test bad file type */
348350 try {
349- req .setPublicKey (cliKeyRSADer , WolfSSL .RSAk , 12345 );
351+ req .setPublicKey (rsaPubKeyDer , WolfSSL .RSAk , 12345 );
350352 System .out .println ("\t \t ... failed" );
351353 fail ("bad file type should throw exception" );
352354 } catch (WolfSSLException e ) {
0 commit comments