aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2015-01-12 19:27:46 +0100
committerVincent Breitmoser <valodim@mugenguild.com>2015-01-12 19:27:46 +0100
commite0847cafaf53eac9b364343c1f5e74554b51053d (patch)
treec1ac24aeca5a3d090be8d1773f710e2e4ecdc0fd /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources
parent5faeb5f5f060e049000e804deca5445d281f8611 (diff)
downloadopen-keychain-e0847cafaf53eac9b364343c1f5e74554b51053d.tar.gz
open-keychain-e0847cafaf53eac9b364343c1f5e74554b51053d.tar.bz2
open-keychain-e0847cafaf53eac9b364343c1f5e74554b51053d.zip
even more intermediate result
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/DnsResouce.java45
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/GenericHttpsResource.java74
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/TwitterResource.java117
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/UnknownResource.java6
4 files changed, 192 insertions, 50 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/DnsResouce.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/DnsResouce.java
index 3e39a695d..20216972a 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/DnsResouce.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/DnsResouce.java
@@ -1,4 +1,47 @@
package org.sufficientlysecure.keychain.pgp.affirmation.resources;
-public class DnsResouce {
+import android.content.Context;
+
+import org.sufficientlysecure.keychain.Constants;
+import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
+import org.sufficientlysecure.keychain.pgp.affirmation.AffirmationResource;
+import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
+import org.sufficientlysecure.keychain.util.Log;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Set;
+
+import de.measite.minidns.Client;
+import de.measite.minidns.DNSMessage;
+import de.measite.minidns.Question;
+import de.measite.minidns.Record;
+import de.measite.minidns.Record.TYPE;
+import de.measite.minidns.record.TXT;
+
+public class DnsResouce extends AffirmationResource {
+
+ DnsResouce(Set<String> flags, HashMap<String,String> params, URI uri) {
+ super(flags, params, uri);
+ }
+
+ public static String generate (Context context, byte[] fingerprint, String nonce) {
+
+ return "pgpid+cookie:"
+ + KeyFormattingUtils.convertFingerprintToHex(fingerprint) + ";" + nonce + "";
+
+ }
+
+ @Override
+ protected String fetchResource (OperationLog log, int indent) {
+
+ Client c = new Client();
+ DNSMessage msg = c.query(new Question("mugenguild.com", TYPE.TXT));
+ Record aw = msg.getAnswers()[0];
+ TXT txt = (TXT) aw.getPayload();
+ Log.d(Constants.TAG, txt.getText());
+ return txt.getText();
+
+ }
+
}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/GenericHttpsResource.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/GenericHttpsResource.java
index 42615d105..c8c3cbb4d 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/GenericHttpsResource.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/GenericHttpsResource.java
@@ -5,9 +5,14 @@ import android.content.Context;
import com.textuality.keybase.lib.Search;
import org.sufficientlysecure.keychain.Constants;
+import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
+import org.sufficientlysecure.keychain.operations.results.LinkedVerifyResult;
+import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
+import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify;
import org.sufficientlysecure.keychain.pgp.Progressable;
+import org.sufficientlysecure.keychain.pgp.affirmation.Affirmation;
import org.sufficientlysecure.keychain.pgp.affirmation.AffirmationResource;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
@@ -17,7 +22,6 @@ import org.sufficientlysecure.keychain.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
@@ -33,58 +37,26 @@ public class GenericHttpsResource extends AffirmationResource {
super(flags, params, uri);
}
- @Override
- public boolean verify() {
- return false;
- }
-
- public static String generate (byte[] fingerprint, String uri) {
- long nonce = generateNonce();
-
- StringBuilder b = new StringBuilder();
- b.append("---\r\n");
-
- b.append("fingerprint=");
- b.append(KeyFormattingUtils.convertFingerprintToHex(fingerprint));
- b.append('\r').append('\n');
+ public static String generateText (Context context, byte[] fingerprint, String nonce) {
+ String cookie = AffirmationResource.generate(context, fingerprint, nonce);
- b.append("nonce=");
- b.append(nonce);
- b.append('\r').append('\n');
-
- if (uri != null) {
- b.append("uri=");
- b.append(uri);
- b.append('\r').append('\n');
- }
- b.append("---\r\n");
-
- return b.toString();
+ return String.format(context.getResources().getString(R.string.linked_id_generic_text),
+ cookie, "0x" + KeyFormattingUtils.convertFingerprintToHex(fingerprint).substring(24));
}
- public DecryptVerifyResult verify
- (Context context, ProviderHelper providerHelper, Progressable progress)
- throws IOException {
-
- byte[] data = fetchResource(mUri).getBytes();
- InputData input = new InputData(new ByteArrayInputStream(data), data.length);
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- PgpDecryptVerify.Builder b =
- new PgpDecryptVerify.Builder(context, providerHelper, progress, input, out);
- PgpDecryptVerify op = b.build();
-
- Log.d(Constants.TAG, new String(out.toByteArray()));
-
- return op.execute();
- }
+ @Override
+ protected String fetchResource (OperationLog log, int indent) {
- protected static String fetchResource (URI uri) throws IOException {
+ log.add(LogType.MSG_LV_FETCH, indent, mUri.toString());
+ indent += 1;
try {
+
HttpsURLConnection conn = null;
- URL url = uri.toURL();
+ URL url = mUri.toURL();
int status = 0;
int redirects = 0;
+
while (redirects < 5) {
conn = (HttpsURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", "OpenKeychain");
@@ -95,18 +67,28 @@ public class GenericHttpsResource extends AffirmationResource {
if (status == 301) {
redirects++;
url = new URL(conn.getHeaderFields().get("Location").get(0));
+ log.add(LogType.MSG_LV_FETCH_REDIR, indent, url.toString());
} else {
break;
}
}
+
if (status >= 200 && status < 300) {
+ log.add(LogType.MSG_LV_FETCH_OK, indent, Integer.toString(status));
return Search.snarf(conn.getInputStream());
} else {
- throw new IOException("Fetch failed, status " + status + ": " + Search.snarf(conn.getErrorStream()));
+ // log verbose output to logcat
+ Log.e(Constants.TAG, Search.snarf(conn.getErrorStream()));
+ log.add(LogType.MSG_LV_FETCH_ERROR, indent, Integer.toString(status));
+ return null;
}
} catch (MalformedURLException e) {
- throw new IOException(e);
+ log.add(LogType.MSG_LV_FETCH_ERROR_URL, indent);
+ return null;
+ } catch (IOException e) {
+ log.add(LogType.MSG_LV_FETCH_ERROR_IO, indent);
+ return null;
}
}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/TwitterResource.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/TwitterResource.java
index 4fc3590f8..b426c16b9 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/TwitterResource.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/TwitterResource.java
@@ -1,4 +1,119 @@
package org.sufficientlysecure.keychain.pgp.affirmation.resources;
-public class TwitterResource {
+import android.util.Base64;
+import android.util.JsonReader;
+
+import com.textuality.keybase.lib.JWalk;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.BasicHttpParams;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
+import org.sufficientlysecure.keychain.pgp.affirmation.AffirmationResource;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Set;
+
+public class TwitterResource extends AffirmationResource {
+
+ TwitterResource(Set<String> flags, HashMap<String,String> params, URI uri) {
+ super(flags, params, uri);
+ }
+
+ private String getTwitterStream(String screenName) {
+ String results = null;
+
+ // Step 1: Encode consumer key and secret
+ try {
+ // URL encode the consumer key and secret
+ String urlApiKey = URLEncoder.encode("6IhPnWbYxASAoAzH2QaUtHD0J", "UTF-8");
+ String urlApiSecret = URLEncoder.encode("L0GnuiOnapWbSBbQtLIqtpeS5BTtvh06dmoMoKQfHQS8UwHuWm", "UTF-8");
+
+ // Concatenate the encoded consumer key, a colon character, and the
+ // encoded consumer secret
+ String combined = urlApiKey + ":" + urlApiSecret;
+
+ // Base64 encode the string
+ String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP);
+
+ // Step 2: Obtain a bearer token
+ HttpPost httpPost = new HttpPost("https://api.twitter.com/oauth2/token");
+ httpPost.setHeader("Authorization", "Basic " + base64Encoded);
+ httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
+ httpPost.setEntity(new StringEntity("grant_type=client_credentials"));
+ JSONObject rawAuthorization = new JSONObject(getResponseBody(httpPost));
+ String auth = JWalk.getString(rawAuthorization, "access_token");
+
+ // Applications should verify that the value associated with the
+ // token_type key of the returned object is bearer
+ if (auth != null && JWalk.getString(rawAuthorization, "token_type").equals("bearer")) {
+
+ // Step 3: Authenticate API requests with bearer token
+ HttpGet httpGet =
+ new HttpGet("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" + screenName);
+
+ // construct a normal HTTPS request and include an Authorization
+ // header with the value of Bearer <>
+ httpGet.setHeader("Authorization", "Bearer " + auth);
+ httpGet.setHeader("Content-Type", "application/json");
+ // update the results with the body of the response
+ results = getResponseBody(httpGet);
+ }
+ } catch (UnsupportedEncodingException ex) {
+ } catch (JSONException ex) {
+ } catch (IllegalStateException ex1) {
+ }
+ return results;
+ }
+
+ private static String getResponseBody(HttpRequestBase request) {
+ StringBuilder sb = new StringBuilder();
+ try {
+
+ DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
+ HttpResponse response = httpClient.execute(request);
+ int statusCode = response.getStatusLine().getStatusCode();
+ String reason = response.getStatusLine().getReasonPhrase();
+
+ if (statusCode == 200) {
+
+ HttpEntity entity = response.getEntity();
+ InputStream inputStream = entity.getContent();
+
+ BufferedReader bReader = new BufferedReader(
+ new InputStreamReader(inputStream, "UTF-8"), 8);
+ String line = null;
+ while ((line = bReader.readLine()) != null) {
+ sb.append(line);
+ }
+ } else {
+ sb.append(reason);
+ }
+ } catch (UnsupportedEncodingException ex) {
+ } catch (ClientProtocolException ex1) {
+ } catch (IOException ex2) {
+ }
+ return sb.toString();
+ }
+
+ @Override
+ protected String fetchResource(OperationLog log, int indent) {
+ return getTwitterStream("Valodim");
+ }
+
}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/UnknownResource.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/UnknownResource.java
index e2d050eb4..2f67c948e 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/UnknownResource.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/affirmation/resources/UnknownResource.java
@@ -1,7 +1,9 @@
package org.sufficientlysecure.keychain.pgp.affirmation.resources;
+import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.affirmation.AffirmationResource;
+import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Set;
@@ -13,8 +15,8 @@ public class UnknownResource extends AffirmationResource {
}
@Override
- public boolean verify() {
- return false;
+ protected String fetchResource(OperationLog log, int indent) {
+ return null;
}
}