function_name
stringlengths
1
80
function
stringlengths
14
10.6k
comment
stringlengths
12
8.02k
normalized_comment
stringlengths
6
6.55k
FreeCipher
void FreeCipher(CIPHER *c) { // Validate arguments if (c == NULL) { return; } if (c->Ctx != NULL) { #if OPENSSL_VERSION_NUMBER >= 0x10100000L EVP_CIPHER_CTX_free(c->Ctx); #else EVP_CIPHER_CTX_cleanup(c->Ctx); Free(c->Ctx); #endif } Free(c); }
/* Release of the cipher object*/
Release of the cipher object
RsaPublicSize
UINT RsaPublicSize(K *k) { BUF *b; UINT ret; b = RsaPublicToBuf(k); if (b == NULL) { return 0; } ret = b->Size; FreeBuf(b); return ret; }
/* Get public key size*/
Get public key size
HashPtrToUINT
UINT HashPtrToUINT(void *p) { UCHAR hash_data[MD5_SIZE]; UINT ret; // Validate arguments if (p == NULL) { return 0; } Md5(hash_data, &p, sizeof(p)); Copy(&ret, hash_data, sizeof(ret)); return ret; }
/* Hash a pointer to a 32-bit*/
Hash a pointer to a 32-bit
CopyName
NAME *CopyName(NAME *n) { // Validate arguments if (n == NULL) { return NULL; } return NewName(n->CommonName, n->Organization, n->Unit, n->Country, n->State, n->Local); }
/* Copy of the NAME*/
Copy of the NAME
BinToBigNum
BIGNUM *BinToBigNum(void *data, UINT size) { BIGNUM *bn; // Validate arguments if (data == NULL) { return NULL; } bn = BN_new(); BN_bin2bn(data, size, bn); return bn; }
/* Convert the binary to the BIGNUM*/
Convert the binary to the BIGNUM
BigNumToBuf
BUF *BigNumToBuf(const BIGNUM *bn) { UINT size; UCHAR *tmp; BUF *b; // Validate arguments if (bn == NULL) { return NULL; } size = BN_num_bytes(bn); tmp = ZeroMalloc(size); BN_bn2bin(bn, tmp); b = NewBuf(); WriteBuf(b, tmp, size); Free(tmp); SeekBuf(b, 0, 0); return b; }
/* Convert a BIGNUM to a buffer*/
Convert a BIGNUM to a buffer
OpenSSL_Id
static void OpenSSL_Id(CRYPTO_THREADID *id) { CRYPTO_THREADID_set_numeric(id, (unsigned long)ThreadId()); }
/* Return the thread ID*/
Return the thread ID
OpenSSL_InitLock
void OpenSSL_InitLock() { #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) UINT i; // Initialization of the lock object ssl_lock_num = CRYPTO_num_locks(); ssl_lock_obj = Malloc(sizeof(LOCK *) * ssl_lock_num); for (i = 0;i < ssl_lock_num;i++) { ssl_lock_obj[i] = NewLock(); } // Setting the lock function CRYPTO_set_locking_callback(OpenSSL_Lock); CRYPTO_THREADID_set_callback(OpenSSL_Id); #endif }
/* Initialization of the lock of OpenSSL*/
Initialization of the lock of OpenSSL
OpenSSL_FreeLock
void OpenSSL_FreeLock() { #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) UINT i; for (i = 0;i < ssl_lock_num;i++) { DeleteLock(ssl_lock_obj[i]); } Free(ssl_lock_obj); ssl_lock_obj = NULL; CRYPTO_set_locking_callback(NULL); CRYPTO_THREADID_set_callback(NULL); #endif }
/* Release of the lock of OpenSSL*/
Release of the lock of OpenSSL
OpenSSL_Lock
void OpenSSL_Lock(int mode, int n, const char *file, int line) { #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) LOCK *lock = ssl_lock_obj[n]; if (mode & CRYPTO_LOCK) { // Lock Lock(lock); } else { // Unlock Unlock(lock); } #endif }
/* Lock function for OpenSSL*/
Lock function for OpenSSL
GetPrintNameFromX
void GetPrintNameFromX(wchar_t *str, UINT size, X *x) { // Validate arguments if (x == NULL || str == NULL) { return; } GetPrintNameFromName(str, size, x->subject_name); }
/* Get the display name of the certificate*/
Get the display name of the certificate
CloneK
K *CloneK(K *k) { BUF *b; K *ret; // Validate arguments if (k == NULL) { return NULL; } b = KToBuf(k, false, NULL); if (b == NULL) { return NULL; } ret = BufToK(b, k->private_key, false, NULL); FreeBuf(b); return ret; }
/* Clone of the key*/
Clone of the key
CloneX
X *CloneX(X *x) { BUF *b; X *ret; // Validate arguments if (x == NULL) { return NULL; } b = XToBuf(x, false); if (b == NULL) { return NULL; } ret = BufToX(b, false); FreeBuf(b); return ret; }
/* Clone of certificate*/
Clone of certificate
NewP12
P12 *NewP12(X *x, K *k, char *password) { PKCS12 *pkcs12; P12 *p12; // Validate arguments if (x == NULL || k == NULL) { return false; } if (password && StrLen(password) == 0) { password = NULL; } Lock(openssl_lock); { pkcs12 = PKCS12_create(password, NULL, k->pkey, x->x509, NULL, 0, 0, 0, 0, 0); if (pkcs12 == NULL) { Unlock(openssl_lock); return NULL; } } Unlock(openssl_lock); p12 = PKCS12ToP12(pkcs12); return p12; }
/* Generate a P12*/
Generate a P12
IsEncryptedP12
bool IsEncryptedP12(P12 *p12) { X *x; K *k; // Validate arguments if (p12 == NULL) { return false; } if (ParseP12(p12, &x, &k, NULL) == true) { FreeX(x); FreeK(k); return false; } return true; }
/* Check whether the P12 is encrypted*/
Check whether the P12 is encrypted
P12ToFileW
bool P12ToFileW(P12 *p12, wchar_t *filename) { BUF *b; // Validate arguments if (p12 == NULL || filename == NULL) { return false; } b = P12ToBuf(p12); if (b == NULL) { return false; } if (DumpBufW(b, filename) == false) { FreeBuf(b); return false; } FreeBuf(b); return true; }
/* Write the P12 to a file*/
Write the P12 to a file
FreeP12
void FreeP12(P12 *p12) { // Validate arguments if (p12 == NULL) { return; } FreePKCS12(p12->pkcs12); Free(p12); }
/* Release of P12*/
Release of P12
FreePKCS12
void FreePKCS12(PKCS12 *pkcs12) { // Validate arguments if (pkcs12 == NULL) { return; } PKCS12_free(pkcs12); }
/* Release of PKCS12*/
Release of PKCS12
P12ToBuf
BUF *P12ToBuf(P12 *p12) { BIO *bio; BUF *buf; // Validate arguments if (p12 == NULL) { return NULL; } bio = P12ToBio(p12); if (bio == NULL) { return NULL; } buf = BioToBuf(bio); FreeBio(bio); SeekBuf(buf, 0, 0); return buf; }
/* Converted the P12 to a BUF*/
Converted the P12 to a BUF
P12ToBio
BIO *P12ToBio(P12 *p12) { BIO *bio; // Validate arguments if (p12 == NULL) { return NULL; } bio = NewBio(); Lock(openssl_lock); { i2d_PKCS12_bio(bio, p12->pkcs12); } Unlock(openssl_lock); return bio; }
/* Converted the P12 to a BIO*/
Converted the P12 to a BIO
BufToP12
P12 *BufToP12(BUF *b) { P12 *p12; BIO *bio; // Validate arguments if (b == NULL) { return NULL; } bio = BufToBio(b); if (bio == NULL) { return NULL; } p12 = BioToP12(bio); FreeBio(bio); return p12; }
/* Read the P12 from the BUF*/
Read the P12 from the BUF
BioToP12
P12 *BioToP12(BIO *bio) { PKCS12 *pkcs12; // Validate arguments if (bio == NULL) { return NULL; } // Conversion Lock(openssl_lock); { pkcs12 = d2i_PKCS12_bio(bio, NULL); } Unlock(openssl_lock); if (pkcs12 == NULL) { // Failure return NULL; } return PKCS12ToP12(pkcs12); }
/* Read the P12 from the BIO*/
Read the P12 from the BIO
PKCS12ToP12
P12 *PKCS12ToP12(PKCS12 *pkcs12) { P12 *p12; // Validate arguments if (pkcs12 == NULL) { return NULL; } p12 = ZeroMalloc(sizeof(P12)); p12->pkcs12 = pkcs12; return p12; }
/* Generate a P12 from a PKCS12*/
Generate a P12 from a PKCS12
FreeXSerial
void FreeXSerial(X_SERIAL *serial) { // Validate arguments if (serial == NULL) { return; } Free(serial->data); Free(serial); }
/* Release of X_SERIAL*/
Release of X_SERIAL
CompareXSerial
bool CompareXSerial(X_SERIAL *s1, X_SERIAL *s2) { // Validate arguments if (s1 == NULL || s2 == NULL) { return false; } if (s1->size != s2->size) { return false; } if (Cmp(s1->data, s2->data, s1->size) != 0) { return false; } return true; }
/* Comparison of X_SERIAL*/
Comparison of X_SERIAL
CloneXSerial
X_SERIAL *CloneXSerial(X_SERIAL *src) { X_SERIAL *s; // Validate arguments if (src == NULL) { return NULL; } s = ZeroMalloc(sizeof(X_SERIAL)); s->data = ZeroMalloc(src->size); Copy(s->data, src->data, src->size); s->size = src->size; return s; }
/* Copy of X_SERIAL*/
Copy of X_SERIAL
NewXSerial
X_SERIAL *NewXSerial(void *data, UINT size) { X_SERIAL *serial; UCHAR *buf = (UCHAR *)data; UINT i; // Validate arguments if (data == NULL || size == 0) { return NULL; } for (i = 0;i < size;i++) { if (buf[i] != 0) { break; } } if (i == size) { i = size - 1; } buf += i; serial = Malloc(sizeof(X_SERIAL)); serial->size = size - i; serial->data = ZeroMalloc(size + 16); Copy(serial->data, buf, size - i); return serial; }
/* Initialization of X_SERIAL*/
Initialization of X_SERIAL
GetDaysUntil2038
UINT GetDaysUntil2038() { UINT64 now = SystemTime64(); UINT64 target; SYSTEMTIME st; Zero(&st, sizeof(st)); st.wYear = 2038; st.wMonth = 1; st.wDay = 1; target = SystemToUINT64(&st); if (now >= target) { return 0; } else { return (UINT)((target - now) / (UINT64)(1000 * 60 * 60 * 24)); } }
/* Get the number of days till January 1, 2038*/
Get the number of days till January 1, 2038
NewX
X *NewX(K *pub, K *priv, X *ca, NAME *name, UINT days, X_SERIAL *serial) { X509 *x509; X *x; // Validate arguments if (pub == NULL || priv == NULL || name == NULL || ca == NULL) { return NULL; } x509 = NewX509(pub, priv, ca, name, days, serial); if (x509 == NULL) { return NULL; } x = X509ToX(x509); if (x == NULL) { return NULL; } return x; }
/* Issue an X509 certificate*/
Issue an X509 certificate
NewRootX
X *NewRootX(K *pub, K *priv, NAME *name, UINT days, X_SERIAL *serial) { X509 *x509; X *x, *x2; // Validate arguments if (pub == NULL || priv == NULL || name == NULL) { return NULL; } x509 = NewRootX509(pub, priv, name, days, serial); if (x509 == NULL) { return NULL; } x = X509ToX(x509); if (x == NULL) { return NULL; } x2 = CloneX(x); FreeX(x); return x2; }
/* Create a root certificate*/
Create a root certificate
AddKeyUsageX509
void AddKeyUsageX509(EXTENDED_KEY_USAGE *ex, int nid) { ASN1_OBJECT *obj; // Validate arguments if (ex == NULL) { return; } obj = OBJ_nid2obj(nid); if (obj != NULL) { sk_ASN1_OBJECT_push(ex, obj); } }
/* Create new X509 basic & extended key usage*/
Create new X509 basic & extended key usage
NameToX509Name
void *NameToX509Name(NAME *nm) { X509_NAME *xn; // Validate arguments if (nm == NULL) { return NULL; } xn = X509_NAME_new(); if (xn == NULL) { return NULL; } // Add the entries AddX509Name(xn, NID_commonName, nm->CommonName); AddX509Name(xn, NID_organizationName, nm->Organization); AddX509Name(xn, NID_organizationalUnitName, nm->Unit); AddX509Name(xn, NID_countryName, nm->Country); AddX509Name(xn, NID_stateOrProvinceName, nm->State); AddX509Name(xn, NID_localityName, nm->Local); return xn; }
/* Convert the NAMEto a X509_NAME*/
Convert the NAMEto a X509_NAME
FreeX509Name
void FreeX509Name(void *xn) { X509_NAME *x509_name; // Validate arguments if (xn == NULL) { return; } x509_name = (X509_NAME *)xn; X509_NAME_free(x509_name); }
/* Release the X509_NAME*/
Release the X509_NAME
CheckXDateNow
bool CheckXDateNow(X *x) { // Validate arguments if (x == NULL) { return false; } return CheckXDate(x, SystemTime64()); }
/* Check the expiration date of the certificate by the current time*/
Check the expiration date of the certificate by the current time
CheckXDate
bool CheckXDate(X *x, UINT64 current_system_time) { // Validate arguments if (x == NULL) { return false; } if (x->notBefore >= current_system_time || x->notAfter <= current_system_time) { return false; } return true; }
/* Check the expiration date of the certificate*/
Check the expiration date of the certificate
LoadXDates
void LoadXDates(X *x) { // Validate arguments if (x == NULL) { return; } x->notBefore = Asn1TimeToUINT64((ASN1_TIME *)X509_get0_notBefore(x->x509)); x->notAfter = Asn1TimeToUINT64((ASN1_TIME *)X509_get0_notAfter(x->x509)); }
/* Read the expiration date of the certificate*/
Read the expiration date of the certificate
UINT64ToAsn1Time
bool UINT64ToAsn1Time(void *asn1_time, UINT64 t) { SYSTEMTIME st; // Validate arguments if (asn1_time == NULL) { return false; } UINT64ToSystem(&st, t); return SystemToAsn1Time(asn1_time, &st); }
/* Convert the 64bit system time to ASN1 time*/
Convert the 64bit system time to ASN1 time
SystemToStr
bool SystemToStr(char *str, UINT size, SYSTEMTIME *s) { // Validate arguments if (str == NULL || s == NULL) { return false; } Format(str, size, "%02u%02u%02u%02u%02u%02uZ", s->wYear % 100, s->wMonth, s->wDay, s->wHour, s->wMinute, s->wSecond); return true; }
/* Convert the system time to a string*/
Convert the system time to a string
Asn1TimeToUINT64
UINT64 Asn1TimeToUINT64(void *asn1_time) { SYSTEMTIME st; // Validate arguments if (asn1_time == NULL) { return 0; } if (Asn1TimeToSystem(&st, asn1_time) == false) { return 0; } return SystemToUINT64(&st); }
/* Convert an ASN1 time to an UINT64 time*/
Convert an ASN1 time to an UINT64 time
Asn1TimeToSystem
bool Asn1TimeToSystem(SYSTEMTIME *s, void *asn1_time) { ASN1_TIME *t; // Validate arguments if (s == NULL || asn1_time == NULL) { return false; } t = (ASN1_TIME *)asn1_time; if (StrToSystem(s, (char *)t->data) == false) { return false; } if (t->type == V_ASN1_GENERALIZEDTIME) { LocalToSystem(s, s); } return true; }
/* Converted an ASN1 time to a system time*/
Converted an ASN1 time to a system time
RsaVerify
bool RsaVerify(void *data, UINT data_size, void *sign, K *k) { return RsaVerifyEx(data, data_size, sign, k, 0); }
/* Verify the RSA signature*/
Verify the RSA signature
HashForSign
bool HashForSign(void *dst, UINT dst_size, void *src, UINT src_size) { UCHAR *buf = (UCHAR *)dst; UCHAR sign_data[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14, }; // Validate arguments if (dst == NULL || src == NULL || src_size == 0 || MIN_SIGN_HASH_SIZE > dst_size) { return false; } // Header part Copy(buf, sign_data, sizeof(sign_data)); // Hash Sha1(HASHED_DATA(buf), src, src_size); return true; }
/* Generation of signature data by SHA-1*/
Generation of signature data by SHA-1
RsaCheckEx
bool RsaCheckEx() { UINT num = 20; UINT i; for (i = 0;i < num;i++) { if (RsaCheck()) { return true; } SleepThread(100); } return false; }
/* RSA operating environment check*/
RSA operating environment check
CheckXEx
bool CheckXEx(X *x, X *x_issuer, bool check_name, bool check_date) { K *k; bool ret; // Validate arguments if (x == NULL || x_issuer == NULL) { return false; } k = GetKFromX(x_issuer); if (k == NULL) { return false; } ret = CheckSignature(x, k); if (ret) { if (check_name) { if (CompareName(x->issuer_name, x_issuer->subject_name) == false) { ret = false; } } if (check_date) { if (CheckXDateNow(x_issuer) == false) { ret = false; } } } FreeK(k); return ret; }
/* Confirm whether the certificate X is signed by the issuer of the certificate x_issuer*/
Confirm whether the certificate X is signed by the issuer of the certificate x_issuer
CheckSignature
bool CheckSignature(X *x, K *k) { // Validate arguments if (x == NULL || k == NULL) { return false; } Lock(openssl_lock); { if (X509_verify(x->x509, k->pkey) == 0) { Unlock(openssl_lock); return false; } } Unlock(openssl_lock); return true; }
/* Confirm the signature of the certificate X with the public key K*/
Confirm the signature of the certificate X with the public key K
GetKFromX
K *GetKFromX(X *x) { EVP_PKEY *pkey; K *k; // Validate arguments if (x == NULL) { return NULL; } Lock(openssl_lock); { pkey = X509_get_pubkey(x->x509); } Unlock(openssl_lock); if (pkey == NULL) { return NULL; } k = ZeroMalloc(sizeof(K)); k->pkey = pkey; return k; }
/* Get the public key from the certificate*/
Get the public key from the certificate
CompareName
bool CompareName(NAME *n1, NAME *n2) { // Validate arguments if (n1 == NULL || n2 == NULL) { return false; } // Name comparison if (UniStrCmpi(n1->CommonName, n2->CommonName) == 0 && UniStrCmpi(n1->Organization, n2->Organization) == 0 && UniStrCmpi(n1->Unit, n2->Unit) == 0 && UniStrCmpi(n1->Country, n2->Country) == 0 && UniStrCmpi(n1->State, n2->State) == 0 && UniStrCmpi(n1->Local, n2->Local) == 0) { return true; } return false; }
/* The name comparison*/
The name comparison
FreeXNames
void FreeXNames(X *x) { // Validate arguments if (x == NULL) { return; } FreeName(x->issuer_name); x->issuer_name = NULL; FreeName(x->subject_name); x->subject_name = NULL; }
/* Release the name of the X*/
Release the name of the X
FreeName
void FreeName(NAME *n) { // Validate arguments if (n == NULL) { return; } // Release the string Free(n->CommonName); Free(n->Organization); Free(n->Unit); Free(n->Country); Free(n->State); Free(n->Local); // Release the object Free(n); return; }
/* Release the name*/
Release the name
LoadXNames
void LoadXNames(X *x) { X509 *x509; // Validate arguments if (x == NULL) { return; } x509 = x->x509; x->issuer_name = X509NameToName(X509_get_issuer_name(x509)); x->subject_name = X509NameToName(X509_get_subject_name(x509)); }
/* Get the name of the certificate*/
Get the name of the certificate
X509NameToName
NAME *X509NameToName(void *xn) { NAME *n; // Validate arguments if (xn == NULL) { return NULL; } n = ZeroMalloc(sizeof(NAME)); // Get the strings one by one n->CommonName = GetUniStrFromX509Name(xn, NID_commonName); n->Organization = GetUniStrFromX509Name(xn, NID_organizationName); n->Unit = GetUniStrFromX509Name(xn, NID_organizationalUnitName); n->Country = GetUniStrFromX509Name(xn, NID_countryName); n->State = GetUniStrFromX509Name(xn, NID_stateOrProvinceName); n->Local = GetUniStrFromX509Name(xn, NID_localityName); return n; }
/* Convert the X509_NAME structure to the NAME structure*/
Convert the X509_NAME structure to the NAME structure
CompareX
bool CompareX(X *x1, X *x2) { // Validate arguments if (x1 == NULL || x2 == NULL) { return false; } Lock(openssl_lock); if (X509_cmp(x1->x509, x2->x509) == 0) { Unlock(openssl_lock); return true; } else { Unlock(openssl_lock); return false; } }
/* Check whether the certificate x1 equal to x2*/
Check whether the certificate x1 equal to x2
CheckXandK
bool CheckXandK(X *x, K *k) { // Validate arguments if (x == NULL || k == NULL) { return false; } Lock(openssl_lock); if (X509_check_private_key(x->x509, k->pkey) != 0) { Unlock(openssl_lock); return true; } else { Unlock(openssl_lock); return false; } }
/* Check whether K is private key of X*/
Check whether K is private key of X
FileToX
X *FileToX(char *filename) { wchar_t *filename_w = CopyStrToUni(filename); X *ret = FileToXW(filename_w); Free(filename_w); return ret; }
/* Read a X from the file*/
Read a X from the file
XToFile
bool XToFile(X *x, char *filename, bool text) { wchar_t *filename_w = CopyStrToUni(filename); bool ret = XToFileW(x, filename_w, text); Free(filename_w); return ret; }
/* Write the X to a file*/
Write the X to a file
FileToKW
K *FileToKW(wchar_t *filename, bool private_key, char *password) { bool text; BUF *b; K *k; // Validate arguments if (filename == NULL) { return NULL; } b = ReadDumpW(filename); if (b == NULL) { return NULL; } text = IsBase64(b); if (text == false) { k = BufToK(b, private_key, false, NULL); } else { k = BufToK(b, private_key, true, NULL); if (k == NULL) { k = BufToK(b, private_key, true, password); } } FreeBuf(b); return k; }
/* Read a K from the file*/
Read a K from the file
KToFileW
bool KToFileW(K *k, wchar_t *filename, bool text, char *password) { BUF *b; bool ret; // Validate arguments if (k == NULL || filename == NULL) { return false; } b = KToBuf(k, text, password); if (b == NULL) { return false; } ret = DumpBufW(b, filename); FreeBuf(b); return ret; }
/* Save the K to a file*/
Save the K to a file
KToBuf
BUF *KToBuf(K *k, bool text, char *password) { BUF *buf; BIO *bio; // Validate arguments if (k == NULL) { return NULL; } bio = KToBio(k, text, password); if (bio == NULL) { return NULL; } buf = BioToBuf(bio); FreeBio(bio); SeekBuf(buf, 0, 0); return buf; }
/* Convert the K to the BUF*/
Convert the K to the BUF
IsEncryptedK
bool IsEncryptedK(BUF *b, bool private_key) { K *k; // Validate arguments if (b == NULL) { return false; } if (IsBase64(b) == false) { return false; } k = BufToK(b, private_key, true, NULL); if (k != NULL) { FreeK(k); return false; } return true; }
/* Check whether the K in the BUF is encrypted*/
Check whether the K in the BUF is encrypted
BufToK
K *BufToK(BUF *b, bool private_key, bool text, char *password) { BIO *bio; K *k; // Validate arguments if (b == NULL) { return NULL; } bio = BufToBio(b); k = BioToK(bio, private_key, text, password); FreeBio(bio); return k; }
/* Convert the BUF to a K*/
Convert the BUF to a K
FreeK
void FreeK(K *k) { // Validate arguments if (k == NULL) { return; } FreePKey(k->pkey); Free(k); }
/* Release of K*/
Release of K
FreePKey
void FreePKey(EVP_PKEY *pkey) { // Validate arguments if (pkey == NULL) { return; } EVP_PKEY_free(pkey); }
/* Release the secret key*/
Release the secret key
PKeyPasswordCallbackFunction
int PKeyPasswordCallbackFunction(char *buf, int bufsize, int verify, void *param) { CB_PARAM *cb; // Validate arguments if (buf == NULL || param == NULL || bufsize == 0) { return 0; } cb = (CB_PARAM *)param; if (cb->password == NULL) { return 0; } return StrCpy(buf, bufsize, cb->password); }
/* Password callback function*/
Password callback function
XToBuf
BUF *XToBuf(X *x, bool text) { BIO *bio; BUF *b; // Validate arguments if (x == NULL) { return NULL; } bio = XToBio(x, text); if (bio == NULL) { return NULL; } b = BioToBuf(bio); FreeBio(bio); SeekBuf(b, 0, 0); return b; }
/* Convert the X to a BUF*/
Convert the X to a BUF
XToBio
BIO *XToBio(X *x, bool text) { BIO *bio; // Validate arguments if (x == NULL) { return NULL; } bio = NewBio(); Lock(openssl_lock); { if (text == false) { // Binary format i2d_X509_bio(bio, x->x509); } else { // Text format PEM_write_bio_X509(bio, x->x509); } } Unlock(openssl_lock); return bio; }
/* Convert the X to a BIO*/
Convert the X to a BIO
FreeX
void FreeX(X *x) { // Validate arguments if (x == NULL) { return; } // Release the name FreeXNames(x); // Release the Serial FreeXSerial(x->serial); if (x->do_not_free == false) { FreeX509(x->x509); } Free(x); }
/* Release of the X*/
Release of the X
FreeX509
void FreeX509(X509 *x509) { // Validate arguments if (x509 == NULL) { return; } Lock(openssl_lock); { X509_free(x509); } Unlock(openssl_lock); }
/* Release of the X509*/
Release of the X509
BufToX
X *BufToX(BUF *b, bool text) { X *x; BIO *bio; // Validate arguments if (b == NULL) { return NULL; } bio = BufToBio(b); if (bio == NULL) { FreeBuf(b); return NULL; } x = BioToX(bio, text); FreeBio(bio); return x; }
/* Convert the BUF to a X*/
Convert the BUF to a X
GetXDigest
void GetXDigest(X *x, UCHAR *buf, bool sha1) { // Validate arguments if (x == NULL) { return; } if (sha1 == false) { UINT size = MD5_SIZE; X509_digest(x->x509, EVP_md5(), buf, (unsigned int *)&size); } else { UINT size = SHA1_SIZE; X509_digest(x->x509, EVP_sha1(), buf, (unsigned int *)&size); } }
/* Get a digest of the X*/
Get a digest of the X
BioToX
X *BioToX(BIO *bio, bool text) { X *x; X509 *x509; // Validate arguments if (bio == NULL) { return NULL; } Lock(openssl_lock); { // Reading x509 if (text == false) { // Binary mode x509 = d2i_X509_bio(bio, NULL); } else { // Text mode x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); } } Unlock(openssl_lock); if (x509 == NULL) { return NULL; } x = X509ToX(x509); if (x == NULL) { return NULL; } return x; }
/* Convert BIO to X*/
Convert BIO to X
NewBio
BIO *NewBio() { return BIO_new(BIO_s_mem()); }
/* Create a BIO*/
Create a BIO
FreeBio
void FreeBio(BIO *bio) { // Validate arguments if (bio == NULL) { return; } BIO_free(bio); }
/* Release the BIO*/
Release the BIO
BioToBuf
BUF *BioToBuf(BIO *bio) { BUF *b; UINT size; void *tmp; // Validate arguments if (bio == NULL) { return NULL; } BIO_seek(bio, 0); size = (UINT)BIO_number_written(bio); tmp = Malloc(size); BIO_read(bio, tmp, size); b = NewBuf(); WriteBuf(b, tmp, size); Free(tmp); return b; }
/* Convert the BIO to the BUF*/
Convert the BIO to the BUF
BufToBio
BIO *BufToBio(BUF *b) { BIO *bio; // Validate arguments if (b == NULL) { return NULL; } Lock(openssl_lock); { bio = BIO_new(BIO_s_mem()); if (bio == NULL) { Unlock(openssl_lock); return NULL; } BIO_write(bio, b->Buf, b->Size); BIO_seek(bio, 0); } Unlock(openssl_lock); return bio; }
/* Convert the BUF to a BIO*/
Convert the BUF to a BIO
Rand64
UINT64 Rand64() { UINT64 i; Rand(&i, sizeof(i)); return i; }
/* 64-bit random number generation*/
64-bit random number generation
Rand32
UINT Rand32() { UINT i; Rand(&i, sizeof(i)); return i; }
/* 32-bit random number generation*/
32-bit random number generation
Rand16
USHORT Rand16() { USHORT i; Rand(&i, sizeof(i)); return i; }
/* 16-bit random number generation*/
16-bit random number generation
Rand8
UCHAR Rand8() { UCHAR i; Rand(&i, sizeof(i)); return i; }
/* 8-bit random number generation*/
8-bit random number generation
Rand1
bool Rand1() { return (Rand32() % 2) == 0 ? false : true; }
/* 1-bit random number generation*/
1-bit random number generation
Rand
void Rand(void *buf, UINT size) { // Validate arguments if (buf == NULL || size == 0) { return; } RAND_bytes(buf, size); }
/* Random number generation*/
Random number generation
FreeOpenSSLThreadState
void FreeOpenSSLThreadState() { #if OPENSSL_VERSION_NUMBER < 0x10100000L CRYPTO_cleanup_all_ex_data(); ERR_remove_thread_state(NULL); #else #ifndef LIBRESSL_VERSION_NUMBER OPENSSL_thread_stop(); #endif #endif }
/* Delete a thread-specific information that OpenSSL has holded*/
Delete a thread-specific information that OpenSSL has holded
FreeCryptLibrary
void FreeCryptLibrary() { openssl_inited = false; DeleteLock(openssl_lock); openssl_lock = NULL; // RAND_Free_For_SoftEther(); OpenSSL_FreeLock(); #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) #ifdef OPENSSL_FIPS FIPS_mode_set(0); #endif #ifndef OPENSSL_NO_ENGINE ENGINE_cleanup(); #endif CONF_modules_unload(1); EVP_cleanup(); FreeOpenSSLThreadState(); ERR_free_strings(); #ifndef OPENSSL_NO_COMP SSL_COMP_free_compression_methods(); #endif #endif }
/* Release the Crypt library*/
Release the Crypt library
HashToUINT
UINT HashToUINT(void *data, UINT size) { UCHAR hash[SHA1_SIZE]; UINT u; // Validate arguments if (data == NULL && size != 0) { return 0; } Sha1(hash, data, size); Copy(&u, hash, sizeof(UINT)); u = Endian32(u); return u; }
/* Hash with the SHA-1 and convert it to UINT*/
Hash with the SHA-1 and convert it to UINT
NewCrypt
CRYPT *NewCrypt(void *key, UINT size) { CRYPT *c = ZeroMalloc(sizeof(CRYPT)); c->Rc4Key = Malloc(sizeof(struct rc4_key_st)); RC4_set_key(c->Rc4Key, size, (UCHAR *)key); return c; }
/* Creating a new CRYPT object*/
Creating a new CRYPT object
FreeCrypt
void FreeCrypt(CRYPT *c) { // Validate arguments if (c == NULL) { return; } // Memory release Free(c->Rc4Key); Free(c); }
/* Release the CRYPT object*/
Release the CRYPT object
Encrypt
void Encrypt(CRYPT *c, void *dst, void *src, UINT size) { RC4(c->Rc4Key, size, src, dst); }
/* Encryption and decryption*/
Encryption and decryption
DesNewKeyValue
DES_KEY_VALUE *DesNewKeyValue(void *value) { DES_KEY_VALUE *v; // Validate arguments if (value == NULL) { return NULL; } v = ZeroMalloc(sizeof(DES_KEY_VALUE)); Copy(v->KeyValue, value, DES_KEY_SIZE); v->KeySchedule = ZeroMalloc(sizeof(DES_key_schedule)); DES_set_key_unchecked(value, v->KeySchedule); return v; }
/* Create a new DES key element*/
Create a new DES key element
DesFreeKeyValue
void DesFreeKeyValue(DES_KEY_VALUE *v) { // Validate arguments if (v == NULL) { return; } Free(v->KeySchedule); Free(v); }
/* Release of DES key element*/
Release of DES key element
AesFreeKey
void AesFreeKey(AES_KEY_VALUE *k) { // Validate arguments if (k == NULL) { return; } Free(k->EncryptKey); Free(k->DecryptKey); Free(k); }
/* Release the AES key*/
Release the AES key
IsAesNiSupported
bool IsAesNiSupported() { bool supported = false; // Unfortunately OpenSSL doesn't provide a function to do it #ifdef _MSC_VER int regs[4]; // EAX, EBX, ECX, EDX __cpuid(regs, 1); supported = (regs[2] >> 25) & 1; #else // _MSC_VER #if defined(CPU_FEATURES_ARCH_X86) const X86Features features = GetX86Info().features; supported = features.aes; #elif defined(CPU_FEATURES_ARCH_ARM) const ArmFeatures features = GetArmInfo().features; supported = features.aes; #elif defined(CPU_FEATURES_ARCH_AARCH64) const Aarch64Features features = GetAarch64Info().features; supported = features.aes; #elif defined(CPU_FEATURES_ARCH_MIPS) //const MipsFeatures features = GetMipsInfo().features; // no features.aes #elif defined(CPU_FEATURES_ARCH_PPC) //const PPCFeatures features = GetPPCInfo().features; // no features.aes #endif #endif // _MSC_VER return supported; }
/* Determine whether the AES-NI instruction set is supported by the CPU*/
Determine whether the AES-NI instruction set is supported by the CPU
DhNew2048
DH_CTX *DhNew2048() { return DhNew(DH_SET_2048, 2); }
/* Creating a DH 2048bit*/
Creating a DH 2048bit
DhNew3072
DH_CTX *DhNew3072() { return DhNew(DH_SET_3072, 2); }
/* Creating a DH 3072bit*/
Creating a DH 3072bit
DhNew4096
DH_CTX *DhNew4096() { return DhNew(DH_SET_4096, 2); }
/* Creating a DH 4096bit*/
Creating a DH 4096bit
DhNewGroup1
DH_CTX *DhNewGroup1() { return DhNew(DH_GROUP1_PRIME_768, 2); }
/* Creating a DH GROUP1*/
Creating a DH GROUP1
DhNewGroup2
DH_CTX *DhNewGroup2() { return DhNew(DH_GROUP2_PRIME_1024, 2); }
/* Creating a DH GROUP2*/
Creating a DH GROUP2
DhNewGroup5
DH_CTX *DhNewGroup5() { return DhNew(DH_GROUP5_PRIME_1536, 2); }
/* Creating a DH GROUP5*/
Creating a DH GROUP5
DhNewSimple160
DH_CTX *DhNewSimple160() { return DhNew(DH_SIMPLE_160, 2); }
/* Creating a DH SIMPLE 160bits*/
Creating a DH SIMPLE 160bits
DhFree
void DhFree(DH_CTX *dh) { // Validate arguments if (dh == NULL) { return; } DH_free(dh->dh); FreeBuf(dh->MyPrivateKey); FreeBuf(dh->MyPublicKey); Free(dh); }
/* Release of DH*/
Release of DH
IsFileWriteLockedW
bool IsFileWriteLockedW(wchar_t *name) { IO *io; // Validate arguments if (name == NULL) { return false; } if (IsFileExistsW(name) == false) { return false; } io = FileOpenW(name, true); if (io == NULL) { return true; } FileClose(io); return false; }
/* Check whether the file is write-locked*/
Check whether the file is write-locked
NewZipPacker
ZIP_PACKER *NewZipPacker() { ZIP_PACKER *p = ZeroMalloc(sizeof(ZIP_PACKER)); p->Fifo = NewFifo(); p->FileList = NewList(NULL); p->CurrentFile = NULL; return p; }
/* Creating a ZIP packer*/
Creating a ZIP packer