KeyPairDSA.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   //private int key_size=0;
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       //System.err.println("KeyPairDSA: "+e); 
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 +                           // INTEGER
00083       1+countLength(P_array.length) + P_array.length + // INTEGER  P
00084       1+countLength(Q_array.length) + Q_array.length + // INTEGER  Q
00085       1+countLength(G_array.length) + G_array.length + // INTEGER  G
00086       1+countLength(pub_array.length) + pub_array.length + // INTEGER  pub
00087       1+countLength(prv_array.length) + prv_array.length;  // INTEGER  prv
00088 
00089     int total=
00090       1+countLength(content)+content;   // SEQUENCE
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]);  // 0
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){              // FSecure
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++; // SEQUENCE
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++; // INTEGER
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       //System.err.println(e);
00193       //e.printStackTrace();
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 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1