]> Joshua Wise's Git repositories - dumload.git/blob - src/com/jcraft/jsch/KeyPairDSA.java
Initial commit.
[dumload.git] / src / com / jcraft / jsch / KeyPairDSA.java
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright 
12      notice, this list of conditions and the following disclaimer in 
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
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=0;
40   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       //System.err.println("KeyPairDSA: "+e); 
62       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=Util.str2byte("-----BEGIN DSA PRIVATE KEY-----");
69   private static final byte[] end=Util.str2byte("-----END DSA PRIVATE KEY-----");
70
71   byte[] getBegin(){ return begin; }
72   byte[] getEnd(){ return end; }
73
74   byte[] getPrivateKey(){
75     int content=
76       1+countLength(1) + 1 +                           // INTEGER
77       1+countLength(P_array.length) + P_array.length + // INTEGER  P
78       1+countLength(Q_array.length) + Q_array.length + // INTEGER  Q
79       1+countLength(G_array.length) + G_array.length + // INTEGER  G
80       1+countLength(pub_array.length) + pub_array.length + // INTEGER  pub
81       1+countLength(prv_array.length) + prv_array.length;  // INTEGER  prv
82
83     int total=
84       1+countLength(content)+content;   // SEQUENCE
85
86     byte[] plain=new byte[total];
87     int index=0;
88     index=writeSEQUENCE(plain, index, content);
89     index=writeINTEGER(plain, index, new byte[1]);  // 0
90     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){              // FSecure
103           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++; // SEQUENCE
120       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++; // INTEGER
128       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       //System.err.println(e);
187       //e.printStackTrace();
188       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=Util.str2byte("ssh-dss");
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 }
This page took 0.035665 seconds and 4 git commands to generate.