KeyPairDSA.java
Go to the documentation of this file.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 KeyPairDSA extends KeyPair{
00039 private byte[] P_array;
00040 private byte[] Q_array;
00041 private byte[] G_array;
00042 private byte[] pub_array;
00043 private byte[] prv_array;
00044
00045
00046 private int key_size=1024;
00047
00048 public KeyPairDSA(JSch jsch){
00049 super(jsch);
00050 }
00051
00052 void generate(int key_size) throws JSchException{
00053 this.key_size=key_size;
00054 try{
00055 Class c=Class.forName(jsch.getConfig("keypairgen.dsa"));
00056 KeyPairGenDSA keypairgen=(KeyPairGenDSA)(c.newInstance());
00057 keypairgen.init(key_size);
00058 P_array=keypairgen.getP();
00059 Q_array=keypairgen.getQ();
00060 G_array=keypairgen.getG();
00061 pub_array=keypairgen.getY();
00062 prv_array=keypairgen.getX();
00063
00064 keypairgen=null;
00065 }
00066 catch(Exception e){
00067
00068 if(e instanceof Throwable)
00069 throw new JSchException(e.toString(), (Throwable)e);
00070 throw new JSchException(e.toString());
00071 }
00072 }
00073
00074 private static final byte[] begin=Util.str2byte("-----BEGIN DSA PRIVATE KEY-----");
00075 private static final byte[] end=Util.str2byte("-----END DSA PRIVATE KEY-----");
00076
00077 byte[] getBegin(){ return begin; }
00078 byte[] getEnd(){ return end; }
00079
00080 byte[] getPrivateKey(){
00081 int content=
00082 1+countLength(1) + 1 +
00083 1+countLength(P_array.length) + P_array.length +
00084 1+countLength(Q_array.length) + Q_array.length +
00085 1+countLength(G_array.length) + G_array.length +
00086 1+countLength(pub_array.length) + pub_array.length +
00087 1+countLength(prv_array.length) + prv_array.length;
00088
00089 int total=
00090 1+countLength(content)+content;
00091
00092 byte[] plain=new byte[total];
00093 int index=0;
00094 index=writeSEQUENCE(plain, index, content);
00095 index=writeINTEGER(plain, index, new byte[1]);
00096 index=writeINTEGER(plain, index, P_array);
00097 index=writeINTEGER(plain, index, Q_array);
00098 index=writeINTEGER(plain, index, G_array);
00099 index=writeINTEGER(plain, index, pub_array);
00100 index=writeINTEGER(plain, index, prv_array);
00101 return plain;
00102 }
00103
00104 boolean parse(byte[] plain){
00105 try{
00106
00107 if(vendor==VENDOR_FSECURE){
00108 if(plain[0]!=0x30){
00109 Buffer buf=new Buffer(plain);
00110 buf.getInt();
00111 P_array=buf.getMPIntBits();
00112 G_array=buf.getMPIntBits();
00113 Q_array=buf.getMPIntBits();
00114 pub_array=buf.getMPIntBits();
00115 prv_array=buf.getMPIntBits();
00116 return true;
00117 }
00118 return false;
00119 }
00120
00121 int index=0;
00122 int length=0;
00123
00124 if(plain[index]!=0x30)return false;
00125 index++;
00126 length=plain[index++]&0xff;
00127 if((length&0x80)!=0){
00128 int foo=length&0x7f; length=0;
00129 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00130 }
00131
00132 if(plain[index]!=0x02)return false;
00133 index++;
00134 length=plain[index++]&0xff;
00135 if((length&0x80)!=0){
00136 int foo=length&0x7f; length=0;
00137 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00138 }
00139 index+=length;
00140
00141 index++;
00142 length=plain[index++]&0xff;
00143 if((length&0x80)!=0){
00144 int foo=length&0x7f; length=0;
00145 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00146 }
00147 P_array=new byte[length];
00148 System.arraycopy(plain, index, P_array, 0, length);
00149 index+=length;
00150
00151 index++;
00152 length=plain[index++]&0xff;
00153 if((length&0x80)!=0){
00154 int foo=length&0x7f; length=0;
00155 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00156 }
00157 Q_array=new byte[length];
00158 System.arraycopy(plain, index, Q_array, 0, length);
00159 index+=length;
00160
00161 index++;
00162 length=plain[index++]&0xff;
00163 if((length&0x80)!=0){
00164 int foo=length&0x7f; length=0;
00165 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00166 }
00167 G_array=new byte[length];
00168 System.arraycopy(plain, index, G_array, 0, length);
00169 index+=length;
00170
00171 index++;
00172 length=plain[index++]&0xff;
00173 if((length&0x80)!=0){
00174 int foo=length&0x7f; length=0;
00175 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00176 }
00177 pub_array=new byte[length];
00178 System.arraycopy(plain, index, pub_array, 0, length);
00179 index+=length;
00180
00181 index++;
00182 length=plain[index++]&0xff;
00183 if((length&0x80)!=0){
00184 int foo=length&0x7f; length=0;
00185 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
00186 }
00187 prv_array=new byte[length];
00188 System.arraycopy(plain, index, prv_array, 0, length);
00189 index+=length;
00190 }
00191 catch(Exception e){
00192
00193
00194 return false;
00195 }
00196 return true;
00197 }
00198
00199 public byte[] getPublicKeyBlob(){
00200 byte[] foo=super.getPublicKeyBlob();
00201 if(foo!=null) return foo;
00202
00203 if(P_array==null) return null;
00204
00205 Buffer buf=new Buffer(sshdss.length+4+
00206 P_array.length+4+
00207 Q_array.length+4+
00208 G_array.length+4+
00209 pub_array.length+4);
00210 buf.putString(sshdss);
00211 buf.putString(P_array);
00212 buf.putString(Q_array);
00213 buf.putString(G_array);
00214 buf.putString(pub_array);
00215 return buf.buffer;
00216 }
00217
00218 private static final byte[] sshdss=Util.str2byte("ssh-dss");
00219 byte[] getKeyTypeName(){return sshdss;}
00220 public int getKeyType(){return DSA;}
00221
00222 public int getKeySize(){return key_size; }
00223 public void dispose(){
00224 super.dispose();
00225 Util.bzero(prv_array);
00226 }
00227 }