Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - ProcessBuilder and running OpenSSL command which contains spaces

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...