0

I'm writing a task to access the OpenSSL binary. If I copy/paste the command without the single quote, then it works perfectly in terminal.

I keep pasting the quote into the command. Here is the code / error...

let keySizeValue = keySizes[keySizeChoice.indexOfSelectedItem]

task.launchPath = "/usr/bin/openssl"
task.arguments = ["req -new -newkey \(keySizeValue) -out ~/Desktop/Certs/MyNew.csr -keyout ~/Desktop/Certs/\(privateKeyText.stringValue).key -subj \"/C=US/ST=\(stateText.stringValue)/L=\(cityText.stringValue)/O=\(organizationText.stringValue)/OU=\(departmentText.stringValue)/CN=\(commonNameText.stringValue)\"", "\(privateKeyPassword)", "\(passwordVerify)"]

task.launch()
task.waitUntilExit()

I've defined my task and keySizes array above, however the error is Openssl:Error: req -new -newkey rsa:2048 -out ~/Desktop/Certs/MyNew.csr -keyout ~/Desktop/Certs/a.key -subj "/C=US/ST=a/L=a/O=a/OU=a/CN=a"' is an invalid command.

It could be that i'm using NSTask improperly, but i feel like this looks right. Anyone got a good suggestion :D

Lorenzo
  • 3,293
  • 4
  • 29
  • 56
chrono
  • 83
  • 1
  • 8

2 Answers2

2

If you want to give multiple arguments to a NSTask, they must be added to the arguments array as an unique item separated by a comma

let keySizeValue = keySizes[keySizeChoice.indexOfSelectedItem]

task.launchPath = "/usr/bin/openssl"
task.arguments = ["req", "-new", "-newkey", "\(keySizeValue)", "-out", "~/Desktop/Certs/MyNew.csr", "-keyout", "~/Desktop/Certs/\(privateKeyText.stringValue).key", "-subj", "\"/C=US/ST=\(stateText.stringValue)/L=\(cityText.stringValue)/O=\(organizationText.stringValue)/OU=\(departmentText.stringValue)/CN=\(commonNameText.stringValue)\"", "\(privateKeyPassword)", "\(passwordVerify)"]

task.launch()
task.waitUntilExit()
agarzon
  • 83
  • 1
  • 7
0

If anyone is still looking at this, I was able to find a solution. I had to separate the arguments of my NSTask and then pass the arguments through an NSPipe and use stdin to get the argument to properly go into terminal

mainTask.launchPath = "/usr/bin/openssl"
mainTask.arguments = ["req", "-new", "-nodes", "-newkey", "\(keySizeValue)", "-passout", "pass:\(privateKeyPassword.stringValue)", "-out", "/Users/\(userName)/Desktop/Certs/MyNew.csr", "-keyout", "/Users/\(userName)/Desktop/Certs/\(privateKeyText.stringValue).key", "-subj", "\"/C=US/ST=\(stateText.stringValue)/L=\(cityText.stringValue)/O=\(organizationText.stringValue)/OU=\(departmentText.stringValue)/CN=\(commonNameText.stringValue)\""]

let pipe = NSPipe()
mainTask.standardInput = pipe
mainTask.launch()
mainTask.waitUntilExit()

That fixed my error. Thanks for the help

chrono
  • 83
  • 1
  • 8