| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2017 Jeromy Johnson |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test keystore commands" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | |
| 11 | test_init_ipfs |
| 12 | |
| 13 | test_key_cmd() { |
| 14 | # test key output format |
| 15 | test_expect_success "create an RSA key and test B58MH/B36CID output formats" ' |
| 16 | PEERID=$(ipfs key gen --ipns-base=b58mh --type=rsa --size=2048 key_rsa) && |
| 17 | test_check_rsa2048_b58mh_peerid $PEERID && |
| 18 | ipfs key rm key_rsa && |
| 19 | PEERID=$(ipfs key gen --ipns-base=base36 --type=rsa --size=2048 key_rsa) && |
| 20 | test_check_rsa2048_base36_peerid $PEERID |
| 21 | ' |
| 22 | |
| 23 | test_expect_success "test RSA key sk export format" ' |
| 24 | ipfs key export key_rsa && |
| 25 | test_check_rsa2048_sk key_rsa.key && |
| 26 | rm key_rsa.key |
| 27 | ' |
| 28 | |
| 29 | test_expect_success "test RSA key B58MH/B36CID multihash format" ' |
| 30 | PEERID=$(ipfs key list --ipns-base=b58mh -l | grep key_rsa | head -n 1 | cut -d " " -f1) && |
| 31 | test_check_rsa2048_b58mh_peerid $PEERID && |
| 32 | PEERID=$(ipfs key list --ipns-base=base36 -l | grep key_rsa | head -n 1 | cut -d " " -f1) && |
| 33 | test_check_rsa2048_base36_peerid $PEERID && |
| 34 | ipfs key rm key_rsa |
| 35 | ' |
| 36 | |
| 37 | test_expect_success "create an ED25519 key and test B58MH/B36CID output formats" ' |
| 38 | PEERID=$(ipfs key gen --ipns-base=b58mh --type=ed25519 key_ed25519) && |
| 39 | test_check_ed25519_b58mh_peerid $PEERID && |
| 40 | ipfs key rm key_ed25519 && |
| 41 | PEERID=$(ipfs key gen --ipns-base=base36 --type=ed25519 key_ed25519) && |
| 42 | test_check_ed25519_base36_peerid $PEERID |
| 43 | ' |
| 44 | |
| 45 | test_expect_success "test ED25519 key sk export format" ' |
| 46 | ipfs key export key_ed25519 && |
| 47 | test_check_ed25519_sk key_ed25519.key && |
| 48 | rm key_ed25519.key |
| 49 | ' |
| 50 | |
| 51 | test_expect_success "test ED25519 key B58MH/B36CID multihash format" ' |
| 52 | PEERID=$(ipfs key list --ipns-base=b58mh -l | grep key_ed25519 | head -n 1 | cut -d " " -f1) && |
| 53 | test_check_ed25519_b58mh_peerid $PEERID && |
| 54 | PEERID=$(ipfs key list --ipns-base=base36 -l | grep key_ed25519 | head -n 1 | cut -d " " -f1) && |
| 55 | test_check_ed25519_base36_peerid $PEERID && |
| 56 | ipfs key rm key_ed25519 |
| 57 | ' |
| 58 | # end of format test |
| 59 | |
| 60 | |
| 61 | test_expect_success "create a new rsa key" ' |
| 62 | rsahash=$(ipfs key gen generated_rsa_key --type=rsa --size=2048) |
| 63 | echo $rsahash > rsa_key_id |
| 64 | ' |
| 65 | |
| 66 | test_key_import_export_all_formats rsa_key |
| 67 | |
| 68 | test_expect_success "create a new ed25519 key" ' |
| 69 | edhash=$(ipfs key gen generated_ed25519_key --type=ed25519) |
| 70 | echo $edhash > ed25519_key_id |
| 71 | ' |
| 72 | |
| 73 | test_key_import_export_all_formats ed25519_key |
| 74 | |
| 75 | test_openssl_compatibility_all_types |
| 76 | |
| 77 | INVALID_KEY=../t0165-keystore-data/openssl_secp384r1.pem |
| 78 | test_expect_success "import key type we don't generate fails" ' |
| 79 | test_must_fail ipfs key import restricted-type -f pem-pkcs8-cleartext $INVALID_KEY 2>&1 | tee key_exp_out && |
| 80 | grep -q "Error: key type \*crypto.ECDSAPrivateKey is not allowed to be imported" key_exp_out && |
| 81 | rm key_exp_out |
| 82 | ' |
| 83 | |
| 84 | test_expect_success "import key type we don't generate succeeds with flag" ' |
| 85 | ipfs key import restricted-type --allow-any-key-type -f pem-pkcs8-cleartext $INVALID_KEY > /dev/null && |
| 86 | ipfs key rm restricted-type |
| 87 | ' |
| 88 | |
| 89 | test_expect_success "test export file option" ' |
| 90 | ipfs key export generated_rsa_key -o=named_rsa_export_file && |
| 91 | test_cmp generated_rsa_key.key named_rsa_export_file && |
| 92 | ipfs key export generated_ed25519_key -o=named_ed25519_export_file && |
| 93 | test_cmp generated_ed25519_key.key named_ed25519_export_file |
| 94 | ' |
| 95 | |
| 96 | test_expect_success "key export can't export self" ' |
| 97 | test_must_fail ipfs key export self 2>&1 | tee key_exp_out && |
| 98 | grep -q "Error: cannot export key with name" key_exp_out && |
| 99 | test_must_fail ipfs key export self -o=selfexport 2>&1 | tee key_exp_out && |
| 100 | grep -q "Error: cannot export key with name" key_exp_out |
| 101 | ' |
| 102 | |
| 103 | test_expect_success "key import can't import self" ' |
| 104 | ipfs key gen overwrite_self_import && |
| 105 | ipfs key export overwrite_self_import && |
| 106 | test_must_fail ipfs key import self overwrite_self_import.key 2>&1 | tee key_imp_out && |
| 107 | grep -q "Error: cannot import key with name" key_imp_out && |
| 108 | ipfs key rm overwrite_self_import && |
| 109 | rm overwrite_self_import.key |
| 110 | ' |
| 111 | |
| 112 | test_expect_success "add a default key" ' |
| 113 | ipfs key gen quxel |
| 114 | ' |
| 115 | |
| 116 | test_expect_success "all keys show up in list output" ' |
| 117 | echo generated_ed25519_key > list_exp && |
| 118 | echo generated_rsa_key >> list_exp && |
| 119 | echo quxel >> list_exp && |
| 120 | echo self >> list_exp |
| 121 | ipfs key list > list_out && |
| 122 | test_sort_cmp list_exp list_out |
| 123 | ' |
| 124 | |
| 125 | test_expect_success "key hashes show up in long list output" ' |
| 126 | ipfs key list -l | grep $edhash > /dev/null && |
| 127 | ipfs key list -l | grep $rsahash > /dev/null |
| 128 | ' |
| 129 | |
| 130 | test_expect_success "key list -l contains self key with peerID" ' |
| 131 | PeerID="$(ipfs config Identity.PeerID)" |
| 132 | ipfs key list -l --ipns-base=b58mh | grep "$PeerID\s\+self" |
| 133 | ' |
| 134 | |
| 135 | test_expect_success "key rm remove a key" ' |
| 136 | ipfs key rm generated_rsa_key |
| 137 | echo generated_ed25519_key > list_exp && |
| 138 | echo quxel >> list_exp && |
| 139 | echo self >> list_exp |
| 140 | ipfs key list > list_out && |
| 141 | test_sort_cmp list_exp list_out |
| 142 | ' |
| 143 | |
| 144 | test_expect_success "key rm can't remove self" ' |
| 145 | test_must_fail ipfs key rm self 2>&1 | tee key_rm_out && |
| 146 | grep -q "Error: cannot remove key with name" key_rm_out |
| 147 | ' |
| 148 | |
| 149 | test_expect_success "key rename rename a key" ' |
| 150 | ipfs key rename generated_ed25519_key fooed |
| 151 | echo fooed > list_exp && |
| 152 | echo quxel >> list_exp && |
| 153 | echo self >> list_exp |
| 154 | ipfs key list > list_out && |
| 155 | test_sort_cmp list_exp list_out |
| 156 | ' |
| 157 | |
| 158 | test_expect_success "key rename rename key output succeeds" ' |
| 159 | key_content=$(ipfs key gen key1 --type=rsa --size=2048) && |
| 160 | ipfs key rename key1 key2 >rs && |
| 161 | echo "Key $key_content renamed to key2" >expect && |
| 162 | test_cmp rs expect |
| 163 | ' |
| 164 | |
| 165 | test_expect_success "key rename can't rename self" ' |
| 166 | test_must_fail ipfs key rename self bar 2>&1 | tee key_rename_out && |
| 167 | grep -q "Error: cannot rename key with name" key_rename_out |
| 168 | ' |
| 169 | |
| 170 | test_expect_success "key rename can't overwrite self, even with force" ' |
| 171 | test_must_fail ipfs key rename -f fooed self 2>&1 | tee key_rename_out && |
| 172 | grep -q "Error: cannot overwrite key with name" key_rename_out |
| 173 | ' |
| 174 | |
| 175 | test_launch_ipfs_daemon |
| 176 | |
| 177 | test_expect_success "online import rsa key" ' |
| 178 | ipfs key import generated_rsa_key generated_rsa_key.key > roundtrip_rsa_key_id && |
| 179 | test_cmp rsa_key_id roundtrip_rsa_key_id |
| 180 | ' |
| 181 | |
| 182 | # export works directly on the keystore present in IPFS_PATH |
| 183 | test_expect_success "prepare ed25519 key while daemon is running" ' |
| 184 | edhash=$(ipfs key gen generated_ed25519_key --type=ed25519) |
| 185 | echo $edhash > ed25519_key_id |
| 186 | ' |
| 187 | |
| 188 | test_key_import_export_all_formats ed25519_key |
| 189 | |
| 190 | test_openssl_compatibility_all_types |
| 191 | |
| 192 | test_expect_success "key export over HTTP /api/v0/key/export is not possible" ' |
| 193 | ipfs key gen nohttpexporttest_key --type=ed25519 && |
| 194 | curl -X POST -sI "http://$API_ADDR/api/v0/key/export&arg=nohttpexporttest_key" | grep -q "^HTTP/1.1 404 Not Found" |
| 195 | ' |
| 196 | |
| 197 | test_expect_success "online rotate rsa key" ' |
| 198 | test_must_fail ipfs key rotate |
| 199 | ' |
| 200 | |
| 201 | test_kill_ipfs_daemon |
| 202 | |
| 203 | } |
| 204 | |
| 205 | test_check_rsa2048_sk() { |
| 206 | sklen=$(ls -l $1 | awk '{print $5}') && |
| 207 | test "$sklen" -lt "1600" && test "$sklen" -gt "1000" || { |
| 208 | echo "Bad RSA2048 sk '$1' with len '$sklen'" |
| 209 | return 1 |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | test_check_ed25519_sk() { |
| 214 | sklen=$(ls -l $1 | awk '{print $5}') && |
| 215 | test "$sklen" -lt "100" && test "$sklen" -gt "30" || { |
| 216 | echo "Bad ED25519 sk '$1' with len '$sklen'" |
| 217 | return 1 |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | test_key_import_export_all_formats() { |
| 222 | KEY_NAME=$1 |
| 223 | test_key_import_export $KEY_NAME pem-pkcs8-cleartext |
| 224 | test_key_import_export $KEY_NAME libp2p-protobuf-cleartext |
| 225 | } |
| 226 | |
| 227 | test_key_import_export() { |
| 228 | local KEY_NAME FORMAT |
| 229 | KEY_NAME=$1 |
| 230 | FORMAT=$2 |
| 231 | ORIG_KEY="generated_$KEY_NAME" |
| 232 | if [ $FORMAT == "pem-pkcs8-cleartext" ]; then |
| 233 | FILE_EXT="pem" |
| 234 | else |
| 235 | FILE_EXT="key" |
| 236 | fi |
| 237 | |
| 238 | test_expect_success "export and import $KEY_NAME with format $FORMAT" ' |
| 239 | ipfs key export $ORIG_KEY --format=$FORMAT && |
| 240 | ipfs key rm $ORIG_KEY && |
| 241 | ipfs key import $ORIG_KEY $ORIG_KEY.$FILE_EXT --format=$FORMAT > imported_key_id && |
| 242 | test_cmp ${KEY_NAME}_id imported_key_id |
| 243 | ' |
| 244 | } |
| 245 | |
| 246 | # Test the entire import/export cycle with a openssl-generated key. |
| 247 | # 1. Import openssl key with PEM format. |
| 248 | # 2. Export key with libp2p format. |
| 249 | # 3. Reimport key. |
| 250 | # 4. Now exported with PEM format. |
| 251 | # 5. Compare with original openssl key. |
| 252 | # 6. Clean up. |
| 253 | test_openssl_compatibility() { |
| 254 | local KEY_NAME FORMAT |
| 255 | KEY_NAME=$1 |
| 256 | |
| 257 | test_expect_success "import and export $KEY_NAME with all formats" ' |
| 258 | ipfs key import test-openssl -f pem-pkcs8-cleartext $KEY_NAME > /dev/null && |
| 259 | ipfs key export test-openssl -f libp2p-protobuf-cleartext -o $KEY_NAME.libp2p.key && |
| 260 | ipfs key rm test-openssl && |
| 261 | |
| 262 | ipfs key import test-openssl -f libp2p-protobuf-cleartext $KEY_NAME.libp2p.key > /dev/null && |
| 263 | ipfs key export test-openssl -f pem-pkcs8-cleartext -o $KEY_NAME.ipfs-exported.pem && |
| 264 | ipfs key rm test-openssl && |
| 265 | |
| 266 | test_cmp $KEY_NAME $KEY_NAME.ipfs-exported.pem && |
| 267 | |
| 268 | rm $KEY_NAME.libp2p.key && |
| 269 | rm $KEY_NAME.ipfs-exported.pem |
| 270 | ' |
| 271 | } |
| 272 | |
| 273 | test_openssl_compatibility_all_types() { |
| 274 | test_openssl_compatibility ../t0165-keystore-data/openssl_ed25519.pem |
| 275 | test_openssl_compatibility ../t0165-keystore-data/openssl_rsa.pem |
| 276 | } |
| 277 | |
| 278 | |
| 279 | test_key_cmd |
| 280 | |
| 281 | test_done |