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