Skip to content

Commit 5b8e0e1

Browse files
authored
Merge pull request #19 from FIWARE/fix-key-loading
fix key loading
2 parents 76943d7 + 02b3e17 commit 5b8e0e1

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

src/main/java/org/fiware/iam/Application.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import java.net.ProxySelector;
3535
import java.net.http.HttpClient;
3636
import java.nio.charset.StandardCharsets;
37+
import java.nio.file.Files;
38+
import java.nio.file.Path;
39+
import java.nio.file.Paths;
3740
import java.security.KeyFactory;
3841
import java.security.NoSuchAlgorithmException;
3942
import 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

Comments
 (0)