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