Doug Zongker | 4c95917 | 2009-12-01 12:26:51 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2009 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Generates a public/private key pair suitable for use in signing |
| 18 | # android .apks and OTA update packages. |
| 19 | |
Kenny Root | 98356f6 | 2013-09-19 15:24:41 -0700 | [diff] [blame] | 20 | if [ "$#" -lt 2 -o "$#" -gt 3 ]; then |
Doug Zongker | 4c95917 | 2009-12-01 12:26:51 -0800 | [diff] [blame] | 21 | cat <<EOF |
Kenny Root | 98356f6 | 2013-09-19 15:24:41 -0700 | [diff] [blame] | 22 | Usage: $0 <name> <subject> [<keytype>] |
Doug Zongker | 4c95917 | 2009-12-01 12:26:51 -0800 | [diff] [blame] | 23 | |
| 24 | Creates <name>.pk8 key and <name>.x509.pem cert. Cert contains the |
Kenny Root | 98356f6 | 2013-09-19 15:24:41 -0700 | [diff] [blame] | 25 | given <subject>. A keytype of "rsa" or "ec" is accepted. |
Doug Zongker | 4c95917 | 2009-12-01 12:26:51 -0800 | [diff] [blame] | 26 | EOF |
| 27 | exit 2 |
| 28 | fi |
| 29 | |
| 30 | if [[ -e $1.pk8 || -e $1.x509.pem ]]; then |
| 31 | echo "$1.pk8 and/or $1.x509.pem already exist; please delete them first" |
| 32 | echo "if you want to replace them." |
| 33 | exit 1 |
| 34 | fi |
| 35 | |
| 36 | # Use named pipes to connect get the raw RSA private key to the cert- |
| 37 | # and .pk8-creating programs, to avoid having the private key ever |
| 38 | # touch the disk. |
| 39 | |
| 40 | tmpdir=$(mktemp -d) |
| 41 | trap 'rm -rf ${tmpdir}; echo; exit 1' EXIT INT QUIT |
| 42 | |
| 43 | one=${tmpdir}/one |
| 44 | two=${tmpdir}/two |
| 45 | mknod ${one} p |
| 46 | mknod ${two} p |
| 47 | chmod 0600 ${one} ${two} |
| 48 | |
| 49 | read -p "Enter password for '$1' (blank for none; password will be visible): " \ |
| 50 | password |
| 51 | |
Kenny Root | 98356f6 | 2013-09-19 15:24:41 -0700 | [diff] [blame] | 52 | if [ "${3}" = "rsa" -o "$#" -eq 2 ]; then |
| 53 | ( openssl genrsa -f4 2048 | tee ${one} > ${two} ) & |
| 54 | hash="-sha1" |
| 55 | elif [ "${3}" = "ec" ]; then |
| 56 | ( openssl ecparam -name prime256v1 -genkey -noout | tee ${one} > ${two} ) & |
| 57 | hash="-sha256" |
| 58 | else |
| 59 | echo "Only accepts RSA or EC keytypes." |
| 60 | exit 1 |
| 61 | fi |
Doug Zongker | 4c95917 | 2009-12-01 12:26:51 -0800 | [diff] [blame] | 62 | |
Kenny Root | 98356f6 | 2013-09-19 15:24:41 -0700 | [diff] [blame] | 63 | openssl req -new -x509 ${hash} -key ${two} -out $1.x509.pem \ |
Doug Zongker | 4c95917 | 2009-12-01 12:26:51 -0800 | [diff] [blame] | 64 | -days 10000 -subj "$2" & |
| 65 | |
| 66 | if [ "${password}" == "" ]; then |
| 67 | echo "creating ${1}.pk8 with no password" |
| 68 | openssl pkcs8 -in ${one} -topk8 -outform DER -out $1.pk8 -nocrypt |
| 69 | else |
| 70 | echo "creating ${1}.pk8 with password [${password}]" |
Kenny Root | 7f3f83e | 2014-07-31 12:59:57 -0700 | [diff] [blame] | 71 | export password |
| 72 | openssl pkcs8 -in ${one} -topk8 -outform DER -out $1.pk8 \ |
| 73 | -passout env:password |
| 74 | unset password |
Doug Zongker | 4c95917 | 2009-12-01 12:26:51 -0800 | [diff] [blame] | 75 | fi |
| 76 | |
| 77 | wait |
| 78 | wait |