1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33: if [ -z "$OPENSSL" ]; then OPENSSL=openssl; fi
34:
35: DAYS="-days 365"
36: CADAYS="-days 1095"
37: REQ="$OPENSSL req $SSLEAY_CONFIG"
38: CA="$OPENSSL ca $SSLEAY_CONFIG"
39: VERIFY="$OPENSSL verify"
40: X509="$OPENSSL x509"
41:
42: CATOP=./demoCA
43: CAKEY=./cakey.pem
44: CAREQ=./careq.pem
45: CACERT=./cacert.pem
46:
47: for i
48: do
49: case $i in
50: -\?|-h|-help)
51: echo "usage: CA -newcert|-newreq|-newca|-sign|-verify" >&2
52: exit 0
53: ;;
54: -newcert)
55:
56: $REQ -new -x509 -keyout newkey.pem -out newcert.pem $DAYS
57: RET=$?
58: echo "Certificate is in newcert.pem, private key is in newkey.pem"
59: ;;
60: -newreq)
61:
62: $REQ -new -keyout newkey.pem -out newreq.pem $DAYS
63: RET=$?
64: echo "Request is in newreq.pem, private key is in newkey.pem"
65: ;;
66: -newca)
67:
68:
69: NEW="1"
70: if [ "$NEW" -o ! -f ${CATOP}/serial ]; then
71:
72: mkdir ${CATOP}
73: mkdir ${CATOP}/certs
74: mkdir ${CATOP}/crl
75: mkdir ${CATOP}/newcerts
76: mkdir ${CATOP}/private
77: echo "00" > ${CATOP}/serial
78: touch ${CATOP}/index.txt
79: fi
80: if [ ! -f ${CATOP}/private/$CAKEY ]; then
81: echo "CA certificate filename (or enter to create)"
82: read FILE
83:
84:
85: if [ "$FILE" ]; then
86: cp $FILE ${CATOP}/private/$CAKEY
87: RET=$?
88: else
89: echo "Making CA certificate ..."
90: $REQ -new -keyout ${CATOP}/private/$CAKEY \
91: -out ${CATOP}/$CAREQ
92: $CA -out ${CATOP}/$CACERT $CADAYS -batch \
93: -keyfile ${CATOP}/private/$CAKEY -selfsign \
94: -infiles ${CATOP}/$CAREQ
95: RET=$?
96: fi
97: fi
98: ;;
99: -xsign)
100: $CA -policy policy_anything -infiles newreq.pem
101: RET=$?
102: ;;
103: -sign|-signreq)
104: $CA -policy policy_anything -out newcert.pem -infiles newreq.pem
105: RET=$?
106: cat newcert.pem
107: echo "Signed certificate is in newcert.pem"
108: ;;
109: -signcert)
110: echo "Cert passphrase will be requested twice - bug?"
111: $X509 -x509toreq -in newreq.pem -signkey newreq.pem -out tmp.pem
112: $CA -policy policy_anything -out newcert.pem -infiles tmp.pem
113: cat newcert.pem
114: echo "Signed certificate is in newcert.pem"
115: ;;
116: -verify)
117: shift
118: if [ -z "$1" ]; then
119: $VERIFY -CAfile $CATOP/$CACERT newcert.pem
120: RET=$?
121: else
122: for j
123: do
124: $VERIFY -CAfile $CATOP/$CACERT $j
125: if [ $? != 0 ]; then
126: RET=$?
127: fi
128: done
129: fi
130: exit 0
131: ;;
132: *)
133: echo "Unknown arg $i";
134: exit 1
135: ;;
136: esac
137: done
138: exit $RET
139: