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 KeyPairRSA : KeyPair
00035 {
00036 private byte[] prv_array;
00037 private byte[] pub_array;
00038 private byte[] n_array;
00039
00040 private byte[] p_array;
00041 private byte[] q_array;
00042 private byte[] ep_array;
00043 private byte[] eq_array;
00044 private byte[] c_array;
00045
00046
00047 private int key_size=1024;
00048
00049 public KeyPairRSA(JSch jsch):base(jsch)
00050 {
00051 }
00052
00053 internal override void generate(int key_size)
00054 {
00055 this.key_size=key_size;
00056 try
00057 {
00058 Type t=Type.GetType(jsch.getConfig("keypairgen.rsa"));
00059 KeyPairGenRSA keypairgen=(KeyPairGenRSA)(Activator.CreateInstance(t));
00060 keypairgen.init(key_size);
00061 pub_array=keypairgen.getE();
00062 prv_array=keypairgen.getD();
00063 n_array=keypairgen.getN();
00064
00065 p_array=keypairgen.getP();
00066 q_array=keypairgen.getQ();
00067 ep_array=keypairgen.getEP();
00068 eq_array=keypairgen.getEQ();
00069 c_array=keypairgen.getC();
00070
00071 keypairgen=null;
00072 }
00073 catch(Exception e)
00074 {
00075 Console.WriteLine("KeyPairRSA: "+e);
00076 throw new JSchException(e.ToString());
00077 }
00078 }
00079
00080 private static byte[] begin= Util.getBytes( "-----BEGIN RSA PRIVATE KEY-----");
00081 private static byte[] end=Util.getBytes("-----END RSA PRIVATE KEY-----");
00082
00083 internal override byte[] getBegin(){ return begin; }
00084 internal override byte[] getEnd(){ return end; }
00085
00086 internal override byte[] getPrivateKey()
00087 {
00088 int content=
00089 1+countLength(1) + 1 +
00090 1+countLength(n_array.Length) + n_array.Length +
00091 1+countLength(pub_array.Length) + pub_array.Length +
00092 1+countLength(prv_array.Length) + prv_array.Length+
00093 1+countLength(p_array.Length) + p_array.Length+
00094 1+countLength(q_array.Length) + q_array.Length+
00095 1+countLength(ep_array.Length) + ep_array.Length+
00096 1+countLength(eq_array.Length) + eq_array.Length+
00097 1+countLength(c_array.Length) + c_array.Length;
00098
00099 int total=
00100 1+countLength(content)+content;
00101
00102 byte[] plain=new byte[total];
00103 int index=0;
00104 index=writeSEQUENCE(plain, index, content);
00105 index=writeINTEGER(plain, index, new byte[1]);
00106 index=writeINTEGER(plain, index, n_array);
00107 index=writeINTEGER(plain, index, pub_array);
00108 index=writeINTEGER(plain, index, prv_array);
00109 index=writeINTEGER(plain, index, p_array);
00110 index=writeINTEGER(plain, index, q_array);
00111 index=writeINTEGER(plain, index, ep_array);
00112 index=writeINTEGER(plain, index, eq_array);
00113 index=writeINTEGER(plain, index, c_array);
00114 return plain;
00115 }
00116
00117 internal override bool parse(byte [] plain)
00118 {
00119
00120
00121
00122
00123
00124
00125
00126 try
00127 {
00128 int index=0;
00129 int Length=0;
00130
00131 if(vendor==VENDOR_FSECURE)
00132 {
00133 if(plain[index]!=0x30)
00134 {
00135 Buffer buf=new Buffer(plain);
00136 pub_array=buf.getMPIntBits();
00137 prv_array=buf.getMPIntBits();
00138 n_array=buf.getMPIntBits();
00139 byte[] u_array=buf.getMPIntBits();
00140 p_array=buf.getMPIntBits();
00141 q_array=buf.getMPIntBits();
00142 return true;
00143 }
00144 return false;
00145 }
00146
00147 index++;
00148 Length=plain[index++]&0xff;
00149 if((Length&0x80)!=0)
00150 {
00151 int foo=Length&0x7f; Length=0;
00152 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00153 }
00154
00155 if(plain[index]!=0x02)return false;
00156 index++;
00157 Length=plain[index++]&0xff;
00158 if((Length&0x80)!=0)
00159 {
00160 int foo=Length&0x7f; Length=0;
00161 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00162 }
00163 index+=Length;
00164
00165
00166
00167
00168
00169 index++;
00170 Length=plain[index++]&0xff;
00171 if((Length&0x80)!=0)
00172 {
00173 int foo=Length&0x7f; Length=0;
00174 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00175 }
00176 n_array=new byte[Length];
00177 Array.Copy(plain, index, n_array, 0, Length);
00178 index+=Length;
00179
00180
00181
00182
00183
00184
00185
00186 index++;
00187 Length=plain[index++]&0xff;
00188 if((Length&0x80)!=0)
00189 {
00190 int foo=Length&0x7f; Length=0;
00191 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00192 }
00193 pub_array=new byte[Length];
00194 Array.Copy(plain, index, pub_array, 0, Length);
00195 index+=Length;
00196
00197
00198
00199
00200
00201
00202
00203 index++;
00204 Length=plain[index++]&0xff;
00205 if((Length&0x80)!=0)
00206 {
00207 int foo=Length&0x7f; Length=0;
00208 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00209 }
00210 prv_array=new byte[Length];
00211 Array.Copy(plain, index, prv_array, 0, Length);
00212 index+=Length;
00213
00214
00215
00216
00217
00218
00219
00220
00221 index++;
00222 Length=plain[index++]&0xff;
00223 if((Length&0x80)!=0)
00224 {
00225 int foo=Length&0x7f; Length=0;
00226 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00227 }
00228 p_array=new byte[Length];
00229 Array.Copy(plain, index, p_array, 0, Length);
00230 index+=Length;
00231
00232
00233
00234
00235
00236
00237
00238 index++;
00239 Length=plain[index++]&0xff;
00240 if((Length&0x80)!=0)
00241 {
00242 int foo=Length&0x7f; Length=0;
00243 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00244 }
00245 q_array=new byte[Length];
00246 Array.Copy(plain, index, q_array, 0, Length);
00247 index+=Length;
00248
00249
00250
00251
00252
00253
00254
00255 index++;
00256 Length=plain[index++]&0xff;
00257 if((Length&0x80)!=0)
00258 {
00259 int foo=Length&0x7f; Length=0;
00260 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00261 }
00262 ep_array=new byte[Length];
00263 Array.Copy(plain, index, ep_array, 0, Length);
00264 index+=Length;
00265
00266
00267
00268
00269
00270
00271
00272 index++;
00273 Length=plain[index++]&0xff;
00274 if((Length&0x80)!=0)
00275 {
00276 int foo=Length&0x7f; Length=0;
00277 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00278 }
00279 eq_array=new byte[Length];
00280 Array.Copy(plain, index, eq_array, 0, Length);
00281 index+=Length;
00282
00283
00284
00285
00286
00287
00288
00289 index++;
00290 Length=plain[index++]&0xff;
00291 if((Length&0x80)!=0)
00292 {
00293 int foo=Length&0x7f; Length=0;
00294 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00295 }
00296 c_array=new byte[Length];
00297 Array.Copy(plain, index, c_array, 0, Length);
00298 index+=Length;
00299
00300
00301
00302
00303
00304
00305
00306 }
00307 catch
00308 {
00309
00310 return false;
00311 }
00312 return true;
00313 }
00314
00315
00316 public override byte[] getPublicKeyBlob()
00317 {
00318 byte[] foo=base.getPublicKeyBlob();
00319 if(foo!=null) return foo;
00320
00321 if(pub_array==null) return null;
00322
00323 Buffer buf=new Buffer(sshrsa.Length+4+
00324 pub_array.Length+4+
00325 n_array.Length+4);
00326 buf.putString(sshrsa);
00327 buf.putString(pub_array);
00328 buf.putString(n_array);
00329 return buf.buffer;
00330 }
00331
00332 private static byte[] sshrsa= Util.getBytes( "ssh-rsa" );
00333 internal override byte[] getKeyTypeName(){return sshrsa;}
00334 public override int getKeyType(){return RSA;}
00335
00336 public override int getKeySize(){return key_size; }
00337 public override void dispose()
00338 {
00339 base.dispose();
00340 pub_array=null;
00341 prv_array=null;
00342 n_array=null;
00343
00344 p_array=null;
00345 q_array=null;
00346 ep_array=null;
00347 eq_array=null;
00348 c_array=null;
00349 }
00350 }
00351
00352 }