@@ -1,3 +1,9 @@ | |||||
2.0.7 2014-11-11 | |||||
- Use OpenSSL for now | |||||
2.0.6 2014-11-10 | |||||
- Minor bug fixes | |||||
2.0.5 2014-11-09 | 2.0.5 2014-11-09 | ||||
- Fix QRCode size | - Fix QRCode size | ||||
- Share over LAN option | - Share over LAN option | ||||
@@ -10,7 +10,7 @@ namespace Shadowsocks.Encrypt | |||||
return new TableEncryptor(method, password); | return new TableEncryptor(method, password); | ||||
} | } | ||||
return new PolarSSLEncryptor(method, password); | |||||
return new OpenSSLEncryptor(method, password); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -0,0 +1,45 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Runtime.InteropServices; | |||||
using System.Text; | |||||
namespace Shadowsocks.Encrypt | |||||
{ | |||||
public class OpenSSL | |||||
{ | |||||
const string DLLNAME = "libeay32"; | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static void OpenSSL_add_all_ciphers(); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static IntPtr EVP_md5(); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_BytesToKey(IntPtr type, IntPtr md, IntPtr salt, byte[] data, int datal, int count, byte[] key, byte[] iv); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int RAND_bytes(byte[] buf, int num); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static IntPtr EVP_get_cipherbyname(byte[] name); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static IntPtr EVP_CIPHER_CTX_new(); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CipherInit_ex(IntPtr ctx, IntPtr type, IntPtr impl, byte[] key, byte[] iv, int enc); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CIPHER_CTX_cleanup(IntPtr a); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CIPHER_CTX_free(IntPtr a); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static int EVP_CipherUpdate(IntPtr ctx, byte[] outb, out int outl, byte[] inb, int inl); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public extern static IntPtr MD5(byte[] d, long n, byte[] md); | |||||
} | |||||
} |
@@ -0,0 +1,170 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Runtime.InteropServices; | |||||
using System.Text; | |||||
namespace Shadowsocks.Encrypt | |||||
{ | |||||
public class OpenSSLEncryptor | |||||
: EncryptorBase, IDisposable | |||||
{ | |||||
static Dictionary<string, int[]> ciphers = new Dictionary<string, int[]> { | |||||
{"aes-128-cfb", new int[]{16, 16}}, | |||||
{"aes-192-cfb", new int[]{24, 16}}, | |||||
{"aes-256-cfb", new int[]{32, 16}}, | |||||
{"bf-cfb", new int[]{16, 8}}, | |||||
{"rc4", new int[]{16, 0}}, | |||||
{"rc4-md5", new int[]{16, 16}}, | |||||
}; | |||||
static OpenSSLEncryptor() | |||||
{ | |||||
OpenSSL.OpenSSL_add_all_ciphers(); | |||||
} | |||||
public OpenSSLEncryptor(string method, string password) | |||||
: base(method, password) | |||||
{ | |||||
InitKey(method, password); | |||||
} | |||||
static byte[] tempbuf = new byte[32768]; | |||||
public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) | |||||
{ | |||||
if (_encryptCtx == IntPtr.Zero) | |||||
{ | |||||
OpenSSL.RAND_bytes(outbuf, ivLen); | |||||
InitCipher(ref _encryptCtx, outbuf, true); | |||||
outlength = length + ivLen; | |||||
OpenSSL.EVP_CipherUpdate(_encryptCtx, tempbuf, out outlength, buf, length); | |||||
outlength = length + ivLen; | |||||
Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, outlength); | |||||
} | |||||
else | |||||
{ | |||||
outlength = length; | |||||
OpenSSL.EVP_CipherUpdate(_encryptCtx, outbuf, out outlength, buf, length); | |||||
} | |||||
} | |||||
public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) | |||||
{ | |||||
if (_decryptCtx == IntPtr.Zero) | |||||
{ | |||||
InitCipher(ref _decryptCtx, buf, false); | |||||
outlength = length - ivLen; | |||||
Buffer.BlockCopy(buf, ivLen, tempbuf, 0, length - ivLen); | |||||
OpenSSL.EVP_CipherUpdate(_decryptCtx, outbuf, out outlength, tempbuf, length - ivLen); | |||||
} | |||||
else | |||||
{ | |||||
outlength = length; | |||||
OpenSSL.EVP_CipherUpdate(_decryptCtx, outbuf, out outlength, buf, length); | |||||
} | |||||
} | |||||
private static readonly Dictionary<string, byte[]> CachedKeys = new Dictionary<string, byte[]>(); | |||||
private byte[] _key; | |||||
private IntPtr _encryptCtx; | |||||
private IntPtr _decryptCtx; | |||||
private IntPtr _cipher; | |||||
private string _method; | |||||
private int keyLen; | |||||
private int ivLen; | |||||
private void InitKey(string method, string password) | |||||
{ | |||||
method = method.ToLower(); | |||||
_method = method; | |||||
string k = method + ":" + password; | |||||
if (method == "rc4-md5") | |||||
{ | |||||
method = "rc4"; | |||||
} | |||||
_cipher = OpenSSL.EVP_get_cipherbyname(System.Text.Encoding.UTF8.GetBytes(method)); | |||||
if (_cipher == null) | |||||
{ | |||||
throw new Exception("method not found"); | |||||
} | |||||
keyLen = ciphers[_method][0]; | |||||
ivLen = ciphers[_method][1]; | |||||
if (CachedKeys.ContainsKey(k)) | |||||
{ | |||||
_key = CachedKeys[k]; | |||||
} | |||||
else | |||||
{ | |||||
byte[] passbuf = Encoding.UTF8.GetBytes(password); | |||||
_key = new byte[32]; | |||||
byte[] iv = new byte[16]; | |||||
OpenSSL.EVP_BytesToKey(_cipher, OpenSSL.EVP_md5(), IntPtr.Zero, passbuf, passbuf.Length, 1, _key, iv); | |||||
CachedKeys[k] = _key; | |||||
} | |||||
} | |||||
private void InitCipher(ref IntPtr ctx, byte[] iv, bool isCipher) | |||||
{ | |||||
ctx = OpenSSL.EVP_CIPHER_CTX_new(); | |||||
int enc = isCipher ? 1 : 0; | |||||
byte[] realkey; | |||||
IntPtr r = IntPtr.Zero; | |||||
if (_method == "rc4-md5") | |||||
{ | |||||
byte[] temp = new byte[keyLen + ivLen]; | |||||
realkey = new byte[keyLen]; | |||||
Array.Copy(_key, 0, temp, 0, keyLen); | |||||
Array.Copy(iv, 0, temp, keyLen, ivLen); | |||||
r = OpenSSL.MD5(temp, keyLen + ivLen, null); | |||||
Marshal.Copy(r, realkey, 0, keyLen); | |||||
} | |||||
else | |||||
{ | |||||
realkey = _key; | |||||
} | |||||
OpenSSL.EVP_CipherInit_ex(ctx, _cipher, IntPtr.Zero, realkey, iv, enc); | |||||
} | |||||
#region IDisposable | |||||
private bool _disposed; | |||||
public override void Dispose() | |||||
{ | |||||
Dispose(true); | |||||
GC.SuppressFinalize(this); | |||||
} | |||||
~OpenSSLEncryptor() | |||||
{ | |||||
Dispose(false); | |||||
} | |||||
protected virtual void Dispose(bool disposing) | |||||
{ | |||||
if (!_disposed) | |||||
{ | |||||
if (disposing) | |||||
{ | |||||
} | |||||
if (_encryptCtx.ToInt64() != 0) | |||||
{ | |||||
OpenSSL.EVP_CIPHER_CTX_cleanup(_encryptCtx); | |||||
OpenSSL.EVP_CIPHER_CTX_free(_encryptCtx); | |||||
_encryptCtx = IntPtr.Zero; | |||||
} | |||||
if (_decryptCtx.ToInt64() != 0) | |||||
{ | |||||
OpenSSL.EVP_CIPHER_CTX_cleanup(_decryptCtx); | |||||
OpenSSL.EVP_CIPHER_CTX_free(_decryptCtx); | |||||
_decryptCtx = IntPtr.Zero; | |||||
} | |||||
_disposed = true; | |||||
} | |||||
} | |||||
#endregion | |||||
} | |||||
} |
@@ -36,10 +36,10 @@ namespace Shadowsocks | |||||
return; | return; | ||||
} | } | ||||
string tempPath = Path.GetTempPath(); | string tempPath = Path.GetTempPath(); | ||||
string dllPath = tempPath + "/polarssl.dll"; | |||||
string dllPath = tempPath + "/libeay32.dll"; | |||||
try | try | ||||
{ | { | ||||
FileManager.UncompressFile(dllPath, Resources.polarssl_dll); | |||||
FileManager.UncompressFile(dllPath, Resources.libeay32_dll); | |||||
} | } | ||||
catch (IOException e) | catch (IOException e) | ||||
{ | { | ||||
@@ -60,6 +60,16 @@ namespace Shadowsocks.Properties { | |||||
} | } | ||||
} | } | ||||
/// <summary> | |||||
/// Looks up a localized resource of type System.Byte[]. | |||||
/// </summary> | |||||
internal static byte[] libeay32_dll { | |||||
get { | |||||
object obj = ResourceManager.GetObject("libeay32_dll", resourceCulture); | |||||
return ((byte[])(obj)); | |||||
} | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// Looks up a localized resource of type System.Byte[]. | /// Looks up a localized resource of type System.Byte[]. | ||||
/// </summary> | /// </summary> | ||||
@@ -118,6 +118,9 @@ | |||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||||
</resheader> | </resheader> | ||||
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> | <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> | ||||
<data name="libeay32_dll" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||||
<value>..\Data\libeay32.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||||
</data> | |||||
<data name="polarssl_dll" type="System.Resources.ResXFileRef, System.Windows.Forms"> | <data name="polarssl_dll" type="System.Resources.ResXFileRef, System.Windows.Forms"> | ||||
<value>..\Data\polarssl.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | <value>..\Data\polarssl.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||||
</data> | </data> | ||||
@@ -76,6 +76,8 @@ | |||||
<Compile Include="Controller\UpdateChecker.cs" /> | <Compile Include="Controller\UpdateChecker.cs" /> | ||||
<Compile Include="Encrypt\EncryptorBase.cs" /> | <Compile Include="Encrypt\EncryptorBase.cs" /> | ||||
<Compile Include="Encrypt\EncryptorFactory.cs" /> | <Compile Include="Encrypt\EncryptorFactory.cs" /> | ||||
<Compile Include="Encrypt\OpenSSL.cs" /> | |||||
<Compile Include="Encrypt\OpenSSLEncryptor.cs" /> | |||||
<Compile Include="Encrypt\PolarSSL.cs" /> | <Compile Include="Encrypt\PolarSSL.cs" /> | ||||
<Compile Include="Encrypt\PolarSSLEncryptor.cs" /> | <Compile Include="Encrypt\PolarSSLEncryptor.cs" /> | ||||
<Compile Include="Encrypt\TableEncryptor.cs" /> | <Compile Include="Encrypt\TableEncryptor.cs" /> | ||||
@@ -122,6 +124,7 @@ | |||||
<None Include="app.manifest"> | <None Include="app.manifest"> | ||||
<SubType>Designer</SubType> | <SubType>Designer</SubType> | ||||
</None> | </None> | ||||
<None Include="Data\libeay32.dll.gz" /> | |||||
<None Include="Data\polarssl.dll.gz" /> | <None Include="Data\polarssl.dll.gz" /> | ||||
<None Include="Data\polipo.exe.gz" /> | <None Include="Data\polipo.exe.gz" /> | ||||
<None Include="Properties\Settings.settings"> | <None Include="Properties\Settings.settings"> | ||||