aboutsummaryrefslogtreecommitdiff
path: root/lib/_ak_gpg
blob: 0d838f1f624b43563b14c3ae153f5b61bac70a6f (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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env bash
source $AK_LIBDIR/_ak_log
source $AK_LIBDIR/_ak_ipfs
source $AK_LIBDIR/_ak_config
source $AK_LIBDIR/_ak_settings

_ak_gpg(){
    gpg2 --homedir $AK_GPGHOME $*
}

_ak_gpg_check_or_create(){
    _ak_gpg --list-keys | grep kaos@kaos.kaos -B 1
    if [ $? -ne 0 ]
    then
        _ak_gpg --batch --passphrase '' --quick-gen-key kaos@kaos.kaos rsa3072 sign 0
        AK_FINGERPRINT="$(_ak_gpg --list-keys | grep kaos@kaos.kaos -B 1 | head -n 1 | awk '{print $1}')"
        _ak_gpg --batch --passphrase '' --quick-add-key $AK_FINGERPRINT rsa3072 encrypt 0
    fi
}

_ak_gpg_create_key(){
    _ak_log_debug "Key name: $1"
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        _ak_log_error "No email label was given"
        exit 1
    fi
    _ak_gpg --list-secret-keys | grep -F $1 >/dev/null 2>&1
    if [ $? -eq 0 ]
    then
        _ak_log_error "Key exists with the same email label"
        exit 1
    fi
    _ak_gpg --batch --passphrase '' --quick-gen-key $1 rsa4096 sign 0
    FINGERPRINT="$(_ak_gpg --list-keys | grep $1 -B 1 | head -n 1 | awk '{print $1}')"
    _ak_gpg --batch --passphrase '' --quick-add-key $FINGERPRINT rsa4096 encrypt 0
}

_ak_gpg_key_import_from_file(){
    if [ -z $1 ]
    then
        _ak_log_error "No argument given"
        exit 1
    fi
    if [ ! -n "$1" ]
    then
        _ak_log_error "Empty argument given"
        exit 1
    fi
    if [ ! -f "$AK_IPFS_ARTIFACTS/$1" ]
    then
        _ak_log_error "File not found"
        exit 1
    fi
    _ak_gpg --import $AK_IPFS_ARTIFACTS/$1 > /dev/null 2>&1
}

_ak_gpg_key_self_get_fingerprint_from_config(){
    _ak_config_show | jq -r '.gpg'
}

_ak_gpg_key_self_get_fingerprint(){
    if [ -z $1 ]
    then
        _ak_ipfs_cat "$(_ak_gpg_key_self_get_fingerprint_from_config)" | \
            _ak_gpg --show-keys 2>&1 | \
            head -n 2 | \
            tail -n 1 | \
            sed 's/ //g'
    else
        exit 1
    fi
}

_ak_gpg_key_get_fingerprint_from_ipfs(){
    if [ -n "$1" ]
    then
        _ak_ipfs_cat "$1" | \
            _ak_gpg --show-keys 2>&1 | \
            head -n 2 | \
            tail -n 1 | \
            sed 's/ //g'
    else
        exit 1
    fi
}

_ak_gpg_sign(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ]
    then
        _ak_gpg --sign --sign-with $AK_FINGERPRINT --armor --output $1 $2
    else
        exit 1
    fi
}

_ak_gpg_sign_detached(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ]
    then
        _ak_gpg --detach-sign --sign-with $AK_FINGERPRINT --armor --output $1 $2
    else
        _ak_log_error "Not enough arguments"
        exit 1
    fi
}

_ak_gpg_sign_clear(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ]
    then
        _ak_gpg --clear-sign --sign-with $AK_FINGERPRINT --armor --output $1 $2
    else
        exit 1
    fi
}

_ak_gpg_encrypt_sign(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] && [ ! -z $3 ] && [ -n "$3" ]
    then
        _ak_gpg --sign-with $AK_FINGERPRINT --encypt -r $3 --armor --output $1 $2
    else
        exit 1
    fi
}

_ak_gpg_encrypt_sign_for_self(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ]
    then
        _ak_gpg --sign-with $AK_FINGERPRINT --encypt -r $AK_FINGERPRINT --armor --output $1 $2
    else
        exit 1
    fi
}

_ak_gpg_encrypt(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] && [ ! -z $3 ] && [ -n "$3" ]
    then
        _ak_gpg --encypt -r $3 --armor --output $1 $2
    else
        exit 1
    fi
}

_ak_gpg_encrypt_for_self(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ]
    then
        _ak_gpg --encypt -r $AK_FINGERPRINT --armor --output $1 $2
    else
        exit 1
    fi
}

_ak_gpg_verify_clear_signature(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ -f "$1" ]
    then
        fingerprint="$(cat $1 | _ak_gpg 2>&1 | grep RSA | awk '{print $5}')"
        if [ $? -ne 0 ]
        then
            _ak_log_error "Could not verify signature"
            exit 1
        fi
        _ak_log_debug "$1 signed with $fingerprint"
        cat $1 | _ak_gpg 2> /dev/null
    else
        _ak_log_error "Failed to verify detached signature $1 against $2"
        exit 1
    fi
}

_ak_gpg_verify_signature(){
    if [ ! -z $1 ] && [ -n "$1" ] && [ -f "$1" ] && [ ! -z $2 ] && [ -n "$2" ] && [ -f "$2" ] 
    then
        _ak_gpg --verify $1 $2 > /dev/null 2>&1
    else
        _ak_log_error "Failed to verify detached signature $1 against $2"
        exit 1
    fi
}

_ak_gpg_key_self_export(){
    if [ ! -z $1 ] && [ -n "$1" ]
    then
        _ak_gpg --armour --output $1 --export $AK_FINGERPRINT
    else
        exit 1
    fi
}

_ak_gpg_list_keys_plain(){
    _ak_gpg --list-keys
}

_ak_gpg_list_keys(){
    _ak_gpg --list-keys | grep '^ ' | awk '{print $1}'
}

_ak_gpg_list_keys_long(){
    _ak_gpg --list-keys | \
        grep -A 1 '^ \{6\}' | \
        tr $'\n' ' ' | \
        tr '\-\-' $'\n' | \
        sed -e 's/\[//g' | \
        awk '{print $1 " " $4}' | \
        sort | \
        uniq
}

_ak_gpg_list_secret_keys_plain(){
    _ak_gpg --list-secret-keys
}

_ak_gpg_list_secret_keys(){
    _ak_gpg --list-secret-keys | grep '^ ' | awk '{print $1}'
}

_ak_gpg_list_secret_keys_long(){
    _ak_gpg --list-secret-keys | \
        grep -A 1 '^ \{6\}' | \
        tr $'\n' ' ' | \
        tr '\-\-' $'\n' | \
        sed -e 's/\[//g' | \
        awk '{print $1 " " $4}' | \
        sort | \
        uniq
}

_ak_gpg_select_key(){
    select x in $(_ak_gpg_list_secret_keys | tr '\n' ' ')
    do
        if [ -n "$x" ]
        then
            _ak_log_info "$x was selected"
            _ak_settings_set "gpg.fingerprint" "$x"
            break
        else
            _ak_log_warning "You didn't select a key"
        fi
    done
}

_ak_gpg_delete_key(){
    select x in $(_ak_gpg_list_keys | tr '\n' ' ')
    do
        if [ -n "$x" ]
        then
            _ak_log_info "$x was selected"
            _ak_gpg --delete-keys $x
            if [ $? -ne 0 ]
            then
                _ak_log_error "Some error occured while removing $x"
            else
                _ak_log_info "Key $x was deleted"
            fi
            break
        else
            _ak_log_warning "You didn't select a key"
        fi
    done
}

_ak_gpg_delete_secret_key(){
    if [ ! -z $1 ]
    then
        x="$1"
        if [ -n "$x" ]
        then
            _ak_log_info "$x was selected"
            _ak_gpg --delete-secret-keys $x
            if [ $? -ne 0 ]
            then
                _ak_log_error "Some error occured while removing $x"
                exit 1
            else
                _ak_log_info "Key $x was deleted"
            fi
            exit
        else
            _ak_log_warning "You didn't select a key"
            exit 1
        fi
    fi
}