KeyPairRSA.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 KeyPairRSA extends KeyPair{
00039   private byte[] prv_array;
00040   private byte[] pub_array;
00041   private byte[] n_array;
00042 
00043   private byte[] p_array;  // prime p
00044   private byte[] q_array;  // prime q
00045   private byte[] ep_array; // prime exponent p
00046   private byte[] eq_array; // prime exponent q
00047   private byte[] c_array;  // coefficient
00048 
00049   //private int key_size=0;
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       //System.err.println("KeyPairRSA: "+e); 
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 +                           // INTEGER
00091       1+countLength(n_array.length) + n_array.length + // INTEGER  N
00092       1+countLength(pub_array.length) + pub_array.length + // INTEGER  pub
00093       1+countLength(prv_array.length) + prv_array.length+  // INTEGER  prv
00094       1+countLength(p_array.length) + p_array.length+      // INTEGER  p
00095       1+countLength(q_array.length) + q_array.length+      // INTEGER  q
00096       1+countLength(ep_array.length) + ep_array.length+    // INTEGER  ep
00097       1+countLength(eq_array.length) + eq_array.length+    // INTEGER  eq
00098       1+countLength(c_array.length) + c_array.length;      // INTEGER  c
00099 
00100     int total=
00101       1+countLength(content)+content;   // SEQUENCE
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]);  // 0
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     byte[] p_array;
00121     byte[] q_array;
00122     byte[] dmp1_array;
00123     byte[] dmq1_array;
00124     byte[] iqmp_array;
00125     */
00126     try{
00127       int index=0;
00128       int length=0;
00129 
00130       if(vendor==VENDOR_FSECURE){
00131     if(plain[index]!=0x30){                  // FSecure
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++; // SEQUENCE
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++; // INTEGER
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 //System.err.println("int: len="+length);
00161 //System.err.print(Integer.toHexString(plain[index-1]&0xff)+":");
00162 //System.err.println("");
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 System.err.println("int: N len="+length);
00175 for(int i=0; i<n_array.length; i++){
00176 System.err.print(Integer.toHexString(n_array[i]&0xff)+":");
00177 }
00178 System.err.println("");
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 System.err.println("int: E len="+length);
00191 for(int i=0; i<pub_array.length; i++){
00192 System.err.print(Integer.toHexString(pub_array[i]&0xff)+":");
00193 }
00194 System.err.println("");
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 System.err.println("int: prv len="+length);
00207 for(int i=0; i<prv_array.length; i++){
00208 System.err.print(Integer.toHexString(prv_array[i]&0xff)+":");
00209 }
00210 System.err.println("");
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 System.err.println("int: P len="+length);
00224 for(int i=0; i<p_array.length; i++){
00225 System.err.print(Integer.toHexString(p_array[i]&0xff)+":");
00226 }
00227 System.err.println("");
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 System.err.println("int: q len="+length);
00240 for(int i=0; i<q_array.length; i++){
00241 System.err.print(Integer.toHexString(q_array[i]&0xff)+":");
00242 }
00243 System.err.println("");
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 System.err.println("int: ep len="+length);
00256 for(int i=0; i<ep_array.length; i++){
00257 System.err.print(Integer.toHexString(ep_array[i]&0xff)+":");
00258 }
00259 System.err.println("");
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 System.err.println("int: eq len="+length);
00272 for(int i=0; i<eq_array.length; i++){
00273 System.err.print(Integer.toHexString(eq_array[i]&0xff)+":");
00274 }
00275 System.err.println("");
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 System.err.println("int: c len="+length);
00288 for(int i=0; i<c_array.length; i++){
00289 System.err.print(Integer.toHexString(c_array[i]&0xff)+":");
00290 }
00291 System.err.println("");
00292 */
00293     }
00294     catch(Exception e){
00295       //System.err.println(e);
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 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1