简体   繁体   中英

Use a .csv file as a reference to make multiple self-signed certificates

I wanted to preface this with; I am not a coder I am curious as to if I can take a Batch file I have that will generate a self signed certificate using OpenSSL and link it to a .CSV file to generate multiple certificates with a different file name. (My batch file also converts the .cer to .der as that is the format I need).

Ie

R001.der
R002.der
R123.der

My batch file does call another batch file that just references the directory of OpenSSL.

My Batch File:

setlocal enableextensions
@echo off
call vars.bat

echo #### Generating a new private key
openssl genrsa -out NewCertPrivKey.pem 2048

echo #### Converting private key to DER format
openssl rsa -in NewCertPrivKey.pem -inform PEM -out NewCertPrivKey.der -outform DER

echo #### Generating certificate request
openssl req -new -sha256 -key NewCertPrivKey.pem -out NewCertUnsigned.pem -days 3650

echo #### Signing the certificate request
openssl ca -verbose -days 3650 -out NewCertSigned.pem -keyfile demoCA\private\CAPrivKey.pem -cert demoCA\CACert.pem -infiles NewCertUnsigned.pem

echo #### Converting certificate to DER
openssl x509 -in NewCertSigned.pem -inform PEM -outform DER -out NewCertSigned.der

echo ##################################
echo The files to upload are NewCertSigned.der and NewCertPrivKey.der

Since you've omitted to clearly tell us the content of your .csv file, I'll assume your file is the R001.der.. script you've included.

What you could do is (as a batch):

for /f %%a in (yourcsvfilename) do call yourbatchfileasposted.bat %%a

which will execute your batchfile delivering a parameter of the content of the first token (space-separated string) from each line of your .csv file.

Note Executing as a batch will mean not perpetually re-typing the command, but if you wish to make thing hard and re-type the command from the prompt, then reduce each %%a to %a . Note that the case of the %a is case-sensitive.

AND replace each NewCertPrivKey , NewCertUnsigned , NewCertSigned in the filenames used within your batch with NewCertPrivKey%1 , NewCertUnsigned%1 , NewCertSigned%1 (ie. insert %1 after the main filename-body.

This will create filenames like NewCertPrivKeyR001.der.pem when your batch is executed with a parameter of R001.der (%1 means copy the first parameter verbatim )

If you want NewCertPrivKeyR001.pem from a parameter of R001.der then use %~n1 instead of %1 . This uses the "name" part of parameter 1 - that is, the part preceding the final .

Note that your modified batch file will still operate as the original because if you don't supply a parameter when you run it, the %1 is empty, so produces an empty string when copied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM