File: sahpi_gcrypt_utils.c

package info (click to toggle)
openhpi 3.8.0-2.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 31,796 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (343 lines) | stat: -rw-r--r-- 14,408 bytes parent folder | download | duplicates (4)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*      
 *
 * Copyright (C) 2013, Hewlett-Packard Development Company, LLP
 *                     All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the distribution.
 *
 * Neither the name of the Hewlett-Packard Corporation, nor the names
 * of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Author(s)
 *      Mohan Devarajulu <mohan@fc.hp.com>
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <glib.h>

#include <SaHpi.h>
#include <oh_utils.h>
#include <oh_error.h>

#ifdef HAVE_ENCRYPT

SaErrorT oh_initialize_gcrypt(gcry_cipher_hd_t *h1, gcry_cipher_hd_t *h2, gchar *key)
{
        static int initialized = 0;

        if (initialized == 1)
               return(0);
      
	if ((!gcry_check_version ( NULL )) || 
            (gcry_control( GCRYCTL_DISABLE_SECMEM_WARN )) || 
	    (gcry_control( GCRYCTL_INIT_SECMEM, 16384, 0 )) ||
	    (gcry_cipher_open( h1, GCRY_CIPHER_ARCFOUR,GCRY_CIPHER_MODE_STREAM,0 )) ||
	    (gcry_cipher_open( h2, GCRY_CIPHER_ARCFOUR,GCRY_CIPHER_MODE_STREAM,0 )) ||
	    (gcry_cipher_setkey ( *h1, key, strlen(key) )) ||
            (gcry_cipher_setkey ( *h2, key, strlen(key) ))) {
                CRIT("Something went wrong with gcrypt calls");
                return(1);
        } else {
                initialized = 1;
                return(0);
        }
}


	

/******************************************************************************
 * gchar *oh_crypt(gchar *file_path, SaHpiUint32T type)
 *
 * Function to encrypt or decrypt given file. The encrypted
 * or decrypted text is returned to the caller. The caller needs to free the
 * memory. 
 * While encrypting the file, encrypted file is saved to the original file 
 * location after a check and the encrypted text is sent to the caller. While 
 * decrypting, the decrypted text is sent to the caller, but the original file 
 * is left intact.
 *
 * file_path - File name to be encrypted or decrypted. When the file is
 * encrypted, the original file is overwritten, when it is decrypted it is left 
 * as it is
 * type - Encrypt or Decrypt.
 *
 * Return: gchar *
 *         Encrypted or Decrypted string
 *
 *****************************************************************************/
gchar * oh_crypt(gchar *file_path, SaHpiUint32T type)
{
        gcry_cipher_hd_t handle1, handle2;
        int ret = 0, err=0, cwd_fd = 0;
        struct stat buf, keybuf;
        char * plain_text = NULL;
        char * ptr_substr = NULL;
        char * out = NULL;
        char * key = NULL;
        char * deout = NULL;
        long int file_size = 0, en_file_size=0;
        char  file_path_ck[PATH_MAX];
        char  file_path_en[PATH_MAX];
        char cwd[PATH_MAX];
        FILE *fconf = NULL, *fpuuid = NULL;

        /* If you can not stat then you can not access the file */
	
        if (getcwd(cwd, sizeof(cwd)) != NULL)
                cwd_fd = open(cwd, O_DIRECTORY, S_IWUSR);

        ret = faccessat(cwd_fd, file_path, R_OK, AT_EACCESS );
        if (ret != 0 )
        {
                CRIT("Can not access file %s",file_path);
                return(NULL);
        } else {
                ret = stat(file_path, &buf);
                /* Really stat call should not fail here */
                if (ret != 0 )
                {
                        CRIT("Can not stat file %s",file_path);
                        return(NULL);
                }
        }

        file_size = buf.st_size;
        if ((file_size > 1000000000) || 
            (strlen(file_path)+10 > PATH_MAX)) {
                CRIT("%s path is long or too big",file_path);
                return(NULL);
        }

        plain_text = ( char * ) g_malloc0 
			( sizeof ( char ) * ( file_size + 1) );
        fconf = fopen(file_path,"r");
        if (fconf == NULL) {
                CRIT("Unable to open %s",file_path);
                return(NULL);
        }
        fread(plain_text, 1, file_size, fconf);
        fclose(fconf);

        ret = faccessat(0, PUUID_PATH, R_OK, AT_EACCESS );
        if ( ret != 0 ) {
                if (geteuid() == 0) {
                        WARN("Could not find %s",PUUID_PATH);
                        WARN("Using the SAHPI_DEFKEY. If the above file is created");
                        WARN("ever, move the file away to decrypt");
                }
                key = ( char * ) g_malloc0 ( sizeof ( char ) * ( strlen(SAHPI_DEFKEY)+1) );
                strcpy(key,SAHPI_DEFKEY);
        } else {
                ret = stat(PUUID_PATH, &keybuf);
                /* Really stat call should not fail here */
                if (ret != 0 )
                {
                        CRIT("Can not stat file %s",PUUID_PATH);
                        return(NULL);
                }
                fpuuid=fopen(PUUID_PATH, "r");
                if (fpuuid == NULL) {
                      if (geteuid() == 0) 
                              CRIT("Unable to open %s",PUUID_PATH);
                      else
                              CRIT("Unable to open %s, Use sudo", PUUID_PATH);
                      return(NULL);
                }
                key = ( char * ) g_malloc0 ( sizeof ( char ) * ( keybuf.st_size + 1) );
                ret=fread(key, 1, keybuf.st_size, fpuuid);
                fclose(fpuuid);
        }

        if(oh_initialize_gcrypt(&handle1, &handle2, key)) {
                CRIT("Could not initialize gcrypt libraries");
                g_free(key);
                return(NULL);
        } 

        if ( type == OHPI_ENCRYPT ) {
                ptr_substr=strstr(plain_text,"handler");
                if (!ptr_substr) {
                        CRIT("Looks like the %s does not have a handler",file_path);
                        CRIT("It is either already encrypted or no plugins defined");
                        g_free(plain_text);
                        g_free(key);
                        return(NULL);
                }
                out = ( char * ) g_malloc0 ( sizeof ( char ) * ( file_size + 1 ) );
                err =  gcry_cipher_encrypt ( handle1, ( unsigned char * ) out, file_size,
                        ( const unsigned char * ) plain_text, file_size );
                if ( err )
                {
                        CRIT("Failure: %s/%s", gcry_strsource ( err ), gcry_strerror ( err ) );
                        g_free(plain_text);
                        g_free(out);
                        g_free(key);
                        return(NULL);

                }

                strcpy(file_path_en, file_path);
                strcat(file_path_en,".temp_en");
                if (access (file_path_en, F_OK) != -1 ) {
                        CRIT("Trying to use %s for temporary_file",file_path_en);
                        CRIT("That file already exists. Remove it and restart again");
                        g_free(plain_text);
                        g_free(out);
                        g_free(key);
                        return(NULL);
                }
                fconf = fopen(file_path_en,"w");
                if (fconf == NULL) {
                        CRIT("Unable to open the file for writing");
                        g_free(plain_text);
                        g_free(out);
                        g_free(key);
                        return(NULL);
                }
                err = fwrite(out, 1, file_size, fconf);
                if (err != file_size ) {
                        CRIT("Wrote %d, but expected to write %ld",err, file_size);
                        g_free(plain_text);
                        g_free(out);
                        g_free(key);
                        return(NULL);
                }
                err = 0;
                fclose(fconf);
		ret = chmod(file_path_en, buf.st_mode); /* Setting mode to original file mode */ 
                ret = stat(file_path_en, &buf);
                en_file_size = buf.st_size;
                if (file_size != en_file_size) {
                        CRIT("original file size %ld, encrypted %ld", file_size, en_file_size);
                        g_free(plain_text);
                        g_free(out);
                        g_free(key);
                        return(NULL);
                }
                memset(out,0,file_size);
                fconf = fopen(file_path_en,"r");
                fread(out,1,file_size,fconf);
                fclose(fconf);
                deout = ( char * ) g_malloc0 ( sizeof ( char ) * ( file_size + 1 ) );
                err =  gcry_cipher_decrypt ( handle2, ( unsigned char * ) deout, file_size,
                        ( const unsigned char * ) out, file_size );
                if (strcmp(deout, plain_text) != 0 || (err)) {
                        CRIT("Unable to encrypt and decrypt");
                        if(err)
                                CRIT("decrypt call failed");
                        remove(file_path_en);
                        if (strlen(file_path)+10 < 1024) {
                                strcpy(file_path_ck, file_path);
                                strcat(file_path_ck,".check");
                        }
                        fconf = fopen(file_path_ck,"w");
                        err = fwrite(deout, 1, file_size, fconf);
                        if (err != file_size ) {
                                CRIT("Wrote %d, but expected to write %ld",err, file_size);
                        }
                        CRIT("compare %s and %s. Encryption failed",file_path,file_path_ck);
                        g_free(plain_text);
                        g_free(out);
                        g_free(deout);
                        g_free(key);
                        return(NULL);
                }
                /* At this point everything is ok. Able to encrypt and save into a file.
                * the same file is read and the contents decrypted. Original and decryption
                * match. So move the encrypted file to the original location */
                g_free(plain_text);
                g_free(deout);
                err = 0;
                err = rename(file_path_en, file_path);
                if ( err != 0 ) {
                        CRIT("%s is not moved to %s",file_path_en,file_path);
                        g_free(out);
                        g_free(key);
                        return(NULL);
                } 
                g_free(key);
                return(out);
        } else if(type == OHPI_DECRYPT) {
                ptr_substr=strstr(plain_text,"handler");
                if (ptr_substr) {
                        CRIT("Looks like the %s is a plain file",file_path);
                        CRIT("It was not encrypted?. Please check");
                        g_free(plain_text);
                        g_free(key);
                        return(NULL);
                }

                out = ( char * ) g_malloc0 ( sizeof ( char ) * ( file_size + 1 ) );
                err =  gcry_cipher_decrypt ( handle2, ( unsigned char * ) out, file_size,
                        ( const unsigned char * ) plain_text, file_size );
                if ( err )
                {
                        CRIT("Failure: %s/%s", gcry_strsource ( err ), gcry_strerror ( err ) );
                        g_free(plain_text);
                        g_free(out);
                        g_free(key);
                        return(NULL);
                } else {
                        g_free(plain_text);
                        ptr_substr = NULL;
                        ptr_substr=strstr(out,"handler");
                        if ((!ptr_substr) && (geteuid() == 0)) {
                                CRIT("Looks like the decrypted texts does not have a handler");
                                CRIT("Decryption might have failed.");
                                if(!strcmp(key,SAHPI_DEFKEY))
                                       INFO("Check if %s exists",PUUID_PATH);
                                else {
                                       INFO("Try to decrypt it as a normal user/no sudo");
                                       INFO("if that fails recover from backup. Sorry");
                                }
                        } else if (!ptr_substr) {
                                CRIT("Looks like the decrypted text does not have a handler");
                                CRIT("Decryption might have failed.");
                                if(!strcmp(key,SAHPI_DEFKEY))
                                       INFO("If file is bad, Recover from backup or try sudo.");
                                else 
                                       INFO("If file is bad, Recover from backup.");
                        }  
                        g_free(key);
                        return(out);
                }
        } else {
                CRIT("Unknown Type %d",type);
                g_free(plain_text);
                g_free(key);
                return(NULL);
        }
        g_free(key);
        return(NULL);
}

#endif