00001 using System;
00002
00003 namespace Tamir.SharpSsh.jsch
00004 {
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 public class KeyPairDSA : KeyPair
00035 {
00036 private byte[] P_array;
00037 private byte[] Q_array;
00038 private byte[] G_array;
00039 private byte[] pub_array;
00040 private byte[] prv_array;
00041
00042
00043 private int key_size=1024;
00044
00045 public KeyPairDSA(JSch jsch):base(jsch)
00046 {
00047 }
00048
00049 internal override void generate(int key_size)
00050 {
00051 this.key_size=key_size;
00052 try
00053 {
00054 Type t=Type.GetType(jsch.getConfig("keypairgen.dsa"));
00055 KeyPairGenDSA keypairgen=(KeyPairGenDSA)(Activator.CreateInstance(t));
00056 keypairgen.init(key_size);
00057 P_array=keypairgen.getP();
00058 Q_array=keypairgen.getQ();
00059 G_array=keypairgen.getG();
00060 pub_array=keypairgen.getY();
00061 prv_array=keypairgen.getX();
00062
00063 keypairgen=null;
00064 }
00065 catch(Exception e)
00066 {
00067 Console.Error.WriteLine("KeyPairDSA: "+e);
00068 throw new JSchException(e.ToString());
00069 }
00070 }
00071
00072 private static byte[] begin= Util.getBytes( "-----BEGIN DSA PRIVATE KEY-----" );
00073 private static byte[] end=Util.getBytes("-----END DSA PRIVATE KEY-----");
00074
00075 internal override byte[] getBegin(){ return begin; }
00076 internal override byte[] getEnd(){ return end; }
00077
00078 internal override byte[] getPrivateKey()
00079 {
00080 int content=
00081 1+countLength(1) + 1 +
00082 1+countLength(P_array.Length) + P_array.Length +
00083 1+countLength(Q_array.Length) + Q_array.Length +
00084 1+countLength(G_array.Length) + G_array.Length +
00085 1+countLength(pub_array.Length) + pub_array.Length +
00086 1+countLength(prv_array.Length) + prv_array.Length;
00087
00088 int total=
00089 1+countLength(content)+content;
00090
00091 byte[] plain=new byte[total];
00092 int index=0;
00093 index=writeSEQUENCE(plain, index, content);
00094 index=writeINTEGER(plain, index, new byte[1]);
00095 index=writeINTEGER(plain, index, P_array);
00096 index=writeINTEGER(plain, index, Q_array);
00097 index=writeINTEGER(plain, index, G_array);
00098 index=writeINTEGER(plain, index, pub_array);
00099 index=writeINTEGER(plain, index, prv_array);
00100 return plain;
00101 }
00102
00103 internal override bool parse(byte[] plain)
00104 {
00105 try
00106 {
00107
00108 if(vendor==VENDOR_FSECURE)
00109 {
00110 if(plain[0]!=0x30)
00111 {
00112 Buffer buf=new Buffer(plain);
00113 buf.getInt();
00114 P_array=buf.getMPIntBits();
00115 G_array=buf.getMPIntBits();
00116 Q_array=buf.getMPIntBits();
00117 pub_array=buf.getMPIntBits();
00118 prv_array=buf.getMPIntBits();
00119 return true;
00120 }
00121 return false;
00122 }
00123
00124 int index=0;
00125 int Length=0;
00126
00127 if(plain[index]!=0x30)return false;
00128 index++;
00129 Length=plain[index++]&0xff;
00130 if((Length&0x80)!=0)
00131 {
00132 int foo=Length&0x7f; Length=0;
00133 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00134 }
00135
00136 if(plain[index]!=0x02)return false;
00137 index++;
00138 Length=plain[index++]&0xff;
00139 if((Length&0x80)!=0)
00140 {
00141 int foo=Length&0x7f; Length=0;
00142 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00143 }
00144 index+=Length;
00145
00146 index++;
00147 Length=plain[index++]&0xff;
00148 if((Length&0x80)!=0)
00149 {
00150 int foo=Length&0x7f; Length=0;
00151 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00152 }
00153 P_array=new byte[Length];
00154 Array.Copy(plain, index, P_array, 0, Length);
00155 index+=Length;
00156
00157 index++;
00158 Length=plain[index++]&0xff;
00159 if((Length&0x80)!=0)
00160 {
00161 int foo=Length&0x7f; Length=0;
00162 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00163 }
00164 Q_array=new byte[Length];
00165 Array.Copy(plain, index, Q_array, 0, Length);
00166 index+=Length;
00167
00168 index++;
00169 Length=plain[index++]&0xff;
00170 if((Length&0x80)!=0)
00171 {
00172 int foo=Length&0x7f; Length=0;
00173 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00174 }
00175 G_array=new byte[Length];
00176 Array.Copy(plain, index, G_array, 0, Length);
00177 index+=Length;
00178
00179 index++;
00180 Length=plain[index++]&0xff;
00181 if((Length&0x80)!=0)
00182 {
00183 int foo=Length&0x7f; Length=0;
00184 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00185 }
00186 pub_array=new byte[Length];
00187 Array.Copy(plain, index, pub_array, 0, Length);
00188 index+=Length;
00189
00190 index++;
00191 Length=plain[index++]&0xff;
00192 if((Length&0x80)!=0)
00193 {
00194 int foo=Length&0x7f; Length=0;
00195 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00196 }
00197 prv_array=new byte[Length];
00198 Array.Copy(plain, index, prv_array, 0, Length);
00199 index+=Length;
00200 }
00201 catch
00202 {
00203
00204
00205 return false;
00206 }
00207 return true;
00208 }
00209
00210 public override byte[] getPublicKeyBlob()
00211 {
00212 byte[] foo=base.getPublicKeyBlob();
00213 if(foo!=null) return foo;
00214
00215 if(P_array==null) return null;
00216
00217 Buffer buf=new Buffer(sshdss.Length+4+
00218 P_array.Length+4+
00219 Q_array.Length+4+
00220 G_array.Length+4+
00221 pub_array.Length+4);
00222 buf.putString(sshdss);
00223 buf.putString(P_array);
00224 buf.putString(Q_array);
00225 buf.putString(G_array);
00226 buf.putString(pub_array);
00227 return buf.buffer;
00228 }
00229
00230 private static byte[] sshdss= Util.getBytes( "ssh-dss" );
00231 internal override byte[] getKeyTypeName(){return sshdss;}
00232 public override int getKeyType(){return DSA;}
00233
00234 public override int getKeySize(){return key_size; }
00235 public override void dispose()
00236 {
00237 base.dispose();
00238 P_array=null;
00239 Q_array=null;
00240 G_array=null;
00241 pub_array=null;
00242 prv_array=null;
00243 }
00244 }
00245
00246 }