Finally found a form that I like...
[openssl.git] / apps / CA.com
1 $! CA - wrapper around ca to make it easier to use ... basically ca requires
2 $!      some setup stuff to be done before you can use it and this makes
3 $!      things easier between now and when Eric is convinced to fix it :-)
4 $!
5 $! CA -newca ... will setup the right stuff
6 $! CA -newreq ... will generate a certificate request 
7 $! CA -sign ... will sign the generated request and output 
8 $!
9 $! At the end of that grab newreq.pem and newcert.pem (one has the key 
10 $! and the other the certificate) and cat them together and that is what
11 $! you want/need ... I'll make even this a little cleaner later.
12 $!
13 $!
14 $! 12-Jan-96 tjh    Added more things ... including CA -signcert which
15 $!                  converts a certificate to a request and then signs it.
16 $! 10-Jan-96 eay    Fixed a few more bugs and added the SSLEAY_CONFIG
17 $!                 environment variable so this can be driven from
18 $!                 a script.
19 $! 25-Jul-96 eay    Cleaned up filenames some more.
20 $! 11-Jun-96 eay    Fixed a few filename missmatches.
21 $! 03-May-96 eay    Modified to use 'openssl cmd' instead of 'cmd'.
22 $! 18-Apr-96 tjh    Original hacking
23 $!
24 $! Tim Hudson
25 $! tjh@cryptsoft.com
26 $!
27 $!
28 $! default ssleay.cnf file has setup as per the following
29 $! demoCA ... where everything is stored
30 $
31 $ IF F$TYPE(SSLEAY_CONFIG) .EQS. "" THEN SSLEAY_CONFIG := SSLLIB:SSLEAY.CNF
32 $
33 $ DAYS   = "-days 365"
34 $ REQ    = openssl + " req " + SSLEAY_CONFIG
35 $ CA     = openssl + " ca " + SSLEAY_CONFIG
36 $ VERIFY = openssl + " verify"
37 $ X509   = openssl + " x509"
38 $ PKCS12 = openssl + " pkcs12"
39 $ echo   = "write sys$Output"
40 $!
41 $ s = F$PARSE(F$ENVIRONMENT("DEFAULT"),"[]") - "].;"
42 $ CATOP  := 's'.demoCA
43 $ CAKEY  := ]cakey.pem
44 $ CACERT := ]cacert.pem
45 $
46 $ __INPUT := SYS$COMMAND
47 $ RET = 1
48 $!
49 $ i = 1
50 $opt_loop:
51 $ if i .gt. 8 then goto opt_loop_end
52 $
53 $ prog_opt = F$EDIT(P'i',"lowercase")
54 $
55 $ IF (prog_opt .EQS. "?" .OR. prog_opt .EQS. "-h" .OR. prog_opt .EQS. "-help") 
56 $ THEN
57 $   echo "usage: CA -newcert|-newreq|-newca|-sign|-verify" 
58 $   exit
59 $ ENDIF
60 $!
61 $ IF (prog_opt .EQS. "-input")
62 $ THEN
63 $   ! Get input from somewhere other than SYS$COMMAND
64 $   i = i + 1
65 $   __INPUT = P'i'
66 $   GOTO opt_loop_continue
67 $ ENDIF
68 $!
69 $ IF (prog_opt .EQS. "-newcert")
70 $ THEN
71 $   ! Create a certificate.
72 $   DEFINE/USER SYS$INPUT '__INPUT'
73 $   REQ -new -x509 -keyout newreq.pem -out newreq.pem 'DAYS'
74 $   RET=$STATUS
75 $   echo "Certificate (and private key) is in newreq.pem"
76 $   GOTO opt_loop_continue
77 $ ENDIF
78 $!
79 $ IF (prog_opt .EQS. "-newreq")
80 $ THEN
81 $   ! Create a certificate request
82 $   DEFINE/USER SYS$INPUT '__INPUT'
83 $   REQ -new -keyout newreq.pem -out newreq.pem 'DAYS'
84 $   RET=$STATUS
85 $   echo "Request (and private key) is in newreq.pem"
86 $   GOTO opt_loop_continue
87 $ ENDIF
88 $!
89 $ IF (prog_opt .EQS. "-newca")
90 $ THEN
91 $   ! If explicitly asked for or it doesn't exist then setup the directory
92 $   ! structure that Eric likes to manage things.
93 $   IF F$SEARCH(CATOP+"]serial.") .EQS. ""
94 $   THEN
95 $     CREATE /DIR /PROTECTION=OWNER:RWED 'CATOP']
96 $     CREATE /DIR /PROTECTION=OWNER:RWED 'CATOP'.certs]
97 $     CREATE /DIR /PROTECTION=OWNER:RWED 'CATOP'.crl]
98 $     CREATE /DIR /PROTECTION=OWNER:RWED 'CATOP'.newcerts]
99 $     CREATE /DIR /PROTECTION=OWNER:RWED 'CATOP'.private]
100 $     OPEN   /WRITE ser_file 'CATOP']serial. 
101 $     WRITE ser_file "01"
102 $     CLOSE ser_file
103 $     APPEND/NEW NL: 'CATOP']index.txt
104 $   ENDIF
105 $!
106 $   IF F$SEARCH(CATOP+".private"+CAKEY) .EQS. ""
107 $   THEN
108 $     READ '__INPUT' FILE -
109            /PROMT="CA certificate filename (or enter to create)"
110 $     IF F$SEARCH(FILE) .NES. ""
111 $     THEN
112 $       COPY 'FILE' 'CATOP'.private'CAKEY'
113 $       RET=$STATUS
114 $     ELSE
115 $       echo "Making CA certificate ..."
116 $       DEFINE/USER SYS$INPUT '__INPUT'
117 $       REQ -new -x509 -keyout 'CATOP'.private'CAKEY' -
118                        -out 'CATOP''CACERT' 'DAYS'
119 $       RET=$STATUS
120 $     ENDIF
121 $   ENDIF
122 $   GOTO opt_loop_continue
123 $ ENDIF
124 $!
125 $ IF (prog_opt .EQS. "-pkcs12")
126 $ THEN
127 $   i = i + 1
128 $   cname = P'i'
129 $   IF cname .EQS. "" THEN cname = "My certificate"
130 $   PKCS12 -in newcert.pem -inkey newreq.pem -certfile 'CATOP''CACERT -
131            -out newcert.p12 -export -name "''cname'"
132 $   RET=$STATUS
133 $   exit RET
134 $ ENDIF
135 $!
136 $ IF (prog_opt .EQS. "-xsign")
137 $ THEN
138 $!
139 $   DEFINE/USER SYS$INPUT '__INPUT'
140 $   CA -policy policy_anything -infiles newreq.pem
141 $   RET=$STATUS
142 $   GOTO opt_loop_continue
143 $ ENDIF
144 $!
145 $ IF ((prog_opt .EQS. "-sign") .OR. (prog_opt .EQS. "-signreq"))
146 $ THEN
147 $!   
148 $   DEFINE/USER SYS$INPUT '__INPUT'
149 $   CA -policy policy_anything -out newcert.pem -infiles newreq.pem
150 $   RET=$STATUS
151 $   type newcert.pem
152 $   echo "Signed certificate is in newcert.pem"
153 $   GOTO opt_loop_continue
154 $ ENDIF
155 $!
156 $ IF (prog_opt .EQS. "-signcert")
157 $  THEN
158 $!   
159 $   echo "Cert passphrase will be requested twice - bug?"
160 $   DEFINE/USER SYS$INPUT '__INPUT'
161 $   X509 -x509toreq -in newreq.pem -signkey newreq.pem -out tmp.pem
162 $   DEFINE/USER SYS$INPUT '__INPUT'
163 $   CA -policy policy_anything -out newcert.pem -infiles tmp.pem
164 y
165 y
166 $   type newcert.pem
167 $   echo "Signed certificate is in newcert.pem"
168 $   GOTO opt_loop_continue
169 $ ENDIF
170 $!
171 $ IF (prog_opt .EQS. "-verify")
172 $ THEN
173 $!   
174 $   i = i + 1
175 $   IF (p'i' .EQS. "")
176 $   THEN
177 $     DEFINE/USER SYS$INPUT '__INPUT'
178 $     VERIFY "-CAfile" 'CATOP''CACERT' newcert.pem
179 $   ELSE
180 $     j = i
181 $    verify_opt_loop:
182 $     IF j .GT. 8 THEN GOTO verify_opt_loop_end
183 $     IF p'j' .NES. ""
184 $     THEN 
185 $       DEFINE/USER SYS$INPUT '__INPUT'
186 $       __tmp = p'j'
187 $       VERIFY "-CAfile" 'CATOP''CACERT' '__tmp'
188 $       tmp=$STATUS
189 $       IF tmp .NE. 0 THEN RET=tmp
190 $     ENDIF
191 $     j = j + 1
192 $     GOTO verify_opt_loop
193 $    verify_opt_loop_end:
194 $   ENDIF
195 $   
196 $   GOTO opt_loop_end
197 $ ENDIF
198 $!
199 $ IF (prog_opt .NES. "")
200 $ THEN
201 $!   
202 $   echo "Unknown argument ''prog_opt'"
203 $   
204 $   EXIT 3
205 $ ENDIF
206 $
207 $opt_loop_continue:
208 $ i = i + 1
209 $ GOTO opt_loop
210 $
211 $opt_loop_end:
212 $ EXIT 'RET'