1 /* 2 * iperf, Copyright (c) 2020, The Regents of the University of 3 * California, through Lawrence Berkeley National Laboratory (subject 4 * to receipt of any required approvals from the U.S. Dept. of 5 * Energy). All rights reserved. 6 * 7 * If you have questions about your rights to use or distribute this 8 * software, please contact Berkeley Lab's Technology Transfer 9 * Department at [email protected]. 10 * 11 * NOTICE. This software is owned by the U.S. Department of Energy. 12 * As such, the U.S. Government has been granted for itself and others 13 * acting on its behalf a paid-up, nonexclusive, irrevocable, 14 * worldwide license in the Software to reproduce, prepare derivative 15 * works, and perform publicly and display publicly. Beginning five 16 * (5) years after the date permission to assert copyright is obtained 17 * from the U.S. Department of Energy, and subject to any subsequent 18 * five (5) year renewals, the U.S. Government is granted for itself 19 * and others acting on its behalf a paid-up, nonexclusive, 20 * irrevocable, worldwide license in the Software to reproduce, 21 * prepare derivative works, distribute copies to the public, perform 22 * publicly and display publicly, and to permit others to do so. 23 * 24 * This code is distributed under a BSD style license, see the LICENSE 25 * file for complete information. 26 */ 27 #include "iperf_config.h" 28 29 #include <assert.h> 30 #ifdef HAVE_STDINT_H 31 #include <stdint.h> 32 #endif 33 #include <stdio.h> 34 #include <string.h> 35 36 #include "iperf.h" 37 #include "iperf_api.h" 38 #if defined(HAVE_SSL) 39 #include "iperf_auth.h" 40 #endif /* HAVE_SSL */ 41 42 #include "version.h" 43 44 #include "units.h" 45 46 #if defined(HAVE_SSL) 47 int test_authtoken(const char *authUser, const char *authPassword, EVP_PKEY *pubkey, EVP_PKEY *privkey); 48 49 int 50 main(int argc, char **argv) 51 { 52 /* sha256 */ 53 void sha256(const char *string, char outputBuffer[65]); 54 const char sha256String[] = "This is a SHA256 test."; 55 const char sha256Digest[] = "4816482f8b4149f687a1a33d61a0de6b611364ec0fb7adffa59ff2af672f7232"; /* echo -n "This is a SHA256 test." | shasum -a256 */ 56 char sha256Output[65]; 57 58 sha256(sha256String, sha256Output); 59 assert(strcmp(sha256Output, sha256Digest) == 0); 60 61 /* Base64{Encode,Decode} */ 62 int Base64Encode(const unsigned char* buffer, const size_t length, char** b64text); 63 int Base64Decode(const char* b64message, unsigned char** buffer, size_t* length); 64 const char base64String[] = "This is a Base64 test."; 65 char *base64Text; 66 char *base64Decode; 67 size_t base64DecodeLength; 68 const char base64EncodeCheck[] = "VGhpcyBpcyBhIEJhc2U2NCB0ZXN0Lg=="; /* echo -n "This is a Base64 test." | b64encode -r - */ 69 70 assert(Base64Encode((unsigned char *) base64String, strlen(base64String), &base64Text) == 0); 71 assert(strcmp(base64Text, base64EncodeCheck) == 0); 72 assert(Base64Decode(base64Text, (unsigned char **) &base64Decode, &base64DecodeLength) == 0); 73 assert(strcmp(base64String, base64Decode) == 0); 74 75 /* public/private key tests */ 76 const char *pubkeyfile = "public.pem"; 77 const char *privkeyfile = "private.pem"; 78 79 /* built-in tests */ 80 assert(test_load_pubkey_from_file(pubkeyfile) == 0); 81 assert(test_load_private_key_from_file(privkeyfile) == 0); 82 83 /* load public key pair for use in further tests */ 84 EVP_PKEY *pubkey, *privkey; 85 pubkey = load_pubkey_from_file(pubkeyfile); 86 assert(pubkey); 87 privkey = load_privkey_from_file(privkeyfile); 88 assert(privkey); 89 90 /* authentication token tests */ 91 assert(test_authtoken("kilroy", "fubar", pubkey, privkey) == 0); 92 93 /* This should fail because the data is way too long for the RSA key */ 94 /* assert(test_authtoken("kilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroy", "fubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubar", pubkey, privkey) < 0); */ 95 96 return 0; 97 } 98 99 int 100 test_authtoken(const char *authUser, const char *authPassword, EVP_PKEY *pubkey, EVP_PKEY *privkey) { 101 char *authToken; 102 char *decodeUser; 103 char *decodePassword; 104 time_t decodeTime; 105 106 assert(encode_auth_setting(authUser, authPassword, pubkey, &authToken) == 0); 107 assert(decode_auth_setting(0, authToken, privkey, &decodeUser, &decodePassword, &decodeTime) == 0); 108 109 assert(strcmp(decodeUser, authUser) == 0); 110 assert(strcmp(decodePassword, authPassword) == 0); 111 112 time_t now = time(NULL); 113 114 assert(now - decodeTime >= 0); /* time has to go forwards */ 115 assert(now - decodeTime <= 1); /* shouldn't take more than a second to run */ 116 117 return 0; 118 } 119 #else 120 int 121 main(int argc, char **argv) 122 { 123 return 0; 124 } 125 #endif /* HAVE_SSL */ 126