2

I am facing a problem while executing openSSL command using my jar in Ubuntu environemnt. I have concluded that this is happening because of the space in the path of the file which is being passed as a parameter in the command e.g. SHA 256 in below command. I have used both process and ProcessBuilder classes for executing the same:

First:

String certFilePath = "/home/mplusuer/Desktop/Nishant/210515/TestData/TestData/SHA 256/nishant.cer"
String []cmdGetAlgorithm = new String[3];

cmdGetAlgorithm[0] = "openssl x509 -in";
cmdGetAlgorithm[1] = certFilePath;
cmdGetAlgorithm[2] = "-noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject,no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions";

ProcessBuilder pb = new ProcessBuilder(cmdGetAlgorithm[0], cmdGetAlgorithm[1],cmdGetAlgorithm[2]);
// setup other options ...

Process processGetAlgorithm = pb.start();
processGetAlgorithm.waitFor();

Second:

Runtime runtime = Runtime.getRuntime();
String cmdGetAlgorithm = "openssl x509 -in "
        + certFilePAth
        + " -noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject,no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions ";

Process processGetAlgorithm = runtime.exec(cmdGetAlgorithm);

the final command is as below, which works fine if executed separately on command prompt but fails when executed using java code:

openssl x509 -in /home/mplusuer/Desktop/Nishant/210515/TestData/TestData/SHA 256/suketu.cer  \
  -noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject, \
   no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions

I have used the below methods also for resolving this issue, but nothing worked as per expectation:

String quoted = "\"" + certFilePath + "\"";
String escaped = certFilePath.replace(" ", "\\ ");

Please see to it and help me in resolving the same.

jww
  • 97,681
  • 90
  • 411
  • 885
Nishant Kumar
  • 235
  • 2
  • 4
  • 13
  • What happens with 1) ? – user253751 May 25 '15 at 08:20
  • Please find the below errors for scenario :1) java.io.IOException: Cannot run program "openssl x509 -in": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) – Nishant Kumar May 25 '15 at 08:41
  • 2
    Do you have a program called `openssl x509 -in`? If not, then why are you trying to run a program called `openssl x509 -in`? (As opposed to running a program called `openssl` with the first two arguments being `x509` and `-in`) – user253751 May 25 '15 at 08:53
  • Possible duplicate of [Java execute a command with a space in the pathname](http://stackoverflow.com/questions/4916918/java-execute-a-command-with-a-space-in-the-pathname) – jww May 26 '15 at 00:51

1 Answers1

1
cmdGetAlgorithm[0] = "openssl x509 -in";
...

As @immibis stated in the comments, arg[0] is the program name. So the vector should look something like:

cmdArg[0] = "/usr/local/ssl/bin/openssl";
cmdArg[1] = "x509";
cmdArg[2] = "-in";
cmdArg[3] = certFilePAth;
cmdArg[4] = "-noout"
cmdArg[5] = "-text";
cmdArg[6] = "-certopt";
cmdArg[7] = "no_subject,no_header,no_version,no_serial,no_validity," +
            "no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions ";

You should always specify the complete filename of the executable to ensure you are running the intended executable, and not something planted by an adversary.

jww
  • 97,681
  • 90
  • 411
  • 885