aboutsummaryrefslogtreecommitdiffstats
path: root/certs/sample/gen-certs.sh
blob: f0aa1358465745ded50d402f1a6c9c7a7485931e (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
# 
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

ORG="/C=FR/ST=Brittany/L=Lorient/O=IoT.bzh"

cat > extensions << EOC
[root]
basicConstraints=CA:TRUE
keyUsage=keyCertSign
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid
[derivate]
basicConstraints=CA:TRUE
keyUsage=keyCertSign,digitalSignature
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid
EOC

keyof() { echo -n "$1.key.pem"; }
certof() { echo -n "$1.cert.pem"; }

generate() {

local s="$1" n="$2" cn="$3" sig="$4" 
local key="$(keyof "$n")" cert="$(certof "$n")"

if [ ! -f "$key" ]
then
	echo
	echo "generation of the $n key"
	openssl genpkey \
		-algorithm RSA -pkeyopt rsa_keygen_bits:4096 \
		-outform PEM \
		-out "$key"
fi

if [ ! -f "$cert" -o "$key" -nt "$cert" ]
then
	echo
	echo "generation of the $n certificate"
	openssl req -new \
			-key "$key" \
			-subj "$ORG/CN=$cn" |
	openssl x509 -req \
			-days 3653 \
			-sha256 \
			-extfile extensions \
			-trustout \
			$sig \
			-set_serial $s \
			-setalias "$cn" \
			-out "$cert"
fi

}

genroot() {
	local s="$1" n="$2" cn="$3"
	generate "$s" "$n" "$cn" "-signkey $(keyof "$n") -extensions root"
}

derivate() {
	local s="$1" n="$2" cn="$3" i="$4"
	generate "$s" "$n" "$cn" "-CA $(certof "$i") -CAkey $(keyof "$i") -extensions derivate"
}

	
genroot 1 root "Root certificate" 
derivate 2 developer "Root developer" root
derivate 3 platform "Root platform" root
derivate 4 partner "Root partner" root
derivate 5 public "Root public" root

rm extensions