diff options
author | Friendika <info@friendika.com> | 2011-07-25 19:57:17 -0700 |
---|---|---|
committer | Friendika <info@friendika.com> | 2011-07-25 19:57:17 -0700 |
commit | ec52010e1662cd37640096b65d60fd26fbe6c172 (patch) | |
tree | 4b589d160d21601970fa5cc1bbe4c6ba3303c9a1 /include/certfns.php | |
parent | c0749f18d62e9d9fa53a3a60ba580dadffc0ab1b (diff) | |
download | volse-hubzilla-ec52010e1662cd37640096b65d60fd26fbe6c172.tar.gz volse-hubzilla-ec52010e1662cd37640096b65d60fd26fbe6c172.tar.bz2 volse-hubzilla-ec52010e1662cd37640096b65d60fd26fbe6c172.zip |
helper functions for Diaspora cert mangling
Diffstat (limited to 'include/certfns.php')
-rw-r--r-- | include/certfns.php | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/include/certfns.php b/include/certfns.php new file mode 100644 index 000000000..70d2b54a0 --- /dev/null +++ b/include/certfns.php @@ -0,0 +1,52 @@ +<?php + +require_once('library/ASNValue.class.php'); + +function DerToPem($Der, $Private=false) +{ + //Encode: + $Der = base64_encode($Der); + //Split lines: + $lines = str_split($Der, 65); + $body = implode("\n", $lines); + //Get title: + $title = $Private? 'RSA PRIVATE KEY' : 'PUBLIC KEY'; + //Add wrapping: + $result = "-----BEGIN {$title}-----\n"; + $result .= $body . "\n"; + $result .= "-----END {$title}-----\n"; + + return $result; +} + +function pkcs8_encode($Modulus,$PublicExponent) { + //Encode key sequence + $modulus = new ASNValue(ASNValue::TAG_INTEGER); + $modulus->SetIntBuffer($Modulus); + $publicExponent = new ASNValue(ASNValue::TAG_INTEGER); + $publicExponent->SetInt($PublicExponent); + $keySequenceItems = array($modulus, $publicExponent); + $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE); + $keySequence->SetSequence($keySequenceItems); + //Encode bit string + $bitStringValue = $keySequence->Encode(); + $bitStringValue = chr(0x00) . $bitStringValue; //Add unused bits byte + $bitString = new ASNValue(ASNValue::TAG_BITSTRING); + $bitString->Value = $bitStringValue; + //Encode body + $bodyValue = "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00" . $bitString->Encode(); + $body = new ASNValue(ASNValue::TAG_SEQUENCE); + $body->Value = $bodyValue; + //Get DER encoded public key: + $PublicDER = $body->Encode(); + return $PublicDER; +} + + +function metopem($m,$e) { + $der = pkcs8_emcode($m,$e); + $key = DerToPem($der,true); + return $key; +} + + |