int type=ERROR;
int vendor=VENDOR_OPENSSH;
try{
File file=new File(prvkey);
FileInputStream fis=new FileInputStream(prvkey);
byte[] buf=new byte[(int)(file.length())];
int len=0;
while(true){
int i=fis.read(buf, len, buf.length-len);
if(i<=0)
break;
len+=i;
}
fis.close();
int i=0;
while(i<len){
if(buf[i]=='B'&& buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I'){
i+=6;
if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSA; }
else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H'){ // FSecure
type=UNKNOWN;
vendor=VENDOR_FSECURE;
}
else{
//System.err.println("invalid format: "+identity);
throw new JSchException("invalid privatekey: "+prvkey);
}
i+=3;
continue;
}
if(buf[i]=='C'&& buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==','){
i+=4;
for(int ii=0; ii<iv.length; ii++){
iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+(a2b(buf[i++])&0xf));
}
continue;
}
if(buf[i]==0x0d &&
i+1<buf.length && buf[i+1]==0x0a){
i++;
continue;
}
if(buf[i]==0x0a && i+1<buf.length){
if(buf[i+1]==0x0a){ i+=2; break; }
if(buf[i+1]==0x0d &&
i+2<buf.length && buf[i+2]==0x0a){
i+=3; break;
}
boolean inheader=false;
for(int j=i+1; j<buf.length; j++){
if(buf[j]==0x0a) break;
//if(buf[j]==0x0d) break;
if(buf[j]==':'){inheader=true; break;}
}
if(!inheader){
i++;
encrypted=false; // no passphrase
break;
}
}
i++;
}
if(type==ERROR){
throw new JSchException("invalid privatekey: "+prvkey);
}
int start=i;
while(i<len){
if(buf[i]==0x0a){
boolean xd=(buf[i-1]==0x0d);
System.arraycopy(buf, i+1,
buf,
i-(xd ? 1 : 0),
len-i-1-(xd ? 1 : 0)
);
if(xd)len--;
len--;
continue;
}
if(buf[i]=='-'){ break; }
i++;
}
data=Util.fromBase64(buf, start, i-start);
if(data.length>4 && // FSecure
data[0]==(byte)0x3f &&
data[1]==(byte)0x6f &&
data[2]==(byte)0xf9 &&
data[3]==(byte)0xeb){
Buffer _buf=new Buffer(data);
_buf.getInt(); // 0x3f6ff9be
_buf.getInt();
byte[]_type=_buf.getString();
//System.err.println("type: "+new String(_type));
byte[] _cipher=_buf.getString();
String cipher=Util.byte2str(_cipher);
//System.err.println("cipher: "+cipher);
if(cipher.equals("3des-cbc")){
_buf.getInt();
byte[] foo=new byte[data.length-_buf.getOffSet()];
_buf.getByte(foo);
data=foo;
encrypted=true;
throw new JSchException("unknown privatekey format: "+prvkey);
}
else if(cipher.equals("none")){
_buf.getInt();
_buf.getInt();
encrypted=false;
byte[] foo=new byte[data.length-_buf.getOffSet()];
_buf.getByte(foo);
data=foo;
}
}
if(pubkey!=null){
try{
file=new File(pubkey);
fis=new FileInputStream(pubkey);
buf=new byte[(int)(file.length())];
len=0;
while(true){
i=fis.read(buf, len, buf.length-len);
if(i<=0)
break;
len+=i;
}
fis.close();
if(buf.length>4 && // FSecure's public key
buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-'){
boolean valid=true;