0

Is there a way to know a certificate serial number with Ruby? I know I can get it by using:

openssl x509 -inform DER -in file.cer -noout -serial >"serial.txt"
sawa
  • 156,411
  • 36
  • 254
  • 350
fvdev
  • 13
  • 5

2 Answers2

2

You can directly run shell commands in ruby and gets its output to a variable. I assume you will have to do something along the line of:

serial = `openssl x509 -inform DER -in file.cer -noout -serial`

Here is a more detailed answer: Calling shell commands from Ruby

sawa
  • 156,411
  • 36
  • 254
  • 350
Ziyan Junaideen
  • 2,914
  • 5
  • 38
  • 67
  • Thanks! It helped a lot, I was able to run it with the full path of file but I don't know how to run it with a variable (to use it on every file). i used this: serial = `openssl x509 -inform DER -in /home/faviovelez/webapps/projects/007/mosaicone007/lib/cer/2/LAN7008173R5.cer -noout -serial` and got this: "serial=3230303031303030303030333030303232383135\n" Do you know how can I run it with a variable that I can replace with the filename and path to it? – fvdev Oct 30 '17 at 17:59
  • Got it! I just did this: file = File.join(Rails.root, 'lib', 'cer', '2', 'LAN7008173R5.cer') serial = `openssl x509 -inform DER -in #{file} -noout -serial` And got the answer I needed! Thanks a lot! @Ziyan Junaideen – fvdev Oct 30 '17 at 18:23
1

There is module OpenSSL in standard library, which includes class OpenSSL::X509::Certificate, with method #serial

unkmas
  • 898
  • 4
  • 12
  • Thanks, I used this: new_file = File.read(Rails.root.join('lib', 'cer', '2', 'LAN7008173R5.cer')) translate = OpenSSL::X509::Certificate.new new_file but got this: #, issuer=#, serial=#, not_before=2016-10-25 21:52:11 UTC, not_after=2020-10-25 21:52:11 UTC> but when I run the command in shell I got: serial="serial=3230303031303030303030333030303232383135" (thats the expected result) – fvdev Oct 30 '17 at 17:54
  • Sorry, I don't understand your problem. Now you got your certificate object in variable `translate`. Did you call `translate.serial`? What did you received? – unkmas Oct 31 '17 at 03:59
  • Yes, and I got => # but solved with: file = File.join(Rails.root, 'lib', 'cer', '2', 'LAN7008173R5.cer') serial = openssl x509 -inform DER -in #{file} -noout -serial and got this: "serial=3230303031303030303030333030303232383135\n" – fvdev Nov 03 '17 at 15:59