From 9c10ada9060c9745f49652469e172be8152f6cad Mon Sep 17 00:00:00 2001
From: Mike Macgirvin <mike@macgirvin.com>
Date: Wed, 6 Oct 2010 21:13:15 -0700
Subject: [PATCH] salmon-magic-public-freaking-nasty-keys

---
 library/asn1.php    |  9 ++-------
 mod/modexp.php      | 33 +++++++++++++++++++++++++++++++++
 mod/xrd.php         |  3 ++-
 view/xrd_person.tpl |  5 ++++-
 4 files changed, 41 insertions(+), 9 deletions(-)
 create mode 100644 mod/modexp.php

diff --git a/library/asn1.php b/library/asn1.php
index 5b1ad3b8fb..132b032480 100644
--- a/library/asn1.php
+++ b/library/asn1.php
@@ -186,7 +186,7 @@ class ASN_BASE {
 			case ASN_BOOLEAN:
 				return new ASN_BOOLEAN((bool)$data);
 			case ASN_INTEGER:
-				return new ASN_INTEGER(accum($data));
+				return new ASN_INTEGER(strtr(base64_encode($data),'+/=','-_,'));
 //				return new ASN_INTEGER(ord($data));
 			case ASN_BIT_STR:
 				return new ASN_BIT_STR(self::parseASNString($data, $level+1, $maxLevels));
@@ -292,12 +292,7 @@ class ASN_BASE {
 
 
 function accum($s) {
-	$len = strlen($s);
-	$result = '';
-	for ($i=0; $i < $len; $i++)  {
-		$cur = substr($s,$i,1);
-		$result .= bin2hex($cur);
-    }
+	$result = strtr(base64_encode($s),'+/=','-_,');
     return $result;
 }
 
diff --git a/mod/modexp.php b/mod/modexp.php
new file mode 100644
index 0000000000..d424b4c39d
--- /dev/null
+++ b/mod/modexp.php
@@ -0,0 +1,33 @@
+<?php
+
+include('library/asn1.php');
+
+function modexp_init(&$a) {
+
+	if($a->argc != 2)
+		killme();
+
+	$nick = $a->argv[1];
+	$r = q("SELECT `pubkey` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
+			dbesc($nick)
+	);
+
+	if(! count($r))
+		killme();
+
+	$lines = explode("\n",$r[0]['pubkey']);
+	unset($lines[0]);
+	unset($lines[count($lines)]);
+	$x = base64_decode(implode('',$lines));
+
+	$r = ASN_BASE::parseASNString($x);
+
+	$m = $r[0]->asnData[1]->asnData[0]->asnData[0]->asnData;
+	$e = $r[0]->asnData[1]->asnData[0]->asnData[1]->asnData;
+
+	header("Content-type: application/magic-public-key");
+	echo 'RSA' . '.' . $m . '.' . $e ;
+
+	killme();
+
+}
\ No newline at end of file
diff --git a/mod/xrd.php b/mod/xrd.php
index d36b47519b..06b2442b23 100644
--- a/mod/xrd.php
+++ b/mod/xrd.php
@@ -22,7 +22,8 @@ function xrd_content(&$a) {
 		'$accturi' => $uri,
 		'$profile_url' => $a->get_baseurl() . '/profile/' . $r[0]['nickname'],
 		'$photo' => $a->get_baseurl() . '/photo/profile/' . $r[0]['uid'],
-		'$salmon' => $a->get_baseurl() . '/salmon/' . $r[0]['nickname'] . '/mention'
+		'$salmon' => $a->get_baseurl() . '/salmon/' . $r[0]['nickname'] . '/mention',
+		'$modexp' => $a->get_baseurl() . '/modexp/' . $r[0]['nickname']
 	));
 
 	echo $o;
diff --git a/view/xrd_person.tpl b/view/xrd_person.tpl
index b559bfadcd..ba9bc64db1 100644
--- a/view/xrd_person.tpl
+++ b/view/xrd_person.tpl
@@ -15,5 +15,8 @@
     <Link rel='http://webfinger.net/rel/avatar'
           href='$photo' />
     <Link rel="salmon" href="$salmon" />
-
+	<Link rel="magic-public-key" 
+		type="application/magic-public-key"
+		href="$modexp" />
+ 
 </XRD>