/SharpSSH/jsch/IdentityFile.cs
C# | 964 lines | 738 code | 71 blank | 155 comment | 253 complexity | c91f0dcf1e7fdce7a7d5d4f0db7bee54 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- using System;
- using System.IO;
-
- using SharpSsh.Jsch;
- using SharpSsh;
-
- namespace SharpSsh.jsch
- {
- /*
- Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
-
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the distribution.
-
- 3. The names of the authors may not be used to endorse or promote products
- derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
- INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
- internal class IdentityFile : IIdentity
- {
- string m_identity;
- byte[] m_key;
- byte[] m_iv;
- private JSch m_jsch;
- private HashAlgorithm m_hash;
- private byte[] m_encoded_data;
-
- private Cipher m_cipher;
-
- // DSA
- private byte[] m_P;
- private byte[] m_Q;
- private byte[] m_G_array;
- private byte[] m_pub;
- private byte[] m_prv;
-
- // RSA
- private byte[] m_n; // modulus
- private byte[] m_e; // public exponent
- private byte[] m_d; // private exponent
-
- private byte[] m_p;
- private byte[] m_q;
- private byte[] m_dmp1;
- private byte[] m_dmq1;
- private byte[] m_iqmp;
-
- // private string algname="ssh-dss";
- //private string algname="ssh-rsa";
-
- private const int ERROR = 0;
- private const int RSA = 1;
- private const int DSS = 2;
- internal const int UNKNOWN = 3;
-
- private const int OPENSSH = 0;
- private const int FSECURE = 1;
- private const int PUTTY = 2;
-
- private int m_type = ERROR;
- private int m_keytype = OPENSSH;
-
- private byte[] m_publickeyblob = null;
-
- private bool m_encrypted = true;
-
- internal IdentityFile(string identity, JSch jsch)
- {
- this.m_identity = identity;
- this.m_jsch = jsch;
- try
- {
- Type c = Type.GetType(jsch.GetConfig("3des-cbc"));
- m_cipher = (Cipher)Activator.CreateInstance(c);
- m_key = new byte[m_cipher.BlockSize]; // 24
- m_iv = new byte[m_cipher.IVSize]; // 8
- c = Type.GetType(jsch.GetConfig("md5"));
- m_hash = (HashAlgorithm)(Activator.CreateInstance(c));
- m_hash.Init();
- FileInfo file = new FileInfo(identity);
- FileStream fis = File.OpenRead(identity);
- byte[] buf = new byte[(int)(file.Length)];
- int len = fis.Read(buf, 0, buf.Length);
- fis.Close();
-
- int i = 0;
- while (i < len)
- {
- if (buf[i] == 'B' && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf[i + 3] == 'I')
- {
- i += 6;
- if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { m_type = DSS; }
- else if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { m_type = RSA; }
- else if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H')
- { // FSecure
- m_type = UNKNOWN;
- m_keytype = FSECURE;
- }
- else
- {
- //System.out.println("invalid format: "+identity);
- throw new JSchException("invaid privatekey: " + identity);
- }
- i += 3;
- continue;
- }
- if (buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',')
- {
- i += 4;
- for (int ii = 0; ii < m_iv.Length; ii++)
- {
- m_iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) +
- (a2b(buf[i++]) & 0xf));
- }
- continue;
- }
- if (buf[i] == 0x0d &&
- i + 1 < buf.Length && buf[i + 1] == 0x0a)
- {
- i++;
- continue;
- }
- if (buf[i] == 0x0a && i + 1 < buf.Length)
- {
- if (buf[i + 1] == 0x0a) { i += 2; break; }
- if (buf[i + 1] == 0x0d &&
- i + 2 < buf.Length && buf[i + 2] == 0x0a)
- {
- i += 3; break;
- }
- bool inheader = false;
- for (int j = i + 1; j < buf.Length; j++)
- {
- if (buf[j] == 0x0a) break;
- //if(buf[j]==0x0d) break;
- if (buf[j] == ':') { inheader = true; break; }
- }
- if (!inheader)
- {
- i++;
- m_encrypted = false; // no passphrase
- break;
- }
- }
- i++;
- }
-
- if (m_type == ERROR)
- {
- throw new JSchException("invaid privatekey: " + identity);
- }
-
- int start = i;
- while (i < len)
- {
- if (buf[i] == 0x0a)
- {
- bool xd = (buf[i - 1] == 0x0d);
- Array.Copy(buf, i + 1,
- buf,
- i - (xd ? 1 : 0),
- len - i - 1 - (xd ? 1 : 0)
- );
- if (xd) len--;
- len--;
- continue;
- }
- if (buf[i] == '-') { break; }
- i++;
- }
- m_encoded_data = Util.FromBase64(buf, start, i - start);
-
- if (m_encoded_data.Length > 4 && // FSecure
- m_encoded_data[0] == (byte)0x3f &&
- m_encoded_data[1] == (byte)0x6f &&
- m_encoded_data[2] == (byte)0xf9 &&
- m_encoded_data[3] == (byte)0xeb)
- {
-
- ByteBuffer _buf = new ByteBuffer(m_encoded_data);
- _buf.GetInt(); // 0x3f6ff9be
- _buf.GetInt();
- byte[] _type = _buf.GetString();
- //System.out.println("type: "+new string(_type));
- byte[] _cipher = _buf.GetString();
- string s_cipher = System.Text.Encoding.Default.GetString(_cipher);
- //System.out.println("cipher: "+cipher);
- if (s_cipher.Equals("3des-cbc"))
- {
- _buf.GetInt();
- byte[] foo = new byte[m_encoded_data.Length - _buf.Offset];
- _buf.GetByte(foo);
- m_encoded_data = foo;
- m_encrypted = true;
- throw new JSchException("unknown privatekey format: " + identity);
- }
- else if (s_cipher.Equals("none"))
- {
- _buf.GetInt();
- //_buf.getInt();
-
- m_encrypted = false;
-
- byte[] foo = new byte[m_encoded_data.Length - _buf.Offset];
- _buf.GetByte(foo);
- m_encoded_data = foo;
- }
-
- }
-
- try
- {
- file = new FileInfo(identity + ".pub");
- fis = File.OpenRead(identity + ".pub");
- buf = new byte[(int)(file.Length)];
- len = fis.Read(buf, 0, buf.Length);
- fis.Close();
- }
- catch
- {
- return;
- }
-
- if (buf.Length > 4 && // FSecure's public key
- buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-')
- {
-
- i = 0;
- do { i++; } while (buf.Length > i && buf[i] != 0x0a);
- if (buf.Length <= i) return;
-
- while (true)
- {
- if (buf[i] == 0x0a)
- {
- bool inheader = false;
- for (int j = i + 1; j < buf.Length; j++)
- {
- if (buf[j] == 0x0a) break;
- if (buf[j] == ':') { inheader = true; break; }
- }
- if (!inheader)
- {
- i++;
- break;
- }
- }
- i++;
- }
- if (buf.Length <= i) return;
-
- start = i;
- while (i < len)
- {
- if (buf[i] == 0x0a)
- {
- Array.Copy(buf, i + 1, buf, i, len - i - 1);
- len--;
- continue;
- }
- if (buf[i] == '-') { break; }
- i++;
- }
- m_publickeyblob = Util.FromBase64(buf, start, i - start);
-
- if (m_type == UNKNOWN)
- {
- if (m_publickeyblob[8] == 'd')
- {
- m_type = DSS;
- }
- else if (m_publickeyblob[8] == 'r')
- {
- m_type = RSA;
- }
- }
- }
- else
- {
- if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-') return;
- i = 0;
- while (i < len) { if (buf[i] == ' ')break; i++; } i++;
- if (i >= len) return;
- start = i;
- while (i < len) { if (buf[i] == ' ')break; i++; }
- m_publickeyblob = Util.FromBase64(buf, start, i - start);
- }
-
- }
- catch (Exception e)
- {
- Trace.TraceError("Error:\r\n{0}", e.ToString());
- if (e is JSchException)
- throw (JSchException)e;
- throw new JSchException(e.ToString());
- }
-
- }
-
- public string AlgName
- {
- get
- {
- if (m_type == RSA)
- return "ssh-rsa";
- return "ssh-dss";
- }
- }
-
- public bool SetPassphrase(string _passphrase)
- {
- /*
- hash is MD5
- h(0) <- hash(passphrase, iv);
- h(n) <- hash(h(n-1), passphrase, iv);
- key <- (h(0),...,h(n))[0,..,key.Length];
- */
- try
- {
- if (m_encrypted)
- {
- if (_passphrase == null) return false;
- byte[] passphrase = System.Text.Encoding.Default.GetBytes(_passphrase);
- int hsize = m_hash.BlockSize;
- byte[] hn = new byte[m_key.Length / hsize * hsize +
- (m_key.Length % hsize == 0 ? 0 : hsize)];
- byte[] tmp = null;
- if (m_keytype == OPENSSH)
- {
- for (int index = 0; index + hsize <= hn.Length; )
- {
- if (tmp != null) { m_hash.Update(tmp, 0, tmp.Length); }
- m_hash.Update(passphrase, 0, passphrase.Length);
- m_hash.Update(m_iv, 0, m_iv.Length);
- tmp = m_hash.Digest();
- Array.Copy(tmp, 0, hn, index, tmp.Length);
- index += tmp.Length;
- }
- Array.Copy(hn, 0, m_key, 0, m_key.Length);
- }
- else if (m_keytype == FSECURE)
- {
- for (int index = 0; index + hsize <= hn.Length; )
- {
- if (tmp != null) { m_hash.Update(tmp, 0, tmp.Length); }
- m_hash.Update(passphrase, 0, passphrase.Length);
- tmp = m_hash.Digest();
- Array.Copy(tmp, 0, hn, index, tmp.Length);
- index += tmp.Length;
- }
- Array.Copy(hn, 0, m_key, 0, m_key.Length);
- }
- }
- if (Decrypt())
- {
- m_encrypted = false;
- return true;
- }
- m_P = m_Q = m_G_array = m_pub = m_prv = null;
- return false;
- }
- catch (Exception e)
- {
- if (e is JSchException) throw (JSchException)e;
- throw new JSchException(e.ToString());
- }
- }
-
- public byte[] PublicKeyBlob
- {
- get
- {
- if (m_publickeyblob != null)
- return m_publickeyblob;
- if (m_type == RSA)
- return PublicKeyBlob_rsa();
- return PublicKeyBlob_dss();
- }
- }
-
- byte[] PublicKeyBlob_rsa()
- {
- if (m_e == null) return null;
- ByteBuffer buf = new ByteBuffer("ssh-rsa".Length + 4 +
- m_e.Length + 4 +
- m_n.Length + 4);
- buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-rsa"));
- buf.PutString(m_e);
- buf.PutString(m_n);
- return buf.m_buffer;
- }
-
- byte[] PublicKeyBlob_dss()
- {
- if (m_P == null) return null;
- ByteBuffer buf = new ByteBuffer("ssh-dss".Length + 4 +
- m_P.Length + 4 +
- m_Q.Length + 4 +
- m_G_array.Length + 4 +
- m_pub.Length + 4);
- buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-dss"));
- buf.PutString(m_P);
- buf.PutString(m_Q);
- buf.PutString(m_G_array);
- buf.PutString(m_pub);
- return buf.m_buffer;
- }
-
- public byte[] GetSignature(Session session, byte[] data)
- {
- if (m_type == RSA) return GetSignature_rsa(session, data);
- return GetSignature_dss(session, data);
- }
-
- byte[] GetSignature_rsa(Session session, byte[] data)
- {
- try
- {
- Type t = Type.GetType(m_jsch.GetConfig("signature.rsa"));
- ISignatureRSA rsa = (ISignatureRSA)Activator.CreateInstance(t);
-
- rsa.Init();
- rsa.SetPrvKey(m_e, m_n, m_d, m_p, m_q, m_dmp1, m_dmq1, m_iqmp);
-
- /*
- byte[] goo=new byte[4];
- goo[0]=(byte)(session.getSessionId().Length>>>24);
- goo[1]=(byte)(session.getSessionId().Length>>>16);
- goo[2]=(byte)(session.getSessionId().Length>>>8);
- goo[3]=(byte)(session.getSessionId().Length);
- rsa.update(goo);
- rsa.update(session.getSessionId());
- */
- rsa.Update(data);
- byte[] sig = rsa.Sign();
- ByteBuffer buf = new ByteBuffer("ssh-rsa".Length + 4 +
- sig.Length + 4);
- buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-rsa"));
- buf.PutString(sig);
- return buf.m_buffer;
- }
- catch (Exception e)
- {
- Trace.TraceError("Error:\r\n{0}", e.ToString());
- }
- return null;
- }
-
- byte[] GetSignature_dss(Session session, byte[] data)
- {
- /*
- byte[] foo;
- int i;
- System.out.print("P ");
- foo=P_array;
- for(i=0; i<foo.Length; i++){
- System.out.print(Integer.toHexString(foo[i]&0xff)+":");
- }
- System.out.println("");
- System.out.print("Q ");
- foo=Q_array;
- for(i=0; i<foo.Length; i++){
- System.out.print(Integer.toHexString(foo[i]&0xff)+":");
- }
- System.out.println("");
- System.out.print("G ");
- foo=G_array;
- for(i=0; i<foo.Length; i++){
- System.out.print(Integer.toHexString(foo[i]&0xff)+":");
- }
- System.out.println("");
- */
-
- try
- {
- Type t = Type.GetType(m_jsch.GetConfig("signature.dss"));
- ISignatureDSA dsa = (ISignatureDSA)(Activator.CreateInstance(t));
- dsa.Init();
- dsa.SetPrvKey(m_prv, m_P, m_Q, m_G_array);
-
- /*
- byte[] goo=new byte[4];
- goo[0]=(byte)(session.getSessionId().Length>>>24);
- goo[1]=(byte)(session.getSessionId().Length>>>16);
- goo[2]=(byte)(session.getSessionId().Length>>>8);
- goo[3]=(byte)(session.getSessionId().Length);
- dsa.update(goo);
- dsa.update(session.getSessionId());
- */
- dsa.Update(data);
- byte[] sig = dsa.Sign();
- ByteBuffer buf = new ByteBuffer("ssh-dss".Length + 4 +
- sig.Length + 4);
- buf.PutString(System.Text.Encoding.Default.GetBytes("ssh-dss"));
- buf.PutString(sig);
- return buf.m_buffer;
- }
- catch (Exception e)
- {
- Trace.TraceError("Error:\r\n{0}", e.ToString());
- }
- return null;
- }
-
- public bool Decrypt()
- {
- if (m_type == RSA) return Decrypt_rsa();
- return Decrypt_dss();
- }
-
- bool Decrypt_rsa()
- {
- // byte[] p_array;
- // byte[] q_array;
- // byte[] dmp1_array;
- // byte[] dmq1_array;
- // byte[] iqmp_array;
-
- try
- {
- byte[] plain;
- if (m_encrypted)
- {
- if (m_keytype == OPENSSH)
- {
- m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
- plain = new byte[m_encoded_data.Length];
- m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
- }
- else if (m_keytype == FSECURE)
- {
- for (int i = 0; i < m_iv.Length; i++) m_iv[i] = 0;
- m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
- plain = new byte[m_encoded_data.Length];
- m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
- }
- else
- {
- return false;
- }
- }
- else
- {
- if (m_n != null) return true;
- plain = m_encoded_data;
- }
-
- if (m_keytype == FSECURE)
- { // FSecure
- ByteBuffer buf = new ByteBuffer(plain);
- int foo = buf.GetInt();
- if (plain.Length != foo + 4)
- {
- return false;
- }
- m_e = buf.GetMPIntBits();
- m_d = buf.GetMPIntBits();
- m_n = buf.GetMPIntBits();
- byte[] u_array = buf.GetMPIntBits();
- m_p = buf.GetMPIntBits();
- m_q = buf.GetMPIntBits();
- return true;
- }
-
- int index = 0;
- int Length = 0;
-
- if (plain[index] != 0x30) return false;
- index++; // SEQUENCE
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
-
- if (plain[index] != 0x02) return false;
- index++; // INTEGER
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- index += Length;
-
- //System.out.println("int: len="+Length);
- //System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
- //System.out.println("");
-
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_n = new byte[Length];
- Array.Copy(plain, index, m_n, 0, Length);
- index += Length;
- /*
- System.out.println("int: N len="+Length);
- for(int i=0; i<n_array.Length; i++){
- System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
- }
- System.out.println("");
- */
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_e = new byte[Length];
- Array.Copy(plain, index, m_e, 0, Length);
- index += Length;
- /*
- System.out.println("int: E len="+Length);
- for(int i=0; i<e_array.Length; i++){
- System.out.print(Integer.toHexString(e_array[i]&0xff)+":");
- }
- System.out.println("");
- */
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_d = new byte[Length];
- Array.Copy(plain, index, m_d, 0, Length);
- index += Length;
- /*
- System.out.println("int: D len="+Length);
- for(int i=0; i<d_array.Length; i++){
- System.out.print(Integer.toHexString(d_array[i]&0xff)+":");
- }
- System.out.println("");
- */
-
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_p = new byte[Length];
- Array.Copy(plain, index, m_p, 0, Length);
- index += Length;
- /*
- System.out.println("int: P len="+Length);
- for(int i=0; i<p_array.Length; i++){
- System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
- }
- System.out.println("");
- */
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_q = new byte[Length];
- Array.Copy(plain, index, m_q, 0, Length);
- index += Length;
- /*
- System.out.println("int: q len="+Length);
- for(int i=0; i<q_array.Length; i++){
- System.out.print(Integer.toHexString(q_array[i]&0xff)+":");
- }
- System.out.println("");
- */
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_dmp1 = new byte[Length];
- Array.Copy(plain, index, m_dmp1, 0, Length);
- index += Length;
- /*
- System.out.println("int: dmp1 len="+Length);
- for(int i=0; i<dmp1_array.Length; i++){
- System.out.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
- }
- System.out.println("");
- */
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_dmq1 = new byte[Length];
- Array.Copy(plain, index, m_dmq1, 0, Length);
- index += Length;
- /*
- System.out.println("int: dmq1 len="+Length);
- for(int i=0; i<dmq1_array.Length; i++){
- System.out.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
- }
- System.out.println("");
- */
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_iqmp = new byte[Length];
- Array.Copy(plain, index, m_iqmp, 0, Length);
- index += Length;
- /*
- System.out.println("int: iqmp len="+Length);
- for(int i=0; i<iqmp_array.Length; i++){
- System.out.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
- }
- System.out.println("");
- */
- }
- catch
- {
- //System.out.println(e);
- return false;
- }
- return true;
- }
-
- bool Decrypt_dss()
- {
- try
- {
- byte[] plain;
- if (m_encrypted)
- {
- if (m_keytype == OPENSSH)
- {
- m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
- plain = new byte[m_encoded_data.Length];
- m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
- /*
- for(int i=0; i<plain.Length; i++){
- System.out.print(Integer.toHexString(plain[i]&0xff)+":");
- }
- System.out.println("");
- */
- }
- else if (m_keytype == FSECURE)
- {
- for (int i = 0; i < m_iv.Length; i++) m_iv[i] = 0;
- m_cipher.Init(Cipher.DECRYPT_MODE, m_key, m_iv);
- plain = new byte[m_encoded_data.Length];
- m_cipher.Update(m_encoded_data, 0, m_encoded_data.Length, plain, 0);
- }
- else
- {
- return false;
- }
- }
- else
- {
- if (m_P != null) return true;
- plain = m_encoded_data;
- }
-
- if (m_keytype == FSECURE)
- { // FSecure
- ByteBuffer buf = new ByteBuffer(plain);
- int foo = buf.GetInt();
- if (plain.Length != foo + 4)
- {
- return false;
- }
- m_P = buf.GetMPIntBits();
- m_G_array = buf.GetMPIntBits();
- m_Q = buf.GetMPIntBits();
- m_pub = buf.GetMPIntBits();
- m_prv = buf.GetMPIntBits();
- return true;
- }
-
- int index = 0;
- int Length = 0;
-
- if (plain[index] != 0x30) return false;
- index++; // SEQUENCE
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
-
- if (plain[index] != 0x02) return false;
- index++; // INTEGER
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- index += Length;
-
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_P = new byte[Length];
- Array.Copy(plain, index, m_P, 0, Length);
- index += Length;
-
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_Q = new byte[Length];
- Array.Copy(plain, index, m_Q, 0, Length);
- index += Length;
-
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_G_array = new byte[Length];
- Array.Copy(plain, index, m_G_array, 0, Length);
- index += Length;
-
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_pub = new byte[Length];
- Array.Copy(plain, index, m_pub, 0, Length);
- index += Length;
-
- index++;
- Length = plain[index++] & 0xff;
- if ((Length & 0x80) != 0)
- {
- int foo = Length & 0x7f; Length = 0;
- while (foo-- > 0) { Length = (Length << 8) + (plain[index++] & 0xff); }
- }
- m_prv = new byte[Length];
- Array.Copy(plain, index, m_prv, 0, Length);
- index += Length;
- }
- catch
- {
- //System.out.println(e);
- //e.printStackTrace();
- return false;
- }
- return true;
- }
-
- public bool isEncrypted { get { return m_encrypted; } }
-
- public string Name { get { return m_identity; } }
-
- private int WriteSEQUENCE(byte[] buf, int index, int len)
- {
- buf[index++] = 0x30;
- index = WriteLength(buf, index, len);
- return index;
- }
-
- private int WriteINTEGER(byte[] buf, int index, byte[] data)
- {
- buf[index++] = 0x02;
- index = WriteLength(buf, index, data.Length);
- Array.Copy(data, 0, buf, index, data.Length);
- index += data.Length;
- return index;
- }
-
- private int CountLength(int i_len)
- {
- uint len = (uint)i_len;
- int i = 1;
- if (len <= 0x7f) return i;
- while (len > 0)
- {
- len >>= 8;
- i++;
- }
- return i;
- }
-
- private int WriteLength(byte[] data, int index, int i_len)
- {
- int len = (int)i_len;
- int i = CountLength(len) - 1;
- if (i == 0)
- {
- data[index++] = (byte)len;
- return index;
- }
- data[index++] = (byte)(0x80 | i);
- int j = index + i;
- while (i > 0)
- {
- data[index + i - 1] = (byte)(len & 0xff);
- len >>= 8;
- i--;
- }
- return j;
- }
-
- private byte a2b(byte c)
- {
- if ('0' <= c && c <= '9')
- return (byte)(c - '0');
- if ('a' <= c && c <= 'z')
- return (byte)(c - 'a' + 10);
- return (byte)(c - 'A' + 10);
- }
-
- private byte b2a(byte c)
- {
- if (0 <= c && c <= 9)
- return (byte)(c + '0');
- return (byte)(c - 10 + 'A');
- }
- }
-
- }