0
votes

It seems i'm having some trouble understanding NSTask in Cocoa. The application that I want to launch is openSSL. Currently, I'm able to send the information (launch path, arguments etc.) and I can get a response as well using NSPipe. What I need to be able to do though is to respond to the input requests that the application asks for. With the following code, I can send and read a response from a file:

 NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: launchPath];
[task setArguments: arguments];
[task setCurrentDirectoryPath:dir];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];
NSString *response =  [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

Once I launch the NSTask, I'm expected to provide things like the Domain Name, Country etc. The reason for my question is because I need to be able to generate a Certificate Signing Request with openSSL and send it, along with some other data, over to a server. The code above is not broken, I'm just not sure how I can go about sending over that input.

Also, if anyone has used some sort of openSSL implementation with Cocoa/ObjC and feels that it would be a better option than using NSTask, I'm completely open to that as well.

Thanks in Advance.

1

1 Answers

2
votes

Not that anybody is looking at this, but if you are and didn't already know the answer, I found the solution but ended up using a different method. Instead of sending additional input, I would just pass the -subj parameter. However, the solution to what I was originally asking is as follows:

NSTask *task = [[NSTask alloc] init];

NSString *tmpdir=NSTemporaryDirectory();
[task setCurrentDirectoryPath:tmpdir];

[task setLaunchPath:@"/usr/bin/openssl"];
NSArray *sslarguments=@[@"req",@"-nodes",@"-newkey",@"rsa:2048",@"-keyout",@"myserver.key",@"-out",@"server.csr"];
[task setArguments:sslarguments];


NSPipe * in = [NSPipe pipe];

[task setStandardInput:in];

NSData *data=[@"GB\nYorks\n\nYork\SimuplanSL\nIT\nsomeone@simuplan.com" dataUsingEncoding:NSUTF8StringEncoding];

[task launch];
[[in fileHandleForWriting] writeData:data];
[task waitUntilExit];

I just had to write to a file in a temporary directory and feed it through the input pipe.