← history for x509crypt
1de17cf2x509crypt239 lines⬡ raw↓ download
  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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#! /bin/bash

# X509crypt (x509crypt) is Copyright (C) 2014-2021 Paolo Lulli, Kevwe Technology AB
#
# This file is part of x509crypt.
#
# x509crypt is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# x509crypt is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#


CONFIG_DIR=$HOME/.x509crypt
GLOBAL_CONFIG=${CONFIG_DIR}/config


DEPENDENCIES="qrencode openssl"

setup_global_config(){
  echo "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department"> ${GLOBAL_CONFIG}
  echo "Created config file: [${GLOBAL_CONFIG}]"
  exit 1
}

test -d $CONFIG_DIR || mkdir -p $CONFIG_DIR
test -f $GLOBAL_CONFIG || setup_global_config

generate_keys(){
  echo "GENERATE KEYS"
  profile=$1
  CONFIG_CERT_NAME=${profile}
  organization=$(cat ${GLOBAL_CONFIG})
  openssl genrsa -out $CONFIG_DIR/$profile/certs/$CONFIG_CERT_NAME.key 4096
  
  openssl req -batch -new -key $CONFIG_DIR/$profile/certs/$CONFIG_CERT_NAME.key \
  -subj "$organization/CN=$profile"\
  -out $CONFIG_DIR/$profile/certs/$CONFIG_CERT_NAME.csr
  
  openssl x509 -req -days 365 -in $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.csr \
  -signkey $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.key \
  -out $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}

export_key(){
  profile_name=$1
  CONFIG_CERT_NAME=${profile}

  test -f $CONFIG_CERT_NAME.key.enc && echo "ERROR: File already exist: [$CONFIG_CERT_NAME.key.enc]"
  test -f $CONFIG_CERT_NAME.key.enc && exit 1;

  genpassword=$(uuidgen)
  echo "----------------------------------------------------"
  echo ""
  echo "secret key password:[${genpassword}]"
  echo ""
  echo "----------------------------------------------------"
  echo ""
  qrencode "${genpassword}" -t UTF8

  openssl rsa -aes256 -in $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.key -out $CONFIG_CERT_NAME.key.enc -passout pass:${genpassword}
  echo "----------------------------------------------------"
  echo ""
  echo "secret key path:[$CONFIG_CERT_NAME.key.enc]"
  echo ""
  echo "----------------------------------------------------"
  echo ""
}

dependencies_check()
{
  for d in $DEPENDENCIES; do
    CHK=$(which $d)
    if [ "$CHK" = "" ]; then
      echo "missing deps: [$d]";
      exit 1
    fi
  done
}

export_certificate(){
  profile_name=$1
  CONFIG_CERT_NAME=${profile}
  cat $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}


dump_certificate(){
  profile_name=$1
  CONFIG_CERT_NAME=${profile}
  cat $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt | openssl x509 -text
}

setup_profile(){
  echo "SETUP PROFILE"
  profile_name=$1
  test -d $CONFIG_DIR/$profile_name || mkdir -p $CONFIG_DIR/$profile_name
  test -d $CONFIG_DIR/$profile_name/conf || mkdir -p $CONFIG_DIR/$profile_name/conf
  test -d $CONFIG_DIR/$profile_name/certs || mkdir -p $CONFIG_DIR/$profile_name/certs
}

file_decrypt(){
  #echo "FILE DECRYPT: $1"
  test -d $CONFIG_DIR/$profile_name || echo "profile: [$profile_name] does not exist, EXIT"
  test -d $CONFIG_DIR/$profile_name || exit 1
  decrypted_file=$1".cleartext"
  CONFIG_CERT_NAME=${profile_name}
  #openssl smime -decrypt -binary -in $1 -inform DER -out $decrypted_file -inkey $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.key -passin pass:your_password
  openssl smime -decrypt -binary -in $1 -inform DER -out $decrypted_file -inkey $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.key
}

file_encrypt(){
  #echo "FILE ENCRYPT: $1"
  file_size=$(du -s $1 | awk '{print $1}')
  
#  if [ ${file_size} -gt 500000 ]; then
#    echo "File too big to encrypt with smime"
#    exit 1
#  fi

  encrypted_file=$1".enc"
  CONFIG_CERT_NAME=${profile_name}
  openssl smime -encrypt -binary -aes-256-cbc -in $1 -out $encrypted_file  -outform DER $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}

file_import(){
  echo "FILE IMPORT: $1"
  imported_file=$1
  CONFIG_CERT_NAME=${profile_name}

  test -f $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt && echo "profile: [$profile_name] exists, SKIPPING"
  test -f $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt && exit 1
  
  test -d $CONFIG_DIR/$profile_name || mkdir -p $CONFIG_DIR/$profile_name/certs
  
  cp ${imported_file} $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}


list_profiles(){
  echo "profiles"
  echo "--------"
  for i in $(ls $CONFIG_DIR  | sed -e s/config//); do echo ${i}; done
}

usage(){
  program=$(basename $0)
  echo "${program} <profile> -e <file> : Encrypt <file> (safety checks)"
  echo "${program} <profile> -E <file> : Encrypt <file>"
  
  echo "${program} <profile> -d <file> : Decrypt <file>"
 
  echo "${program} <profile> -g        : Generate keys"
  
  echo "${program} <profile> -x        : eXport pub key"
  echo "${program} <profile> -s        : export private key"
  echo "${program} <profile> -X        : eXport pub key details"
  echo "${program} <profile> -i <file> : Import pub key"
  echo "${program} -l                  : List profiles"
}

dependencies_check

if [ "$#" -gt 3 ]; then
  usage
  exit
fi
if [ "$#" -eq 0 ]; then
  usage
  exit
fi

if [ "$#" -eq 1 ]; then
  if [ "$1" == "-l" ]; then
    list_profiles
    exit
  fi
  if [ "$2" == "-x" ]; then
    export_certificate "${profile}"
    exit
  fi
  usage
fi

if [ "$#" -eq 2 ]; then
  profile=$1
  if [ "$2" == "-g" ]; then
    test -d $CONFIG_DIR/$profile && echo "profile: [$profile] exists, SKIPPING"
    test -d $CONFIG_DIR/$profile && exit 1

    setup_profile "${profile}"
    generate_keys "${profile}"
    exit
  fi
  if [ "$2" == "-x" ]; then
    export_certificate "${profile}"
    exit
  fi
  if [ "$2" == "-s" ]; then
    export_key "${profile}"
    exit
  fi
  if [ "$2" == "-X" ]; then
    dump_certificate "${profile}"
    exit
  fi
  
  usage
fi
if [ "$#" -eq 3 ]; then
  profile_name=$1
  input_file=$3
  if [ "$2" == "-E" ]; then
    file_encrypt $input_file
    exit
  fi
  if [ "$2" == "-e" ]; then
    file_encrypt $input_file
    file_decrypt $input_file".enc"
    diff $input_file $input_file".enc.cleartext" || echo "ERROR: BAD ENCRYPTION"
    rm $input_file".enc.cleartext"
    exit
  fi
  if [ "$2" == "-d" ]; then
    file_decrypt $input_file
    exit
  fi
  if [ "$2" == "-i" ]; then
    file_import $input_file
    exit
  fi

  usage
fi