blob: 15cccb7465b8a6017a49861f20a6e9995e2f6059 [file] [log] [blame]
ktimoney28fa9fb2022-05-30 16:08:27 +01001#!/bin/sh
2#
3# ============LICENSE_START=======================================================
ktimoneydf61b022023-04-04 17:17:35 +01004# Copyright (C) 2022-2023 Nordix Foundation.
ktimoney28fa9fb2022-05-30 16:08:27 +01005# ================================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# SPDX-License-Identifier: Apache-2.0
19# ============LICENSE_END=========================================================
20#
21
22
ktimoney076d02a2022-12-13 09:23:43 +000023CA_SUBJECT="/C=IE/ST=Dublin/L=Dublin/O=Keycloak/OU=Keycloak/CN=localhost/emailAddress=ca@mail.com"
24SERVER_SUBJECT="/C=IE/ST=Dublin/L=Dublin/O=Keycloak/OU=Keycloak/CN=localhost/emailAddress=server@mail.com"
ktimoney28fa9fb2022-05-30 16:08:27 +010025PW=changeit
ktimoneydf61b022023-04-04 17:17:35 +010026CERTNAME=tls
27CANAME=rootCA
28IP=$(minikube ip)
29DAYS=3650
30TRUSTSTORE=server.truststore
31KEYSTORE=server.keystore
32STORETYPE=PKCS12
ktimoney28fa9fb2022-05-30 16:08:27 +010033
ktimoneydf61b022023-04-04 17:17:35 +010034rm $TRUSTSTORE $KEYSTORE ${CANAME}.key ${CANAME}.crt ${CERTNAME}.key ${CERTNAME}.csr ${CERTNAME}.crt ${CERTNAME}.p12 2>/dev/null
ktimoney28fa9fb2022-05-30 16:08:27 +010035echo $PW > secretfile.txt
36
ktimoneydf61b022023-04-04 17:17:35 +010037openssl req -x509 -sha256 -days $DAYS -newkey rsa:4096 -keyout ${CANAME}.key -subj "$CA_SUBJECT" -passout file:secretfile.txt -out ${CANAME}.crt
ktimoney28fa9fb2022-05-30 16:08:27 +010038
ktimoneydf61b022023-04-04 17:17:35 +010039openssl req -new -newkey rsa:4096 -keyout ${CERTNAME}.key -subj "$SERVER_SUBJECT" -out ${CERTNAME}.csr -nodes
ktimoney28fa9fb2022-05-30 16:08:27 +010040
ktimoneydf61b022023-04-04 17:17:35 +010041echo "subjectKeyIdentifier = hash" > x509.ext
42echo "authorityKeyIdentifier = keyid:always,issuer:always" >> x509.ext
43echo "basicConstraints = CA:TRUE" >> x509.ext
44echo "keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyAgreement, keyCertSign" >> x509.ext
45echo "subjectAltName = DNS.1:localhost, IP.1:127.0.0.1, DNS.2:minikube, IP.2:${IP}, DNS.3:keycloak.default, DNS.4:keycloak.est.tech, DNS.5:keycloak" >> x509.ext
46echo "issuerAltName = issuer:copy" >> x509.ext
47echo "[ ca ]" >> x509.ext
48echo "# X509 extensions for a ca" >> x509.ext
49echo "keyUsage = critical, cRLSign, keyCertSign" >> x509.ext
50echo "basicConstraints = CA:TRUE, pathlen:0" >> x509.ext
51echo "subjectKeyIdentifier = hash" >> x509.ext
52echo "authorityKeyIdentifier = keyid:always,issuer:always" >> x509.ext
53echo "" >> x509.ext
54echo "[ server ]" >> x509.ext
55echo "# X509 extensions for a server" >> x509.ext
56echo "keyUsage = critical,digitalSignature,keyEncipherment" >> x509.ext
57echo "extendedKeyUsage = serverAuth,clientAuth" >> x509.ext
58echo "basicConstraints = critical,CA:FALSE" >> x509.ext
59echo "subjectKeyIdentifier = hash" >> x509.ext
60echo "authorityKeyIdentifier = keyid,issuer:always" >> x509.ext
ktimoney28fa9fb2022-05-30 16:08:27 +010061
ktimoneydf61b022023-04-04 17:17:35 +010062openssl x509 -req -CA ${CANAME}.crt -CAkey ${CANAME}.key -in ${CERTNAME}.csr -passin file:secretfile.txt -out ${CERTNAME}.crt -days $DAYS -CAcreateserial -extfile x509.ext
ktimoney28fa9fb2022-05-30 16:08:27 +010063
ktimoneydf61b022023-04-04 17:17:35 +010064keytool -import -trustcacerts -file ${CANAME}.crt -keystore $TRUSTSTORE -storepass $PW -storetype $STORETYPE -noprompt
65
66openssl pkcs12 -export -clcerts -in ${CERTNAME}.crt -inkey ${CERTNAME}.key -passout file:secretfile.txt -out ${CERTNAME}.p12
67
68keytool -importkeystore -srckeystore ${CERTNAME}.p12 -srcstorepass $PW -srcstoretype $STORETYPE -destkeystore $KEYSTORE -deststorepass $PW -deststoretype $STORETYPE
69
70rm secretfile.txt x509.ext 2>/dev/null