123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- #ifndef _XS_OPENSSL_H
- #define _XS_OPENSSL_H
- d_char *xs_md5_hex(const void *input, int size);
- d_char *xs_sha1_hex(const void *input, int size);
- d_char *xs_sha256_hex(const void *input, int size);
- d_char *xs_sha256_base64(const void *input, int size);
- d_char *xs_rsa_genkey(int bits);
- d_char *xs_rsa_sign(char *secret, char *mem, int size);
- int xs_rsa_verify(char *pubkey, char *mem, int size, char *b64sig);
- d_char *xs_evp_sign(char *secret, char *mem, int size);
- int xs_evp_verify(char *pubkey, char *mem, int size, char *b64sig);
- #ifdef XS_IMPLEMENTATION
- #include "openssl/md5.h"
- #include "openssl/sha.h"
- #include "openssl/rsa.h"
- #include "openssl/pem.h"
- #include "openssl/evp.h"
- d_char *xs_md5_hex(const void *input, int size)
- {
- unsigned char md5[16];
- MD5_CTX ctx;
- MD5_Init(&ctx);
- MD5_Update(&ctx, input, size);
- MD5_Final(md5, &ctx);
- return xs_hex_enc((char *)md5, sizeof(md5));
- }
- d_char *xs_sha1_hex(const void *input, int size)
- {
- unsigned char sha1[20];
- SHA_CTX ctx;
- SHA1_Init(&ctx);
- SHA1_Update(&ctx, input, size);
- SHA1_Final(sha1, &ctx);
- return xs_hex_enc((char *)sha1, sizeof(sha1));
- }
- unsigned char *_xs_sha256(const void *input, int size, unsigned char *sha256)
- {
- SHA256_CTX ctx;
- SHA256_Init(&ctx);
- SHA256_Update(&ctx, input, size);
- SHA256_Final(sha256, &ctx);
- return sha256;
- }
- d_char *xs_sha256_hex(const void *input, int size)
- {
- unsigned char sha256[32];
- _xs_sha256(input, size, sha256);
- return xs_hex_enc((char *)sha256, sizeof(sha256));
- }
- d_char *xs_sha256_base64(const void *input, int size)
- {
- unsigned char sha256[32];
- _xs_sha256(input, size, sha256);
- return xs_base64_enc((char *)sha256, sizeof(sha256));
- }
- d_char *xs_rsa_genkey(int bits)
- {
- BIGNUM *bne;
- RSA *rsa;
- d_char *keypair = NULL;
- if ((bne = BN_new()) != NULL) {
- if (BN_set_word(bne, RSA_F4) == 1) {
- if ((rsa = RSA_new()) != NULL) {
- if (RSA_generate_key_ex(rsa, bits, bne, NULL) == 1) {
- BIO *bs = BIO_new(BIO_s_mem());
- BIO *bp = BIO_new(BIO_s_mem());
- BUF_MEM *sptr;
- BUF_MEM *pptr;
- PEM_write_bio_RSAPrivateKey(bs, rsa, NULL, NULL, 0, 0, NULL);
- BIO_get_mem_ptr(bs, &sptr);
- PEM_write_bio_RSA_PUBKEY(bp, rsa);
- BIO_get_mem_ptr(bp, &pptr);
- keypair = xs_dict_new();
- keypair = xs_dict_append(keypair, "secret", sptr->data);
- keypair = xs_dict_append(keypair, "public", pptr->data);
- BIO_free(bs);
- BIO_free(bp);
- }
- }
- }
- }
- return keypair;
- }
- d_char *xs_rsa_sign(char *secret, char *mem, int size)
- {
- d_char *signature = NULL;
- BIO *b;
- RSA *rsa;
- unsigned char *sig;
- unsigned int sig_len;
-
- b = BIO_new_mem_buf(secret, strlen(secret));
- rsa = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL);
-
- sig = malloc(RSA_size(rsa));
- if (RSA_sign(NID_sha256, (unsigned char *)mem, size, sig, &sig_len, rsa) == 1)
- signature = xs_base64_enc((char *)sig, sig_len);
- BIO_free(b);
- RSA_free(rsa);
- free(sig);
- return signature;
- }
- int xs_rsa_verify(char *pubkey, char *mem, int size, char *b64sig)
- {
- int r = 0;
- BIO *b;
- RSA *rsa;
-
- b = BIO_new_mem_buf(pubkey, strlen(pubkey));
- rsa = PEM_read_bio_RSA_PUBKEY(b, NULL, NULL, NULL);
- if (rsa != NULL) {
- xs *sig = NULL;
- int s_size;
-
- sig = xs_base64_dec(b64sig, &s_size);
- if (sig != NULL)
- r = RSA_verify(NID_sha256, (unsigned char *)mem, size,
- (unsigned char *)sig, s_size, rsa);
- }
- BIO_free(b);
- RSA_free(rsa);
- return r;
- }
- d_char *xs_evp_sign(char *secret, char *mem, int size)
- {
- d_char *signature = NULL;
- BIO *b;
- unsigned char *sig;
- unsigned int sig_len;
- EVP_PKEY *pkey;
- EVP_MD_CTX *mdctx;
- const EVP_MD *md;
-
- b = BIO_new_mem_buf(secret, strlen(secret));
- pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
-
-
- md = EVP_get_digestbyname("sha256");
- mdctx = EVP_MD_CTX_new();
- sig_len = EVP_PKEY_size(pkey);
- sig = malloc(sig_len);
- EVP_SignInit(mdctx, md);
- EVP_SignUpdate(mdctx, mem, size);
- if (EVP_SignFinal(mdctx, sig, &sig_len, pkey) == 1)
- signature = xs_base64_enc((char *)sig, sig_len);
- EVP_MD_CTX_free(mdctx);
- EVP_PKEY_free(pkey);
- BIO_free(b);
- free(sig);
- return signature;
- }
- int xs_evp_verify(char *pubkey, char *mem, int size, char *b64sig)
- {
- int r = 0;
- BIO *b;
- EVP_PKEY *pkey;
- EVP_MD_CTX *mdctx;
- const EVP_MD *md;
-
- b = BIO_new_mem_buf(pubkey, strlen(pubkey));
- pkey = PEM_read_bio_PUBKEY(b, NULL, NULL, NULL);
- md = EVP_get_digestbyname("sha256");
- mdctx = EVP_MD_CTX_new();
- if (pkey != NULL) {
- xs *sig = NULL;
- int s_size;
-
- sig = xs_base64_dec(b64sig, &s_size);
- if (sig != NULL) {
- EVP_VerifyInit(mdctx, md);
- EVP_VerifyUpdate(mdctx, mem, size);
- r = EVP_VerifyFinal(mdctx, (unsigned char *)sig, s_size, pkey);
- }
- }
- EVP_MD_CTX_free(mdctx);
- EVP_PKEY_free(pkey);
- BIO_free(b);
- return r;
- }
- #endif
- #endif
|