简体   繁体   English

NSTask - 响应来自OpenSSL的输入请求

[英]NSTask - Respond to input request from OpenSSL

It seems i'm having some trouble understanding NSTask in Cocoa. 看来我在Cocoa中理解NSTask有些麻烦。 The application that I want to launch is openSSL. 我想要启动的应用程序是openSSL。 Currently, I'm able to send the information (launch path, arguments etc.) and I can get a response as well using NSPipe. 目前,我能够发送信息(发布路径,参数等),我也可以使用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. 一旦我启动NSTask,我就应该提供域名,国家等等。我的问题的原因是因为我需要能够使用openSSL生成证书签名请求并将其与其他一些数据一起发送,到服务器。 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. 此外,如果有人在Cocoa / ObjC中使用了某种openSSL实现,并且觉得它比使用NSTask更好,我也完全对此持开放态度。

Thanks in Advance. 提前致谢。

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. 我只是传递-subj参数而不是发送额外的输入。 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. 我只需要写入临时目录中的文件并通过输入管道提供它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM