PageRenderTime 113ms CodeModel.GetById 91ms app.highlight 7ms RepoModel.GetById 13ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/util/der/PEM.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 118 lines | 71 code | 10 blank | 37 comment | 11 complexity | b342165ec1ab3819a11eed2fe3493e94 MD5 | raw file
  1/**
  2 * PEM
  3 * 
  4 * A class to parse some PEM stuff.
  5 * Copyright (c) 2007 Henri Torgemane
  6 * 
  7 * See LICENSE.txt for full license information.
  8 */
  9package com.hurlant.util.der
 10{
 11	import com.hurlant.crypto.rsa.RSAKey;
 12	import com.hurlant.math.BigInteger;
 13	import com.hurlant.util.Base64;
 14	
 15	import flash.utils.ByteArray;
 16	import com.hurlant.util.Hex;
 17	
 18	public class PEM
 19	{
 20		private static const RSA_PRIVATE_KEY_HEADER:String = "-----BEGIN RSA PRIVATE KEY-----";
 21		private static const RSA_PRIVATE_KEY_FOOTER:String = "-----END RSA PRIVATE KEY-----";
 22		private static const RSA_PUBLIC_KEY_HEADER:String = "-----BEGIN PUBLIC KEY-----";
 23		private static const RSA_PUBLIC_KEY_FOOTER:String = "-----END PUBLIC KEY-----";
 24		private static const CERTIFICATE_HEADER:String = "-----BEGIN CERTIFICATE-----";
 25		private static const CERTIFICATE_FOOTER:String = "-----END CERTIFICATE-----";
 26		
 27		
 28		
 29		/**
 30		 * 
 31		 * Read a structure encoded according to
 32		 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
 33		 * section 11.1.2
 34		 * 
 35		 * @param str
 36		 * @return 
 37		 * 
 38		 */
 39		public static function readRSAPrivateKey(str:String):RSAKey {
 40			var der:ByteArray = extractBinary(RSA_PRIVATE_KEY_HEADER, RSA_PRIVATE_KEY_FOOTER, str);
 41			if (der==null) return null;
 42			var obj:* = DER.parse(der);
 43			if (obj is Array) {
 44				var arr:Array = obj as Array;
 45				// arr[0] is Version. should be 0. should be checked. shoulda woulda coulda.
 46				return new RSAKey(
 47					arr[1],				// N
 48					arr[2].valueOf(),	// E
 49					arr[3],				// D
 50					arr[4],				// P
 51					arr[5],				// Q
 52					arr[6],				// DMP1
 53					arr[7],				// DMQ1	
 54					arr[8]);			// IQMP
 55			} else {
 56				// dunno
 57				return null;
 58			}
 59		}
 60		
 61		
 62		/**
 63		 * Read a structure encoded according to some spec somewhere
 64		 * Also, follows some chunk from
 65		 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
 66		 * section 11.1
 67		 * 
 68		 * @param str
 69		 * @return 
 70		 * 
 71		 */
 72		public static function readRSAPublicKey(str:String):RSAKey {
 73			var der:ByteArray = extractBinary(RSA_PUBLIC_KEY_HEADER, RSA_PUBLIC_KEY_FOOTER, str);
 74			if (der==null) return null;
 75			var obj:* = DER.parse(der);
 76			if (obj is Array) {
 77				var arr:Array = obj as Array;
 78				// arr[0] = [ <some crap that means "rsaEncryption">, null ]; ( apparently, that's an X-509 Algorithm Identifier.
 79				if (arr[0][0].toString()!=OID.RSA_ENCRYPTION) {
 80					return null;
 81				}
 82				// arr[1] is a ByteArray begging to be parsed as DER
 83				arr[1].position = 1; // there's a 0x00 byte up front. find out why later. like, read a spec.
 84				obj = DER.parse(arr[1]);
 85				if (obj is Array) {
 86					arr = obj as Array;
 87					// arr[0] = modulus
 88					// arr[1] = public expt.
 89					return new RSAKey(arr[0], arr[1]);
 90				} else {
 91					return null;
 92				}
 93			} else {
 94				// dunno
 95				return null;
 96			}
 97		}
 98
 99		public static function readCertIntoArray(str:String):ByteArray {
100			var tmp:ByteArray = extractBinary(CERTIFICATE_HEADER, CERTIFICATE_FOOTER, str);
101			return tmp;
102		}
103		
104		private static function extractBinary(header:String, footer:String, str:String):ByteArray {
105			var i:int = str.indexOf(header);
106			if (i==-1) return null;
107			i += header.length;
108			var j:int = str.indexOf(footer);
109			if (j==-1) return null;
110			var b64:String = str.substring(i, j);
111			// remove whitesapces.
112			b64 = b64.replace(/\s/mg, '');
113			// decode
114			return Base64.decodeToByteArray(b64);
115		}
116		
117	}
118}