00001 using System;
00002 using System.IO;
00003
00004 namespace Tamir.SharpSsh.jsch
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
00035 internal class IdentityFile : Identity
00036 {
00037 String identity;
00038 byte[] key;
00039 byte[] iv;
00040 private JSch jsch;
00041 private HASH hash;
00042 private byte[] encoded_data;
00043
00044 private Cipher cipher;
00045
00046
00047 private byte[] P_array;
00048 private byte[] Q_array;
00049 private byte[] G_array;
00050 private byte[] pub_array;
00051 private byte[] prv_array;
00052
00053
00054 private byte[] n_array;
00055 private byte[] e_array;
00056 private byte[] d_array;
00057
00058 private byte[] p_array;
00059 private byte[] q_array;
00060 private byte[] dmp1_array;
00061 private byte[] dmq1_array;
00062 private byte[] iqmp_array;
00063
00064
00065 private String algname="ssh-rsa";
00066
00067 private const int ERROR=0;
00068 private const int RSA=1;
00069 private const int DSS=2;
00070 internal const int UNKNOWN=3;
00071
00072 private const int OPENSSH=0;
00073 private const int FSECURE=1;
00074 private const int PUTTY=2;
00075
00076 private int type=ERROR;
00077 private int keytype=OPENSSH;
00078
00079 private byte[] publickeyblob=null;
00080
00081 private bool encrypted=true;
00082
00083 internal IdentityFile(String identity, JSch jsch)
00084 {
00085 this.identity=identity;
00086 this.jsch=jsch;
00087 try
00088 {
00089 Type c=Type.GetType(jsch.getConfig("3des-cbc"));
00090 cipher=(Cipher)Activator.CreateInstance(c);
00091 key=new byte[cipher.getBlockSize()];
00092 iv=new byte[cipher.getIVSize()];
00093 c=Type.GetType(jsch.getConfig("md5"));
00094 hash=(HASH)(Activator.CreateInstance(c));
00095 hash.init();
00096 FileInfo file=new FileInfo(identity);
00097 FileStream fis = File.OpenRead(identity);
00098 byte[] buf=new byte[(int)(file.Length)];
00099 int len=fis.Read(buf, 0, buf.Length);
00100 fis.Close();
00101
00102 int i=0;
00103 while(i<len)
00104 {
00105 if(buf[i]=='B'&& buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I')
00106 {
00107 i+=6;
00108 if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSS; }
00109 else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
00110 else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H')
00111 {
00112 type=UNKNOWN;
00113 keytype=FSECURE;
00114 }
00115 else
00116 {
00117
00118 throw new JSchException("invaid privatekey: "+identity);
00119 }
00120 i+=3;
00121 continue;
00122 }
00123 if(buf[i]=='C'&& buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==',')
00124 {
00125 i+=4;
00126 for(int ii=0; ii<iv.Length; ii++)
00127 {
00128 iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+
00129 (a2b(buf[i++])&0xf));
00130 }
00131 continue;
00132 }
00133 if(buf[i]==0x0d &&
00134 i+1<buf.Length && buf[i+1]==0x0a)
00135 {
00136 i++;
00137 continue;
00138 }
00139 if(buf[i]==0x0a && i+1<buf.Length)
00140 {
00141 if(buf[i+1]==0x0a){ i+=2; break; }
00142 if(buf[i+1]==0x0d &&
00143 i+2<buf.Length && buf[i+2]==0x0a)
00144 {
00145 i+=3; break;
00146 }
00147 bool inheader=false;
00148 for(int j=i+1; j<buf.Length; j++)
00149 {
00150 if(buf[j]==0x0a) break;
00151
00152 if(buf[j]==':'){inheader=true; break;}
00153 }
00154 if(!inheader)
00155 {
00156 i++;
00157 encrypted=false;
00158 break;
00159 }
00160 }
00161 i++;
00162 }
00163
00164 if(type==ERROR)
00165 {
00166 throw new JSchException("invaid privatekey: "+identity);
00167 }
00168
00169 int start=i;
00170 while(i<len)
00171 {
00172 if(buf[i]==0x0a)
00173 {
00174 bool xd=(buf[i-1]==0x0d);
00175 Array.Copy(buf, i+1,
00176 buf,
00177 i-(xd ? 1 : 0),
00178 len-i-1-(xd ? 1 : 0)
00179 );
00180 if(xd)len--;
00181 len--;
00182 continue;
00183 }
00184 if(buf[i]=='-'){ break; }
00185 i++;
00186 }
00187 encoded_data=Util.fromBase64(buf, start, i-start);
00188
00189 if(encoded_data.Length>4 &&
00190 encoded_data[0]==(byte)0x3f &&
00191 encoded_data[1]==(byte)0x6f &&
00192 encoded_data[2]==(byte)0xf9 &&
00193 encoded_data[3]==(byte)0xeb)
00194 {
00195
00196 Buffer _buf=new Buffer(encoded_data);
00197 _buf.getInt();
00198 _buf.getInt();
00199 byte[]_type=_buf.getString();
00200
00201 byte[] _cipher=_buf.getString();
00202 String s_cipher=System.Text.Encoding.Default.GetString(_cipher);
00203
00204 if(s_cipher.Equals("3des-cbc"))
00205 {
00206 _buf.getInt();
00207 byte[] foo=new byte[encoded_data.Length-_buf.getOffSet()];
00208 _buf.getByte(foo);
00209 encoded_data=foo;
00210 encrypted=true;
00211 throw new JSchException("unknown privatekey format: "+identity);
00212 }
00213 else if(s_cipher.Equals("none"))
00214 {
00215 _buf.getInt();
00216
00217
00218 encrypted=false;
00219
00220 byte[] foo=new byte[encoded_data.Length-_buf.getOffSet()];
00221 _buf.getByte(foo);
00222 encoded_data=foo;
00223 }
00224
00225 }
00226
00227 try
00228 {
00229 file=new FileInfo(identity+".pub");
00230 fis=File.OpenRead(identity+".pub");
00231 buf=new byte[(int)(file.Length)];
00232 len=fis.Read(buf, 0, buf.Length);
00233 fis.Close();
00234 }
00235 catch
00236 {
00237 return;
00238 }
00239
00240 if(buf.Length>4 &&
00241 buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-')
00242 {
00243
00244 i=0;
00245 do{i++;}while(buf.Length>i && buf[i]!=0x0a);
00246 if(buf.Length<=i) return;
00247
00248 while(true)
00249 {
00250 if(buf[i]==0x0a)
00251 {
00252 bool inheader=false;
00253 for(int j=i+1; j<buf.Length; j++)
00254 {
00255 if(buf[j]==0x0a) break;
00256 if(buf[j]==':'){inheader=true; break;}
00257 }
00258 if(!inheader)
00259 {
00260 i++;
00261 break;
00262 }
00263 }
00264 i++;
00265 }
00266 if(buf.Length<=i) return;
00267
00268 start=i;
00269 while(i<len)
00270 {
00271 if(buf[i]==0x0a)
00272 {
00273 Array.Copy(buf, i+1, buf, i, len-i-1);
00274 len--;
00275 continue;
00276 }
00277 if(buf[i]=='-'){ break; }
00278 i++;
00279 }
00280 publickeyblob=Util.fromBase64(buf, start, i-start);
00281
00282 if(type==UNKNOWN)
00283 {
00284 if(publickeyblob[8]=='d')
00285 {
00286 type=DSS;
00287 }
00288 else if(publickeyblob[8]=='r')
00289 {
00290 type=RSA;
00291 }
00292 }
00293 }
00294 else
00295 {
00296 if(buf[0]!='s'|| buf[1]!='s'|| buf[2]!='h'|| buf[3]!='-') return;
00297 i=0;
00298 while(i<len){ if(buf[i]==' ')break; i++;} i++;
00299 if(i>=len) return;
00300 start=i;
00301 while(i<len){ if(buf[i]==' ')break; i++;}
00302 publickeyblob=Util.fromBase64(buf, start, i-start);
00303 }
00304
00305 }
00306 catch(Exception e)
00307 {
00308 Console.WriteLine("Identity: "+e);
00309 if(e is JSchException) throw (JSchException)e;
00310 throw new JSchException(e.ToString());
00311 }
00312
00313 }
00314
00315 public String getAlgName()
00316 {
00317 if(type==RSA) return "ssh-rsa";
00318 return "ssh-dss";
00319 }
00320
00321 public bool setPassphrase(String _passphrase)
00322 {
00323
00324
00325
00326
00327
00328
00329 try
00330 {
00331 if(encrypted)
00332 {
00333 if(_passphrase==null) return false;
00334 byte[] passphrase= System.Text.Encoding.Default.GetBytes( _passphrase );
00335 int hsize=hash.getBlockSize();
00336 byte[] hn=new byte[key.Length/hsize*hsize+
00337 (key.Length%hsize==0?0:hsize)];
00338 byte[] tmp=null;
00339 if(keytype==OPENSSH)
00340 {
00341 for(int index=0; index+hsize<=hn.Length;)
00342 {
00343 if(tmp!=null){ hash.update(tmp, 0, tmp.Length); }
00344 hash.update(passphrase, 0, passphrase.Length);
00345 hash.update(iv, 0, iv.Length);
00346 tmp=hash.digest();
00347 Array.Copy(tmp, 0, hn, index, tmp.Length);
00348 index+=tmp.Length;
00349 }
00350 Array.Copy(hn, 0, key, 0, key.Length);
00351 }
00352 else if(keytype==FSECURE)
00353 {
00354 for(int index=0; index+hsize<=hn.Length;)
00355 {
00356 if(tmp!=null){ hash.update(tmp, 0, tmp.Length); }
00357 hash.update(passphrase, 0, passphrase.Length);
00358 tmp=hash.digest();
00359 Array.Copy(tmp, 0, hn, index, tmp.Length);
00360 index+=tmp.Length;
00361 }
00362 Array.Copy(hn, 0, key, 0, key.Length);
00363 }
00364 }
00365 if(decrypt())
00366 {
00367 encrypted=false;
00368 return true;
00369 }
00370 P_array=Q_array=G_array=pub_array=prv_array=null;
00371 return false;
00372 }
00373 catch(Exception e)
00374 {
00375 if(e is JSchException) throw (JSchException)e;
00376 throw new JSchException(e.ToString());
00377 }
00378 }
00379
00380 public byte[] getPublicKeyBlob()
00381 {
00382 if(publickeyblob!=null) return publickeyblob;
00383 if(type==RSA) return getPublicKeyBlob_rsa();
00384 return getPublicKeyBlob_dss();
00385 }
00386
00387 byte[] getPublicKeyBlob_rsa()
00388 {
00389 if(e_array==null) return null;
00390 Buffer buf=new Buffer("ssh-rsa".Length+4+
00391 e_array.Length+4+
00392 n_array.Length+4);
00393 buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-rsa" ) );
00394 buf.putString(e_array);
00395 buf.putString(n_array);
00396 return buf.buffer;
00397 }
00398
00399 byte[] getPublicKeyBlob_dss()
00400 {
00401 if(P_array==null) return null;
00402 Buffer buf=new Buffer("ssh-dss".Length+4+
00403 P_array.Length+4+
00404 Q_array.Length+4+
00405 G_array.Length+4+
00406 pub_array.Length+4);
00407 buf.putString(System.Text.Encoding.Default.GetBytes("ssh-dss"));
00408 buf.putString(P_array);
00409 buf.putString(Q_array);
00410 buf.putString(G_array);
00411 buf.putString(pub_array);
00412 return buf.buffer;
00413 }
00414
00415 public byte[] getSignature(Session session, byte[] data)
00416 {
00417 if(type==RSA) return getSignature_rsa(session, data);
00418 return getSignature_dss(session, data);
00419 }
00420
00421 byte[] getSignature_rsa(Session session, byte[] data)
00422 {
00423 try
00424 {
00425 Type t=Type.GetType(jsch.getConfig("signature.rsa"));
00426 SignatureRSA rsa=(SignatureRSA)Activator.CreateInstance(t);
00427
00428 rsa.init();
00429 rsa.setPrvKey(e_array, n_array, d_array, p_array, q_array, dmp1_array, dmq1_array, iqmp_array);
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 rsa.update(data);
00441 byte[] sig = rsa.sign();
00442 Buffer buf=new Buffer("ssh-rsa".Length+4+
00443 sig.Length+4);
00444 buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-rsa" ));
00445 buf.putString(sig);
00446 return buf.buffer;
00447 }
00448 catch(Exception e)
00449 {
00450 Console.WriteLine(e);
00451 }
00452 return null;
00453 }
00454
00455 byte[] getSignature_dss(Session session, byte[] data)
00456 {
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 try
00481 {
00482 Type t=Type.GetType(jsch.getConfig("signature.dss"));
00483 SignatureDSA dsa=(SignatureDSA)(Activator.CreateInstance(t));
00484 dsa.init();
00485 dsa.setPrvKey(prv_array, P_array, Q_array, G_array);
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 dsa.update(data);
00497 byte[] sig = dsa.sign();
00498 Buffer buf=new Buffer("ssh-dss".Length+4+
00499 sig.Length+4);
00500 buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-dss" ) );
00501 buf.putString(sig);
00502 return buf.buffer;
00503 }
00504 catch(Exception e)
00505 {
00506 Console.WriteLine("e "+e);
00507 }
00508 return null;
00509 }
00510
00511 public bool decrypt()
00512 {
00513 if(type==RSA) return decrypt_rsa();
00514 return decrypt_dss();
00515 }
00516
00517 bool decrypt_rsa()
00518 {
00519
00520
00521
00522
00523
00524
00525 try
00526 {
00527 byte[] plain;
00528 if(encrypted)
00529 {
00530 if(keytype==OPENSSH)
00531 {
00532 cipher.init(Cipher.DECRYPT_MODE, key, iv);
00533 plain=new byte[encoded_data.Length];
00534 cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
00535 }
00536 else if(keytype==FSECURE)
00537 {
00538 for(int i=0; i<iv.Length; i++)iv[i]=0;
00539 cipher.init(Cipher.DECRYPT_MODE, key, iv);
00540 plain=new byte[encoded_data.Length];
00541 cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
00542 }
00543 else
00544 {
00545 return false;
00546 }
00547 }
00548 else
00549 {
00550 if(n_array!=null) return true;
00551 plain=encoded_data;
00552 }
00553
00554 if(keytype==FSECURE)
00555 {
00556 Buffer buf=new Buffer(plain);
00557 int foo=buf.getInt();
00558 if(plain.Length!=foo+4)
00559 {
00560 return false;
00561 }
00562 e_array=buf.getMPIntBits();
00563 d_array=buf.getMPIntBits();
00564 n_array=buf.getMPIntBits();
00565 byte[] u_array=buf.getMPIntBits();
00566 p_array=buf.getMPIntBits();
00567 q_array=buf.getMPIntBits();
00568 return true;
00569 }
00570
00571 int index=0;
00572 int Length=0;
00573
00574 if(plain[index]!=0x30)return false;
00575 index++;
00576 Length=plain[index++]&0xff;
00577 if((Length&0x80)!=0)
00578 {
00579 int foo=Length&0x7f; Length=0;
00580 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00581 }
00582
00583 if(plain[index]!=0x02)return false;
00584 index++;
00585 Length=plain[index++]&0xff;
00586 if((Length&0x80)!=0)
00587 {
00588 int foo=Length&0x7f; Length=0;
00589 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00590 }
00591 index+=Length;
00592
00593
00594
00595
00596
00597 index++;
00598 Length=plain[index++]&0xff;
00599 if((Length&0x80)!=0)
00600 {
00601 int foo=Length&0x7f; Length=0;
00602 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00603 }
00604 n_array=new byte[Length];
00605 Array.Copy(plain, index, n_array, 0, Length);
00606 index+=Length;
00607
00608
00609
00610
00611
00612
00613
00614 index++;
00615 Length=plain[index++]&0xff;
00616 if((Length&0x80)!=0)
00617 {
00618 int foo=Length&0x7f; Length=0;
00619 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00620 }
00621 e_array=new byte[Length];
00622 Array.Copy(plain, index, e_array, 0, Length);
00623 index+=Length;
00624
00625
00626
00627
00628
00629
00630
00631 index++;
00632 Length=plain[index++]&0xff;
00633 if((Length&0x80)!=0)
00634 {
00635 int foo=Length&0x7f; Length=0;
00636 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00637 }
00638 d_array=new byte[Length];
00639 Array.Copy(plain, index, d_array, 0, Length);
00640 index+=Length;
00641
00642
00643
00644
00645
00646
00647
00648
00649 index++;
00650 Length=plain[index++]&0xff;
00651 if((Length&0x80)!=0)
00652 {
00653 int foo=Length&0x7f; Length=0;
00654 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00655 }
00656 p_array=new byte[Length];
00657 Array.Copy(plain, index, p_array, 0, Length);
00658 index+=Length;
00659
00660
00661
00662
00663
00664
00665
00666 index++;
00667 Length=plain[index++]&0xff;
00668 if((Length&0x80)!=0)
00669 {
00670 int foo=Length&0x7f; Length=0;
00671 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00672 }
00673 q_array=new byte[Length];
00674 Array.Copy(plain, index, q_array, 0, Length);
00675 index+=Length;
00676
00677
00678
00679
00680
00681
00682
00683 index++;
00684 Length=plain[index++]&0xff;
00685 if((Length&0x80)!=0)
00686 {
00687 int foo=Length&0x7f; Length=0;
00688 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00689 }
00690 dmp1_array=new byte[Length];
00691 Array.Copy(plain, index, dmp1_array, 0, Length);
00692 index+=Length;
00693
00694
00695
00696
00697
00698
00699
00700 index++;
00701 Length=plain[index++]&0xff;
00702 if((Length&0x80)!=0)
00703 {
00704 int foo=Length&0x7f; Length=0;
00705 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00706 }
00707 dmq1_array=new byte[Length];
00708 Array.Copy(plain, index, dmq1_array, 0, Length);
00709 index+=Length;
00710
00711
00712
00713
00714
00715
00716
00717 index++;
00718 Length=plain[index++]&0xff;
00719 if((Length&0x80)!=0)
00720 {
00721 int foo=Length&0x7f; Length=0;
00722 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00723 }
00724 iqmp_array=new byte[Length];
00725 Array.Copy(plain, index, iqmp_array, 0, Length);
00726 index+=Length;
00727
00728
00729
00730
00731
00732
00733
00734 }
00735 catch
00736 {
00737
00738 return false;
00739 }
00740 return true;
00741 }
00742
00743 bool decrypt_dss()
00744 {
00745 try
00746 {
00747 byte[] plain;
00748 if(encrypted)
00749 {
00750 if(keytype==OPENSSH)
00751 {
00752 cipher.init(Cipher.DECRYPT_MODE, key, iv);
00753 plain=new byte[encoded_data.Length];
00754 cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
00755
00756
00757
00758
00759
00760
00761 }
00762 else if(keytype==FSECURE)
00763 {
00764 for(int i=0; i<iv.Length; i++)iv[i]=0;
00765 cipher.init(Cipher.DECRYPT_MODE, key, iv);
00766 plain=new byte[encoded_data.Length];
00767 cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
00768 }
00769 else
00770 {
00771 return false;
00772 }
00773 }
00774 else
00775 {
00776 if(P_array!=null) return true;
00777 plain=encoded_data;
00778 }
00779
00780 if(keytype==FSECURE)
00781 {
00782 Buffer buf=new Buffer(plain);
00783 int foo=buf.getInt();
00784 if(plain.Length!=foo+4)
00785 {
00786 return false;
00787 }
00788 P_array=buf.getMPIntBits();
00789 G_array=buf.getMPIntBits();
00790 Q_array=buf.getMPIntBits();
00791 pub_array=buf.getMPIntBits();
00792 prv_array=buf.getMPIntBits();
00793 return true;
00794 }
00795
00796 int index=0;
00797 int Length=0;
00798
00799 if(plain[index]!=0x30)return false;
00800 index++;
00801 Length=plain[index++]&0xff;
00802 if((Length&0x80)!=0)
00803 {
00804 int foo=Length&0x7f; Length=0;
00805 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00806 }
00807
00808 if(plain[index]!=0x02)return false;
00809 index++;
00810 Length=plain[index++]&0xff;
00811 if((Length&0x80)!=0)
00812 {
00813 int foo=Length&0x7f; Length=0;
00814 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00815 }
00816 index+=Length;
00817
00818 index++;
00819 Length=plain[index++]&0xff;
00820 if((Length&0x80)!=0)
00821 {
00822 int foo=Length&0x7f; Length=0;
00823 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00824 }
00825 P_array=new byte[Length];
00826 Array.Copy(plain, index, P_array, 0, Length);
00827 index+=Length;
00828
00829 index++;
00830 Length=plain[index++]&0xff;
00831 if((Length&0x80)!=0)
00832 {
00833 int foo=Length&0x7f; Length=0;
00834 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00835 }
00836 Q_array=new byte[Length];
00837 Array.Copy(plain, index, Q_array, 0, Length);
00838 index+=Length;
00839
00840 index++;
00841 Length=plain[index++]&0xff;
00842 if((Length&0x80)!=0)
00843 {
00844 int foo=Length&0x7f; Length=0;
00845 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00846 }
00847 G_array=new byte[Length];
00848 Array.Copy(plain, index, G_array, 0, Length);
00849 index+=Length;
00850
00851 index++;
00852 Length=plain[index++]&0xff;
00853 if((Length&0x80)!=0)
00854 {
00855 int foo=Length&0x7f; Length=0;
00856 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00857 }
00858 pub_array=new byte[Length];
00859 Array.Copy(plain, index, pub_array, 0, Length);
00860 index+=Length;
00861
00862 index++;
00863 Length=plain[index++]&0xff;
00864 if((Length&0x80)!=0)
00865 {
00866 int foo=Length&0x7f; Length=0;
00867 while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
00868 }
00869 prv_array=new byte[Length];
00870 Array.Copy(plain, index, prv_array, 0, Length);
00871 index+=Length;
00872 }
00873 catch
00874 {
00875
00876
00877 return false;
00878 }
00879 return true;
00880 }
00881
00882 public bool isEncrypted()
00883 {
00884 return encrypted;
00885 }
00886 public String getName(){return identity;}
00887
00888 private int writeSEQUENCE(byte[] buf, int index, int len)
00889 {
00890 buf[index++]=0x30;
00891 index=writeLength(buf, index, len);
00892 return index;
00893 }
00894 private int writeINTEGER(byte[] buf, int index, byte[] data)
00895 {
00896 buf[index++]=0x02;
00897 index=writeLength(buf, index, data.Length);
00898 Array.Copy(data, 0, buf, index, data.Length);
00899 index+=data.Length;
00900 return index;
00901 }
00902
00903 private int countLength(int i_len)
00904 {
00905 uint len = (uint)i_len;
00906 int i=1;
00907 if(len<=0x7f) return i;
00908 while(len>0)
00909 {
00910 len>>=8;
00911 i++;
00912 }
00913 return i;
00914 }
00915
00916 private int writeLength(byte[] data, int index, int i_len)
00917 {
00918 int len = (int)i_len;
00919 int i=countLength(len)-1;
00920 if(i==0)
00921 {
00922 data[index++]=(byte)len;
00923 return index;
00924 }
00925 data[index++]=(byte)(0x80|i);
00926 int j=index+i;
00927 while(i>0)
00928 {
00929 data[index+i-1]=(byte)(len&0xff);
00930 len>>=8;
00931 i--;
00932 }
00933 return j;
00934 }
00935
00936 private byte a2b(byte c)
00937 {
00938 if('0'<=c&&c<='9') return (byte)(c-'0');
00939 if('a'<=c&&c<='z') return (byte)(c-'a'+10);
00940 return (byte)(c-'A'+10);
00941 }
00942 private byte b2a(byte c)
00943 {
00944 if(0<=c&&c<=9) return (byte)(c+'0');
00945 return (byte)(c-10+'A');
00946 }
00947 }
00948
00949 }