1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
30 package com.jcraft.jsch;
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;
39 //private int key_size=0;
40 private int key_size=1024;
42 public KeyPairDSA(JSch jsch){
46 void generate(int key_size) throws JSchException{
47 this.key_size=key_size;
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();
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());
68 private static final byte[] begin=Util.str2byte("-----BEGIN DSA PRIVATE KEY-----");
69 private static final byte[] end=Util.str2byte("-----END DSA PRIVATE KEY-----");
71 byte[] getBegin(){ return begin; }
72 byte[] getEnd(){ return end; }
74 byte[] getPrivateKey(){
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
84 1+countLength(content)+content; // SEQUENCE
86 byte[] plain=new byte[total];
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);
98 boolean parse(byte[] plain){
101 if(vendor==VENDOR_FSECURE){
102 if(plain[0]!=0x30){ // FSecure
103 Buffer buf=new Buffer(plain);
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();
118 if(plain[index]!=0x30)return false;
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); }
126 if(plain[index]!=0x02)return false;
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); }
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); }
141 P_array=new byte[length];
142 System.arraycopy(plain, index, P_array, 0, length);
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); }
151 Q_array=new byte[length];
152 System.arraycopy(plain, index, Q_array, 0, length);
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); }
161 G_array=new byte[length];
162 System.arraycopy(plain, index, G_array, 0, length);
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); }
171 pub_array=new byte[length];
172 System.arraycopy(plain, index, pub_array, 0, length);
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); }
181 prv_array=new byte[length];
182 System.arraycopy(plain, index, prv_array, 0, length);
186 //System.err.println(e);
187 //e.printStackTrace();
193 public byte[] getPublicKeyBlob(){
194 byte[] foo=super.getPublicKeyBlob();
195 if(foo!=null) return foo;
197 if(P_array==null) return null;
199 Buffer buf=new Buffer(sshdss.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);
212 private static final byte[] sshdss=Util.str2byte("ssh-dss");
213 byte[] getKeyTypeName(){return sshdss;}
214 public int getKeyType(){return DSA;}
216 public int getKeySize(){return key_size; }
217 public void dispose(){
219 Util.bzero(prv_array);