3434import java .net .ProxySelector ;
3535import java .net .http .HttpClient ;
3636import java .nio .charset .StandardCharsets ;
37+ import java .nio .file .Files ;
38+ import java .nio .file .Path ;
39+ import java .nio .file .Paths ;
3740import java .security .KeyFactory ;
3841import java .security .NoSuchAlgorithmException ;
3942import java .security .PrivateKey ;
@@ -129,17 +132,29 @@ public OID4VPClient oid4VPClient(HttpClient httpClient, ObjectMapper objectMappe
129132
130133 }
131134
135+ private static InputStream openInputStream (String filepath ) throws IOException {
136+ Path path = Paths .get (filepath );
137+ if (Files .isDirectory (path )) {
138+ return null ;
139+ }
140+ if (Files .exists (path )) {
141+ return Files .newInputStream (path );
142+ }
143+ return null ;
144+ }
145+
132146 private static PrivateKey loadPrivateKey (String keyType , String filename ) {
133- try (InputStream is = Application . class . getClassLoader (). getResourceAsStream (filename )) {
147+ try (InputStream is = openInputStream (filename )) {
134148 if (is == null ) {
135- throw new IllegalArgumentException ("Resource not found: " + filename );
149+ throw new IllegalArgumentException ("Private key not found: " + filename );
136150 }
137151
138152 // Read PEM file content
139- String pem = new String (is .readAllBytes (), StandardCharsets .UTF_8 )
140- .replaceAll ("-----BEGIN (.*)-----" , "" )
141- .replaceAll ("-----END (.*)-----" , "" )
142- .replaceAll ("\\ s" , "" );
153+ String pem =
154+ new String (is .readAllBytes (), StandardCharsets .UTF_8 )
155+ .replaceAll ("-----BEGIN (.*)-----" , "" )
156+ .replaceAll ("-----END (.*)-----" , "" )
157+ .replaceAll ("\\ s" , "" );
143158
144159 // Base64 decode
145160 byte [] decoded = Base64 .getDecoder ().decode (pem );
@@ -149,7 +164,10 @@ private static PrivateKey loadPrivateKey(String keyType, String filename) {
149164 KeyFactory keyFactory = KeyFactory .getInstance (keyType ); // or "EC"
150165 return keyFactory .generatePrivate (keySpec );
151166 } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e ) {
152- throw new Oid4VpInitException (String .format ("Was not able to load the private key with type %s from %s" , keyType , filename ), e );
167+ throw new IllegalArgumentException (
168+ String .format (
169+ "Was not able to load the private key with type %s from %s" , keyType , filename ),
170+ e );
153171 }
154172 }
155173
@@ -165,9 +183,7 @@ private static List<X509Certificate> loadCertificates(String resource) {
165183 }
166184 return list ;
167185 } catch (IOException | CertificateException e ) {
168- throw new Oid4VpInitException (String .format ("Was not able to load the certificates from %s" , resource ), e );
186+ throw new IllegalArgumentException (String .format ("Was not able to load the certificates from %s" , resource ), e );
169187 }
170188 }
171-
172-
173189}
0 commit comments