1 2 29 30 package com.jcraft.jsch; 31 32 public class KeyPairDSA extends KeyPair{ 33 private byte[] P_array; 34 private byte[] Q_array; 35 private byte[] G_array; 36 private byte[] pub_array; 37 private byte[] prv_array; 38 39 private int key_size=1024; 41 42 public KeyPairDSA(JSch jsch){ 43 super(jsch); 44 } 45 46 void generate(int key_size) throws JSchException{ 47 this.key_size=key_size; 48 try{ 49 Class c=Class.forName(jsch.getConfig("keypairgen.dsa")); 50 KeyPairGenDSA keypairgen=(KeyPairGenDSA)(c.newInstance()); 51 keypairgen.init(key_size); 52 P_array=keypairgen.getP(); 53 Q_array=keypairgen.getQ(); 54 G_array=keypairgen.getG(); 55 pub_array=keypairgen.getY(); 56 prv_array=keypairgen.getX(); 57 58 keypairgen=null; 59 } 60 catch(Exception e){ 61 if(e instanceof Throwable ) 63 throw new JSchException(e.toString(), (Throwable )e); 64 throw new JSchException(e.toString()); 65 } 66 } 67 68 private static final byte[] begin="-----BEGIN DSA PRIVATE KEY-----".getBytes(); 69 private static final byte[] end="-----END DSA PRIVATE KEY-----".getBytes(); 70 71 byte[] getBegin(){ return begin; } 72 byte[] getEnd(){ return end; } 73 74 byte[] getPrivateKey(){ 75 int content= 76 1+countLength(1) + 1 + 1+countLength(P_array.length) + P_array.length + 1+countLength(Q_array.length) + Q_array.length + 1+countLength(G_array.length) + G_array.length + 1+countLength(pub_array.length) + pub_array.length + 1+countLength(prv_array.length) + prv_array.length; 83 int total= 84 1+countLength(content)+content; 86 byte[] plain=new byte[total]; 87 int index=0; 88 index=writeSEQUENCE(plain, index, content); 89 index=writeINTEGER(plain, index, new byte[1]); index=writeINTEGER(plain, index, P_array); 91 index=writeINTEGER(plain, index, Q_array); 92 index=writeINTEGER(plain, index, G_array); 93 index=writeINTEGER(plain, index, pub_array); 94 index=writeINTEGER(plain, index, prv_array); 95 return plain; 96 } 97 98 boolean parse(byte[] plain){ 99 try{ 100 101 if(vendor==VENDOR_FSECURE){ 102 if(plain[0]!=0x30){ Buffer buf=new Buffer(plain); 104 buf.getInt(); 105 P_array=buf.getMPIntBits(); 106 G_array=buf.getMPIntBits(); 107 Q_array=buf.getMPIntBits(); 108 pub_array=buf.getMPIntBits(); 109 prv_array=buf.getMPIntBits(); 110 return true; 111 } 112 return false; 113 } 114 115 int index=0; 116 int length=0; 117 118 if(plain[index]!=0x30)return false; 119 index++; length=plain[index++]&0xff; 121 if((length&0x80)!=0){ 122 int foo=length&0x7f; length=0; 123 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); } 124 } 125 126 if(plain[index]!=0x02)return false; 127 index++; length=plain[index++]&0xff; 129 if((length&0x80)!=0){ 130 int foo=length&0x7f; length=0; 131 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); } 132 } 133 index+=length; 134 135 index++; 136 length=plain[index++]&0xff; 137 if((length&0x80)!=0){ 138 int foo=length&0x7f; length=0; 139 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); } 140 } 141 P_array=new byte[length]; 142 System.arraycopy(plain, index, P_array, 0, length); 143 index+=length; 144 145 index++; 146 length=plain[index++]&0xff; 147 if((length&0x80)!=0){ 148 int foo=length&0x7f; length=0; 149 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); } 150 } 151 Q_array=new byte[length]; 152 System.arraycopy(plain, index, Q_array, 0, length); 153 index+=length; 154 155 index++; 156 length=plain[index++]&0xff; 157 if((length&0x80)!=0){ 158 int foo=length&0x7f; length=0; 159 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); } 160 } 161 G_array=new byte[length]; 162 System.arraycopy(plain, index, G_array, 0, length); 163 index+=length; 164 165 index++; 166 length=plain[index++]&0xff; 167 if((length&0x80)!=0){ 168 int foo=length&0x7f; length=0; 169 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); } 170 } 171 pub_array=new byte[length]; 172 System.arraycopy(plain, index, pub_array, 0, length); 173 index+=length; 174 175 index++; 176 length=plain[index++]&0xff; 177 if((length&0x80)!=0){ 178 int foo=length&0x7f; length=0; 179 while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); } 180 } 181 prv_array=new byte[length]; 182 System.arraycopy(plain, index, prv_array, 0, length); 183 index+=length; 184 } 185 catch(Exception e){ 186 return false; 189 } 190 return true; 191 } 192 193 public byte[] getPublicKeyBlob(){ 194 byte[] foo=super.getPublicKeyBlob(); 195 if(foo!=null) return foo; 196 197 if(P_array==null) return null; 198 199 Buffer buf=new Buffer(sshdss.length+4+ 200 P_array.length+4+ 201 Q_array.length+4+ 202 G_array.length+4+ 203 pub_array.length+4); 204 buf.putString(sshdss); 205 buf.putString(P_array); 206 buf.putString(Q_array); 207 buf.putString(G_array); 208 buf.putString(pub_array); 209 return buf.buffer; 210 } 211 212 private static final byte[] sshdss="ssh-dss".getBytes(); 213 byte[] getKeyTypeName(){return sshdss;} 214 public int getKeyType(){return DSA;} 215 216 public int getKeySize(){return key_size; } 217 public void dispose(){ 218 super.dispose(); 219 Util.bzero(prv_array); 220 } 221 } 222 | Popular Tags |