0

I have a command cat hash.bin | openssl pkeyutl -sign -inkey privk.pem which can get result correctly.

Now I want to do it with python subprocess, I do it like this (where hash is read from hash.bin)

cmd = ['openssl', 'pkeyutl', '-sign', '-inkey ', prvk]
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
out = p.communicate(input=hash)[0]
print(out)

But openssl failed.

I can't use -in and -out because my code can't access to hard drive..

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
demonguy
  • 1,977
  • 5
  • 22
  • 34

1 Answers1

1

The problem is caused by the stray space character following the inkey command.

Try removing the space and it should work:

cmd = ['openssl', 'pkeyutl', '-sign', '-inkey', prvk]

The reason is that Popen() is passing through the space character as part of the -inkey option's token. openssl is deciding that there is not an option named -inkey (with a space), so it exits. It is as if you were executing this from the command line:

cat hash.bin | openssl pkeyutl -sign '-inkey ' privk.pem

which fails.

mhawke
  • 84,695
  • 9
  • 117
  • 138