1
1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3
Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
3
Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
5
5
Redistribution and use in source and binary forms, with or without
6
6
modification, are permitted provided that the following conditions are met:
73
73
private boolean encrypted=true;
75
IdentityFile(String identity, JSch jsch) throws JSchException{
76
this.identity=identity;
75
static IdentityFile newInstance(String prvfile, String pubfile, JSch jsch) throws JSchException{
80
FileInputStream fis=null;
82
file=new File(prvfile);
83
fis=new FileInputStream(prvfile);
84
prvkey=new byte[(int)(file.length())];
87
int i=fis.read(prvkey, len, prvkey.length-len);
95
try{ if(fis!=null) fis.close();}
97
if(e instanceof Throwable)
98
throw new JSchException(e.toString(), (Throwable)e);
99
throw new JSchException(e.toString());
102
String _pubfile=pubfile;
104
_pubfile=prvfile+".pub";
108
file=new File(_pubfile);
109
fis = new FileInputStream(_pubfile);
110
pubkey=new byte[(int)(file.length())];
113
int i=fis.read(pubkey, len, pubkey.length-len);
121
try{ if(fis!=null) fis.close();}
122
catch(Exception ee){}
124
// The pubfile is explicitry given, but not accessible.
125
if(e instanceof Throwable)
126
throw new JSchException(e.toString(), (Throwable)e);
127
throw new JSchException(e.toString());
130
return newInstance(prvfile, prvkey, pubkey, jsch);
133
static IdentityFile newInstance(String name, byte[] prvkey, byte[] pubkey, JSch jsch) throws JSchException{
135
return new IdentityFile(name, prvkey, pubkey, jsch);
142
private IdentityFile(String name, byte[] prvkey, byte[] pubkey, JSch jsch) throws JSchException{
84
151
c=Class.forName((String)jsch.getConfig("md5"));
85
152
hash=(HASH)(c.newInstance());
87
File file=new File(identity);
88
FileInputStream fis = new FileInputStream(identity);
89
byte[] buf=new byte[(int)(file.length())];
90
int len=fis.read(buf, 0, buf.length);
104
//System.out.println("invalid format: "+identity);
105
throw new JSchException("invaid privatekey: "+identity);
169
//System.err.println("invalid format: "+identity);
170
throw new JSchException("invalid privatekey: "+identity);
173
238
_buf.getInt(); // 0x3f6ff9be
175
240
byte[]_type=_buf.getString();
176
//System.out.println("type: "+new String(_type));
241
//System.err.println("type: "+new String(_type));
177
242
byte[] _cipher=_buf.getString();
178
243
String cipher=new String(_cipher);
179
//System.out.println("cipher: "+cipher);
244
//System.err.println("cipher: "+cipher);
180
245
if(cipher.equals("3des-cbc")){
182
247
byte[] foo=new byte[encoded_data.length-_buf.getOffSet()];
202
file=new File(identity+".pub");
203
fis=new FileInputStream(identity+".pub");
204
buf=new byte[(int)(file.length())];
205
len=fis.read(buf, 0, buf.length);
212
273
if(buf.length>4 && // FSecure's public key
213
274
buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-'){
216
do{i++;}while(buf.length>i && buf[i]!=0x0a);
217
if(buf.length<=i) return;
276
do{i++;}while(len>i && buf[i]!=0x0a);
220
279
if(buf[i]==0x0a){
221
280
boolean inheader=false;
222
for(int j=i+1; j<buf.length; j++){
281
for(int j=i+1; j<len; j++){
223
282
if(buf[j]==0x0a) break;
224
283
if(buf[j]==':'){inheader=true; break;}
259
318
while(i<len){ if(buf[i]==' ')break; i++;} i++;
260
319
if(i>=len) return;
262
while(i<len){ if(buf[i]==' ')break; i++;}
321
while(i<len){ if(buf[i]==' ' || buf[i]=='\n')break; i++;}
263
322
publickeyblob=Util.fromBase64(buf, start, i-start);
323
if(publickeyblob.length<4+7){ // It must start with "ssh-XXX".
324
if(JSch.getLogger().isEnabled(Logger.WARN)){
325
JSch.getLogger().log(Logger.WARN,
326
"failed to parse the public key");
267
332
catch(Exception e){
268
System.out.println("Identity: "+e);
333
//System.err.println("IdentityFile: "+e);
269
334
if(e instanceof JSchException) throw (JSchException)e;
335
if(e instanceof Throwable)
336
throw new JSchException(e.toString(), (Throwable)e);
270
337
throw new JSchException(e.toString());
275
341
public String getAlgName(){
289
355
if(_passphrase==null) return false;
290
byte[] passphrase=_passphrase.getBytes();
356
byte[] passphrase=_passphrase;
291
357
int hsize=hash.getBlockSize();
292
358
byte[] hn=new byte[key.length/hsize*hsize+
293
359
(key.length%hsize==0?0:hsize)];
330
391
catch(Exception e){
331
392
if(e instanceof JSchException) throw (JSchException)e;
393
if(e instanceof Throwable)
394
throw new JSchException(e.toString(), (Throwable)e);
332
395
throw new JSchException(e.toString());
379
442
rsa.setPrvKey(d_array, n_array);
382
byte[] goo=new byte[4];
383
goo[0]=(byte)(session.getSessionId().length>>>24);
384
goo[1]=(byte)(session.getSessionId().length>>>16);
385
goo[2]=(byte)(session.getSessionId().length>>>8);
386
goo[3]=(byte)(session.getSessionId().length);
388
rsa.update(session.getSessionId());
390
444
rsa.update(data);
391
445
byte[] sig = rsa.sign();
392
446
Buffer buf=new Buffer("ssh-rsa".length()+4+
407
System.out.print("P ");
461
System.err.print("P ");
409
463
for(i=0; i<foo.length; i++){
410
System.out.print(Integer.toHexString(foo[i]&0xff)+":");
464
System.err.print(Integer.toHexString(foo[i]&0xff)+":");
412
System.out.println("");
413
System.out.print("Q ");
466
System.err.println("");
467
System.err.print("Q ");
415
469
for(i=0; i<foo.length; i++){
416
System.out.print(Integer.toHexString(foo[i]&0xff)+":");
470
System.err.print(Integer.toHexString(foo[i]&0xff)+":");
418
System.out.println("");
419
System.out.print("G ");
472
System.err.println("");
473
System.err.print("G ");
421
475
for(i=0; i<foo.length; i++){
422
System.out.print(Integer.toHexString(foo[i]&0xff)+":");
476
System.err.print(Integer.toHexString(foo[i]&0xff)+":");
424
System.out.println("");
478
System.err.println("");
431
485
dsa.setPrvKey(prv_array, P_array, Q_array, G_array);
434
byte[] goo=new byte[4];
435
goo[0]=(byte)(session.getSessionId().length>>>24);
436
goo[1]=(byte)(session.getSessionId().length>>>16);
437
goo[2]=(byte)(session.getSessionId().length>>>8);
438
goo[3]=(byte)(session.getSessionId().length);
440
dsa.update(session.getSessionId());
443
487
dsa.update(data);
444
488
byte[] sig = dsa.sign();
445
489
Buffer buf=new Buffer("ssh-dss".length()+4+
527
//System.out.println("int: len="+length);
528
//System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
529
//System.out.println("");
571
//System.err.println("int: len="+length);
572
//System.err.print(Integer.toHexString(plain[index-1]&0xff)+":");
573
//System.err.println("");
532
576
length=plain[index++]&0xff;
538
582
System.arraycopy(plain, index, n_array, 0, length);
541
System.out.println("int: N len="+length);
585
System.err.println("int: N len="+length);
542
586
for(int i=0; i<n_array.length; i++){
543
System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
587
System.err.print(Integer.toHexString(n_array[i]&0xff)+":");
545
System.out.println("");
589
System.err.println("");
548
592
length=plain[index++]&0xff;
554
598
System.arraycopy(plain, index, e_array, 0, length);
557
System.out.println("int: E len="+length);
601
System.err.println("int: E len="+length);
558
602
for(int i=0; i<e_array.length; i++){
559
System.out.print(Integer.toHexString(e_array[i]&0xff)+":");
603
System.err.print(Integer.toHexString(e_array[i]&0xff)+":");
561
System.out.println("");
605
System.err.println("");
564
608
length=plain[index++]&0xff;
570
614
System.arraycopy(plain, index, d_array, 0, length);
573
System.out.println("int: D len="+length);
617
System.err.println("int: D len="+length);
574
618
for(int i=0; i<d_array.length; i++){
575
System.out.print(Integer.toHexString(d_array[i]&0xff)+":");
619
System.err.print(Integer.toHexString(d_array[i]&0xff)+":");
577
System.out.println("");
621
System.err.println("");
587
631
System.arraycopy(plain, index, p_array, 0, length);
590
System.out.println("int: P len="+length);
634
System.err.println("int: P len="+length);
591
635
for(int i=0; i<p_array.length; i++){
592
System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
636
System.err.print(Integer.toHexString(p_array[i]&0xff)+":");
594
System.out.println("");
638
System.err.println("");
597
641
length=plain[index++]&0xff;
603
647
System.arraycopy(plain, index, q_array, 0, length);
606
System.out.println("int: q len="+length);
650
System.err.println("int: q len="+length);
607
651
for(int i=0; i<q_array.length; i++){
608
System.out.print(Integer.toHexString(q_array[i]&0xff)+":");
652
System.err.print(Integer.toHexString(q_array[i]&0xff)+":");
610
System.out.println("");
654
System.err.println("");
613
657
length=plain[index++]&0xff;
619
663
System.arraycopy(plain, index, dmp1_array, 0, length);
622
System.out.println("int: dmp1 len="+length);
666
System.err.println("int: dmp1 len="+length);
623
667
for(int i=0; i<dmp1_array.length; i++){
624
System.out.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
668
System.err.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
626
System.out.println("");
670
System.err.println("");
629
673
length=plain[index++]&0xff;
635
679
System.arraycopy(plain, index, dmq1_array, 0, length);
638
System.out.println("int: dmq1 len="+length);
682
System.err.println("int: dmq1 len="+length);
639
683
for(int i=0; i<dmq1_array.length; i++){
640
System.out.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
684
System.err.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
642
System.out.println("");
686
System.err.println("");
645
689
length=plain[index++]&0xff;
651
695
System.arraycopy(plain, index, iqmp_array, 0, length);
654
System.out.println("int: iqmp len="+length);
698
System.err.println("int: iqmp len="+length);
655
699
for(int i=0; i<iqmp_array.length; i++){
656
System.out.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
700
System.err.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
658
System.out.println("");
702
System.err.println("");
661
705
catch(Exception e){
662
//System.out.println(e);
706
//System.err.println(e);
675
719
cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
677
721
for(int i=0; i<plain.length; i++){
678
System.out.print(Integer.toHexString(plain[i]&0xff)+":");
722
System.err.print(Integer.toHexString(plain[i]&0xff)+":");
680
System.out.println("");
724
System.err.println("");
683
727
else if(keytype==FSECURE){
788
832
public boolean isEncrypted(){
789
833
return encrypted;
791
public String getName(){return identity;}
793
private int writeSEQUENCE(byte[] buf, int index, int len){
795
index=writeLength(buf, index, len);
798
private int writeINTEGER(byte[] buf, int index, byte[] data){
800
index=writeLength(buf, index, data.length);
801
System.arraycopy(data, 0, buf, index, data.length);
806
private int countLength(int len){
808
if(len<=0x7f) return i;
816
private int writeLength(byte[] data, int index, int len){
817
int i=countLength(len)-1;
819
data[index++]=(byte)len;
822
data[index++]=(byte)(0x80|i);
825
data[index+i-1]=(byte)(len&0xff);
836
public String getName(){
832
840
private byte a2b(byte c){
834
842
if('a'<=c&&c<='z') return (byte)(c-'a'+10);
835
843
return (byte)(c-'A'+10);
837
private byte b2a(byte c){
838
if(0<=c&&c<=9) return (byte)(c+'0');
839
return (byte)(c-10+'A');
846
public boolean equals(Object o){
847
if(!(o instanceof IdentityFile)) return super.equals(o);
848
IdentityFile foo=(IdentityFile)o;
849
return getName().equals(foo.getName());
853
Util.bzero(encoded_data);
854
Util.bzero(prv_array);
860
public void finalize (){