summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base
diff options
context:
space:
mode:
authorcyBerta <cyberta@riseup.net>2024-11-07 17:22:19 +0100
committercyberta <cyberta@riseup.net>2024-12-11 00:09:34 +0000
commitab4c3397c501e059849d16f05fa211d5ebd3c4db (patch)
tree2bcce219b2c0df1d17affb1bba2068f3416d937b /app/src/main/java/se/leap/bitmaskclient/base
parent44b33d83bdb1f5670eb0eb2adb13695aa8388667 (diff)
add CredentialsParser which is responsible to parse the reponse from /api/5/openvpn/cert endpoint
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/base')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/base/utils/CredentialsParser.java66
1 files changed, 66 insertions, 0 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/CredentialsParser.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/CredentialsParser.java
new file mode 100644
index 00000000..e02922fc
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/CredentialsParser.java
@@ -0,0 +1,66 @@
+package se.leap.bitmaskclient.base.utils;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlPullParserFactory;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import se.leap.bitmaskclient.base.models.Provider;
+
+public class CredentialsParser {
+
+ public static void parseXml(String xmlString, Provider provider) throws XmlPullParserException, IOException {
+ XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
+ XmlPullParser parser = factory.newPullParser();
+ parser.setInput(new StringReader(xmlString));
+
+ String currentTag = null;
+ String ca = null;
+ String key = null;
+ String cert = null;
+
+ int eventType = parser.getEventType();
+ while (eventType != XmlPullParser.END_DOCUMENT) {
+ switch (eventType) {
+ case XmlPullParser.START_TAG -> currentTag = parser.getName();
+ case XmlPullParser.TEXT -> {
+ if (currentTag != null) {
+ switch (currentTag) {
+ case "ca" -> {
+ ca = parser.getText();
+ ca = ca.replaceAll("-----BEGIN CERTIFICATE-----", "");
+ ca = ca.replaceAll("-----END CERTIFICATE-----", "");
+ ca = ca.trim();
+ }
+ case "key" -> {
+ key = parser.getText();
+ key = key.replaceAll("-----BEGIN PRIVATE KEY-----", "");
+ key = key.replaceAll("-----BEGIN RSA PRIVATE KEY-----", "");
+ key = key.replaceAll("-----END PRIVATE KEY-----", "");
+ key = key.replaceAll("-----END RSA PRIVATE KEY-----", "");
+ key = key.trim();
+ }
+ case "cert" -> {
+ cert = parser.getText();
+ cert = cert.replaceAll("-----BEGIN CERTIFICATE-----", "");
+ cert = cert.replaceAll("-----END CERTIFICATE-----", "");
+ cert = cert.trim();
+ }
+ }
+ }
+ }
+ case XmlPullParser.END_TAG -> currentTag = null;
+ }
+ eventType = parser.next();
+ }
+
+ provider.setCaCert(ca);
+ provider.setPrivateKeyString(key);
+ provider.setVpnCertificate(cert);
+
+ }
+}
+
+