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