package mykeynote.keynote;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import mykeynote.exceptions.FileCreateException;
import mykeynote.exceptions.FileDeleteException;
import mykeynote.exceptions.FileNotWritableException;
import mykeynote.exceptions.FileWriteException;
import mykeynote.exceptions.UniqueDirectoryNotFoundException;
import mykeynote.exceptions.keynote.KeyNotFoundException;
import mykeynote.exceptions.keynote.RetValFileNotFoundException;
import mykeynote.exceptions.keynote.RetValStringNotParsableException;
import mykeynote.exceptions.keynote.SignatureVerificationException;
import mykeynote.exceptions.keynote.SyntaxError;
import mykeynote.exceptions.keynote.cl.KeyNoteCLException;
import mykeynote.exceptions.keynote.cl.KeyNoteCLProcessCreationException;
import mykeynote.exceptions.keynote.cl.UnknownCLException;
import mykeynote.misc.CommonFunctions;
import mykeynote.misc.FormatConstants;
import mykeynote.server.Report;
import mykeynote.server.configuration.Configuration;
/**
* @author orlin
* @deprecated The MKN program is not going to support jni keynote support
* and as such this class is useless, please use {@link KeyNoteCL} directly.
*/
public class KeyNoteThread {
//private WriteFile write = new WriteFile();
private CommonFunctions common = new CommonFunctions();
private KeyNoteCL cl;
private BufferedReader read;
private static Report report;
private static Configuration config;
private static File sessionsDir;
public KeyNoteThread(Configuration config, Report report){
KeyNoteThread.config = config;
KeyNoteThread.report = report;
KeyNoteThread.sessionsDir = config.getTempDir();
if(config.getUseCL()){
cl = new KeyNoteCL(config, report);
}
}
/**
* This is the main part of the Servlet and Server. It takes a request, saves
* the data for logging, tests if the request is compilable with our current
* policy and returns the compliance value as a String
*
* @param enviroment
* The request environment (basically said the user data) as a String
* array, for which we need to check what it's compliance value is.
* @param resource
* The resource, that is being requested
* @return Returns the compliance value
* @throws InterruptedException
* @throws IOException
* @throws KeyNoteCLException
* @throws KeyNoteCLProcessCreationException
* @throws FileWriteException
* @throws FileNotWritableException
* @throws FileCreateException
* @throws FileDeleteException
* @throws RetValStringNotParsableException
*/
public String keynoteCL(String unique, String key ,String[] enviroment, String returnValues, String resource)
throws IOException, InterruptedException, KeyNoteCLProcessCreationException, KeyNoteCLException, FileDeleteException, FileCreateException, FileNotWritableException, FileWriteException, RetValStringNotParsableException {
File environmentFile = new File(sessionsDir, unique + FormatConstants.ASSERTIONFORMAT);
String assertion = getExisting(resource, FormatConstants.ASSERTIONFORMAT);
String returnValueFile = getExisting(resource, FormatConstants.RETFORMAT);
String environmentString = "";
for(String s: enviroment){
environmentString += s;
}
String answer = null;
common.writeFile(environmentFile ,environmentString, true);
if (config.getUseCL()) {
// using CL
answer = cl.verify(unique, report, environmentFile, returnValueFile, new File(assertion),new File(key));
} else {
System.err.println("Library is currently not supported");
return "false";
}
return answer;
}
/**
* This method allows us to get the environment file of a given resource
*
* @param resource
* <b>File String of Resource:</b> give the resource path, for whom the
* special file should be found
* @param end
* Gives the ending of the resource type searched
* @return returns the searched file
* @throws IOException
* An IOException is thrown when an assertion cannot be found
*/
public String getExisting(String resource, String end) throws FileNotFoundException{
File resourceFile = new File(resource);
if(!resourceFile.exists()) throw new FileNotFoundException(resource + " was not found");
File answer = new File(resourceFile.getParent(), "." + resourceFile.getName() + end);
if(!answer.exists())
answer = new File(resourceFile.getParent(), ".directory" + end);
if(!answer.exists())
throw new FileNotFoundException("File does not exist: " + answer.getAbsolutePath());
return answer.getAbsolutePath();
}
/**
* This method allows us to get the return values of a given resource
*
* @param resource
* <b>fileString:</b> give the resource path, for whom the
* return values should be found
* @return returns the return values
* @throws IOException
* An IOException is thrown when an assertion cannot be found
*/
public String getRetVal(String resource) throws RetValFileNotFoundException, IOException{
File resourceFile = new File(resource);
if(!resourceFile.exists()) throw new RetValFileNotFoundException(resource, report, "Return value for " + resource + " was not found");
File answer = new File(resourceFile.getParent(), "." + resourceFile.getName() + FormatConstants.RETFORMAT);
if(!answer.exists())
answer = new File(resourceFile.getParent(), ".directory" + FormatConstants.RETFORMAT);
read = new BufferedReader(new FileReader(answer));
return read.readLine();
}
public String keynoteU(String unique, String resource, String key, boolean withCred) throws KeyNoteCLProcessCreationException, KeyNoteCLException, UnknownCLException, SyntaxError, RetValStringNotParsableException{
if(config.getUseCL()){
//String assertion = unique;
//we have credential, that is good
try {
if(withCred) {
try{
cl.sigVerify(unique, new File(config.getTempDir() ,unique + ".cred"));
} catch (SignatureVerificationException e){
return "false\nUnable to verify signature";
}
}
return cl.verify(unique, report, new File(config.getTempDir(), unique + FormatConstants.ENVFORMAT),
getRetVal(resource), new File(getExisting(resource, FormatConstants.ASSERTIONFORMAT)),
new File(key));
} catch (InterruptedException e) {
return "false\nInterrupted Exception";
} catch (RetValFileNotFoundException e){
report.reportErrorLog(unique, e.getMessage());
return "false\nThe targeted file was not found";
} catch (IOException e) {
//report.reportErrorLog(e.printStackTrace());
System.out.println(e.toString());
return "false\nIOException";
}
//else
} else {
return "false\nLib version is not ready yet";
}
}
public boolean sigVerify(String unique, String credential) throws KeyNoteCLException, UnknownCLException, SyntaxError{
if(config.getUseCL()){
try {
cl.sigVerify(unique, credential);
return true;
} catch (InterruptedException e) {
return false;
} catch (IOException e) {
return false;
} catch (SignatureVerificationException e){
return false;
}
}else {
return false;
}
}
/**
* This is the new way to use KeyNote. It will check all credentials and their checksum and then start a
* KeyNote query to find out the answer
* @param unique The unique of the current thread
* @param credFrom This gives us the current starting point of the session
* @param credTo The last credential that should be checked
* @return The KeyNote query answer
* @throws KeyNoteCLException
* @throws KeyNoteCLProcessCreationException
* @throws RetValStringNotParsableException
* @throws FileNotFoundException
*/
public String verify(String unique, String resource, int credFrom, int credTo) throws KeyNotFoundException,
UniqueDirectoryNotFoundException, InterruptedException, IOException, RetValFileNotFoundException, KeyNoteCLProcessCreationException, KeyNoteCLException, RetValStringNotParsableException{
String directory = new File(new File(config.getTempDir(), config.getTempDir().getAbsolutePath()), unique).getAbsolutePath();
String uniqueDF = directory + unique;
File dir = new File(directory);
if(!dir.isDirectory()){
throw new UniqueDirectoryNotFoundException("The directory " + unique + " was not found");
}
String keyFile = directory + unique + FormatConstants.KEYFORMAT;
File key = new File(keyFile);
if(!key.isFile()){
//not allowed only for session with credentials
if(credTo != 0)
throw new KeyNotFoundException(unique, report, "The key for " + unique + " was not found");
//keyFile = Main.nokeyfile;
}
String credA = "";
while(credFrom > credTo){
credA += uniqueDF + "_" + credFrom + FormatConstants.CREDFORMAT;
credFrom++;
}
if(config.getUseCL()){
cl.verify(uniqueDF, report, new File(config.getTempDir(),uniqueDF + FormatConstants.ENVFORMAT),getRetVal(resource) , new File(config.getTempDir(),uniqueDF + FormatConstants.ASSERTIONFORMAT), new File(unique + FormatConstants.KEYFORMAT));
}
return "false\nKeyNoteLib still not implemented";
}
public void finilize() {
return;
}
}