LCOV - code coverage report
Current view: top level - src - openssl.c (source / functions) Hit Total Coverage
Test: oc-runner-0.0.51.5.29c69 Code Coverage Lines: 0 82 0.0 %
Date: 2018-10-15 12:44:38 Functions: 0 10 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  Copyright (c) 2017 by Danny Goossen, Gioxa Ltd.
       3             :  
       4             :  This file is part of the oc-runner
       5             :  
       6             :  MIT License
       7             :  
       8             :  Permission is hereby granted, free of charge, to any person obtaining a copy
       9             :  of this software and associated documentation files (the "Software"), to deal
      10             :  in the Software without restriction, including without limitation the rights
      11             :  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12             :  copies of the Software, and to permit persons to whom the Software is
      13             :  furnished to do so, subject to the following conditions:
      14             :  
      15             :  The above copyright notice and this permission notice shall be included in all
      16             :  copies or substantial portions of the Software.
      17             :  
      18             :  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19             :  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20             :  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21             :  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22             :  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23             :  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      24             :  SOFTWARE.
      25             :  
      26             :  */
      27             : /*! @file openssl.c
      28             :  *  @brief openssl api functions
      29             :  *  @author danny@gioxa.com
      30             :  *  @date 9 Sep 2017
      31             :  *  @copyright (c) 2017 Danny Goossen,Gioxa Ltd.
      32             :  */
      33             : 
      34             : #include "openssl.h"
      35             : #include <unistd.h>
      36             : #include <stdlib.h>
      37             : #include <pthread.h>
      38             : #include <openssl/err.h>
      39             : #include "error.h"
      40             : 
      41             : 
      42             : // For thread safety curl https
      43             : 
      44             : #define MUTEX_TYPE       pthread_mutex_t
      45             : #define MUTEX_SETUP(x)   pthread_mutex_init(&(x), NULL)
      46             : #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
      47             : #define MUTEX_LOCK(x)    pthread_mutex_lock(&(x))
      48             : #define MUTEX_UNLOCK(x)  pthread_mutex_unlock(&(x))
      49             : #define THREAD_ID        pthread_self()
      50             : 
      51             : 
      52             : /**
      53             :  This array will store all of the mutexes available to OpenSSL. 
      54             :  \see https://curl.haxx.se/libcurl/c/opensslthreadlock.html
      55             : */
      56             : static MUTEX_TYPE *mutex_buf= NULL;
      57             : 
      58             : /**
      59             :  \see https://curl.haxx.se/libcurl/c/opensslthreadlock.html
      60             :  */
      61           0 : static void locking_function(int mode, int n, __attribute__((unused)) const char *file, __attribute__((unused)) int line)
      62             : {
      63           0 :    if(mode & CRYPTO_LOCK)
      64           0 :       MUTEX_LOCK(mutex_buf[n]);
      65             :    else
      66           0 :       MUTEX_UNLOCK(mutex_buf[n]);
      67           0 : }
      68             : 
      69             : /**
      70             :  \see https://curl.haxx.se/libcurl/c/opensslthreadlock.html
      71             :  */
      72           0 : static unsigned long id_function(void)
      73             : {
      74           0 :    return ((unsigned long)THREAD_ID);
      75             : }
      76             : 
      77           0 : int thread_setup(void)
      78             : {
      79             :    int i;
      80             : 
      81           0 :    mutex_buf = malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE));
      82           0 :    if(!mutex_buf)
      83             :       return 0;
      84           0 :    for(i = 0;  i < CRYPTO_num_locks();  i++)
      85           0 :       MUTEX_SETUP(mutex_buf[i]);
      86           0 :    CRYPTO_set_id_callback(id_function);
      87           0 :    CRYPTO_set_locking_callback(locking_function);
      88           0 :    return 1;
      89             : }
      90             : 
      91           0 : int thread_cleanup(void)
      92             : {
      93             :    int i;
      94             : 
      95           0 :    if(!mutex_buf)
      96             :       return 0;
      97           0 :    CRYPTO_set_id_callback(NULL);
      98           0 :    CRYPTO_set_locking_callback(NULL);
      99           0 :    for(i = 0;  i < CRYPTO_num_locks();  i++)
     100           0 :       MUTEX_CLEANUP(mutex_buf[i]);
     101           0 :    free(mutex_buf);
     102           0 :    mutex_buf = NULL;
     103           0 :    return 1;
     104             : }
     105             : 
     106             : #include <openssl/bio.h>
     107             : #include <openssl/evp.h>
     108             : #include <openssl/buffer.h>
     109             : #include <stdint.h>
     110             : #include <stdio.h>
     111             : #include <string.h>
     112             : 
     113             : 
     114           0 : void sha256_digest_string (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[SHA256_DIGEST_LENGTH_BLOBSUM])
     115             : {
     116           0 :         int i = 0;
     117           0 :         sprintf(outputBuffer,"sha256:");
     118           0 :         size_t offset=strlen(outputBuffer);
     119           0 :         for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
     120             :         {
     121           0 :                 sprintf(outputBuffer + offset+ (i * 2),"%02x", hash[i]);
     122             :         }
     123           0 : }
     124             : 
     125           0 : void sha256_digest_string_hash (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[SHA256_DIGEST_LENGTH_HASH])
     126             : {
     127           0 :         int i = 0;
     128           0 :         for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
     129             :         {
     130           0 :                 sprintf(outputBuffer + (i * 2),"%02x", hash[i]);
     131             :         }
     132           0 : }
     133             : 
     134           0 : int calc_sha256 (const char* path, char output[SHA256_DIGEST_LENGTH_BLOBSUM])
     135             : {
     136           0 :         FILE* file = fopen(path, "rb");
     137           0 :         if(!file) return -1;
     138             :         
     139             :         unsigned char hash[SHA256_DIGEST_LENGTH];
     140             :         SHA256_CTX sha256;
     141           0 :         SHA256_Init(&sha256);
     142           0 :         const int bufSize = 32768;
     143           0 :         char* buffer = malloc(bufSize);
     144           0 :         int bytesRead = 0;
     145           0 :         if(!buffer) return -1;
     146           0 :         while((bytesRead = (int)fread(buffer, 1,bufSize, file)))
     147             :         {
     148           0 :                 SHA256_Update(&sha256, buffer, bytesRead);
     149             :         }
     150           0 :         SHA256_Final(hash, &sha256);
     151             :         
     152           0 :         sha256_digest_string(hash, output);
     153           0 :         fclose(file);
     154           0 :         free(buffer);
     155           0 :         return 0;
     156             : }
     157           0 : int calc_sha256_e (const char* path, char output[SHA256_DIGEST_LENGTH_BLOBSUM],int * len)
     158             : {
     159           0 :         FILE* file = fopen(path, "rb");
     160           0 :         if(!file) return -1;
     161           0 :         *len=0;
     162             :         unsigned char hash[SHA256_DIGEST_LENGTH];
     163             :         SHA256_CTX sha256;
     164           0 :         SHA256_Init(&sha256);
     165           0 :         const int bufSize = 32768;
     166           0 :         char* buffer = malloc(bufSize);
     167           0 :         int bytesRead = 0;
     168           0 :         if(!buffer) return -1;
     169           0 :         while((bytesRead = (int)fread(buffer, 1,bufSize, file)))
     170             :         {
     171           0 :                 (*len)=(*len)+bytesRead;
     172           0 :                 SHA256_Update(&sha256, buffer, bytesRead);
     173             :         }
     174           0 :         SHA256_Final(hash, &sha256);
     175             :         
     176           0 :         sha256_digest_string(hash, output);
     177           0 :         fclose(file);
     178           0 :         free(buffer);
     179           0 :         return 0;
     180             : }
     181             : 
     182           0 : int calc_sha256_buff (const char* buff,int size, char output[SHA256_DIGEST_LENGTH_BLOBSUM])
     183             : {
     184           0 :         if (!size) size=(int)strlen(buff);
     185             :         unsigned char hash[SHA256_DIGEST_LENGTH];
     186             :         SHA256_CTX sha256;
     187           0 :         SHA256_Init(&sha256);
     188           0 :         SHA256_Update(&sha256, buff, size);
     189             :         
     190           0 :         SHA256_Final(hash, &sha256);
     191             :         
     192           0 :         sha256_digest_string(hash, output);
     193             :         
     194           0 :         return 0;
     195             : }
     196             : 
     197           0 : int calc_sha256_digest_hash (const char* buff,int size, char output[SHA256_DIGEST_LENGTH_HASH])
     198             : {
     199           0 :         if (!size) size=(int)strlen(buff);
     200             :         unsigned char hash[SHA256_DIGEST_LENGTH];
     201             :         SHA256_CTX sha256;
     202           0 :         SHA256_Init(&sha256);
     203           0 :         SHA256_Update(&sha256, buff, size);
     204           0 :         SHA256_Final(hash, &sha256);
     205           0 :         sha256_digest_string_hash(hash, output);
     206           0 :         return 0;
     207             : }
     208             : 

Generated by: LCOV version 1.13