xs_openssl.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  2. #ifndef _XS_OPENSSL_H
  3. #define _XS_OPENSSL_H
  4. xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex);
  5. #ifndef _XS_MD5_H
  6. #define xs_md5_hex(input, size) _xs_digest(input, size, "md5", 1)
  7. #endif /* XS_MD5_H */
  8. #ifndef _XS_BASE64_H
  9. xs_str *xs_base64_enc(const xs_val *data, int sz);
  10. xs_val *xs_base64_dec(const xs_str *data, int *size);
  11. #endif /* XS_BASE64_H */
  12. #define xs_sha1_hex(input, size) _xs_digest(input, size, "sha1", 1)
  13. #define xs_sha256_hex(input, size) _xs_digest(input, size, "sha256", 1)
  14. #define xs_sha256_base64(input, size) _xs_digest(input, size, "sha256", 0)
  15. xs_dict *xs_evp_genkey(int bits);
  16. xs_str *xs_evp_sign(const char *secret, const char *mem, int size);
  17. int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig);
  18. #ifdef XS_IMPLEMENTATION
  19. #include "openssl/rsa.h"
  20. #include "openssl/pem.h"
  21. #include "openssl/evp.h"
  22. #ifndef _XS_BASE64_H
  23. xs_str *xs_base64_enc(const xs_val *data, int sz)
  24. /* encodes data to base64 */
  25. {
  26. BIO *mem, *b64;
  27. BUF_MEM *bptr;
  28. b64 = BIO_new(BIO_f_base64());
  29. mem = BIO_new(BIO_s_mem());
  30. b64 = BIO_push(b64, mem);
  31. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  32. BIO_write(b64, data, sz);
  33. BIO_flush(b64);
  34. BIO_get_mem_ptr(b64, &bptr);
  35. int n = bptr->length;
  36. xs_str *s = xs_realloc(NULL, _xs_blk_size(n + 1));
  37. memcpy(s, bptr->data, n);
  38. s[n] = '\0';
  39. BIO_free_all(b64);
  40. return s;
  41. }
  42. xs_val *xs_base64_dec(const xs_str *data, int *size)
  43. /* decodes data from base64 */
  44. {
  45. BIO *b64, *mem;
  46. *size = strlen(data);
  47. b64 = BIO_new(BIO_f_base64());
  48. mem = BIO_new_mem_buf(data, *size);
  49. b64 = BIO_push(b64, mem);
  50. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  51. /* alloc a very big buffer */
  52. xs_str *s = xs_realloc(NULL, *size);
  53. *size = BIO_read(b64, s, *size);
  54. /* adjust to current size */
  55. s = xs_realloc(s, _xs_blk_size(*size + 1));
  56. s[*size] = '\0';
  57. BIO_free_all(mem);
  58. return s;
  59. }
  60. #endif /* _XS_BASE64_H */
  61. xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex)
  62. /* generic function for generating and encoding digests */
  63. {
  64. const EVP_MD *md;
  65. if ((md = EVP_get_digestbyname(digest)) == NULL)
  66. return NULL;
  67. unsigned char output[1024];
  68. unsigned int out_size;
  69. EVP_MD_CTX *mdctx;
  70. mdctx = EVP_MD_CTX_new();
  71. EVP_DigestInit_ex(mdctx, md, NULL);
  72. EVP_DigestUpdate(mdctx, input, size);
  73. EVP_DigestFinal_ex(mdctx, output, &out_size);
  74. EVP_MD_CTX_free(mdctx);
  75. return as_hex ? xs_hex_enc ((char *)output, out_size) :
  76. xs_base64_enc((char *)output, out_size);
  77. }
  78. xs_dict *xs_evp_genkey(int bits)
  79. /* generates an RSA keypair using the EVP interface */
  80. {
  81. xs_dict *keypair = NULL;
  82. EVP_PKEY_CTX *ctx;
  83. EVP_PKEY *pkey = NULL;
  84. if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)) == NULL)
  85. goto end;
  86. if (EVP_PKEY_keygen_init(ctx) <= 0 ||
  87. EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0 ||
  88. EVP_PKEY_keygen(ctx, &pkey) <= 0)
  89. goto end;
  90. BIO *bs = BIO_new(BIO_s_mem());
  91. BIO *bp = BIO_new(BIO_s_mem());
  92. BUF_MEM *sptr;
  93. BUF_MEM *pptr;
  94. PEM_write_bio_PrivateKey(bs, pkey, NULL, NULL, 0, 0, NULL);
  95. BIO_get_mem_ptr(bs, &sptr);
  96. PEM_write_bio_PUBKEY(bp, pkey);
  97. BIO_get_mem_ptr(bp, &pptr);
  98. keypair = xs_dict_new();
  99. keypair = xs_dict_append(keypair, "secret", sptr->data);
  100. keypair = xs_dict_append(keypair, "public", pptr->data);
  101. BIO_free(bs);
  102. BIO_free(bp);
  103. end:
  104. return keypair;
  105. }
  106. xs_str *xs_evp_sign(const char *secret, const char *mem, int size)
  107. /* signs a memory block (secret is in PEM format) */
  108. {
  109. xs_str *signature = NULL;
  110. BIO *b;
  111. unsigned char *sig;
  112. unsigned int sig_len;
  113. EVP_PKEY *pkey;
  114. EVP_MD_CTX *mdctx;
  115. const EVP_MD *md;
  116. /* un-PEM the key */
  117. b = BIO_new_mem_buf(secret, strlen(secret));
  118. pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
  119. /* I've learnt all these magical incantations by watching
  120. the Python module code and the OpenSSL manual pages */
  121. /* Well, "learnt" may be an overstatement */
  122. md = EVP_get_digestbyname("sha256");
  123. mdctx = EVP_MD_CTX_new();
  124. sig_len = EVP_PKEY_size(pkey);
  125. sig = xs_realloc(NULL, sig_len);
  126. EVP_SignInit(mdctx, md);
  127. EVP_SignUpdate(mdctx, mem, size);
  128. if (EVP_SignFinal(mdctx, sig, &sig_len, pkey) == 1)
  129. signature = xs_base64_enc((char *)sig, sig_len);
  130. EVP_MD_CTX_free(mdctx);
  131. EVP_PKEY_free(pkey);
  132. BIO_free(b);
  133. xs_free(sig);
  134. return signature;
  135. }
  136. int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig)
  137. /* verifies a base64 block, returns non-zero on ok */
  138. {
  139. int r = 0;
  140. BIO *b;
  141. EVP_PKEY *pkey;
  142. EVP_MD_CTX *mdctx;
  143. const EVP_MD *md;
  144. /* un-PEM the key */
  145. b = BIO_new_mem_buf(pubkey, strlen(pubkey));
  146. pkey = PEM_read_bio_PUBKEY(b, NULL, NULL, NULL);
  147. md = EVP_get_digestbyname("sha256");
  148. mdctx = EVP_MD_CTX_new();
  149. if (pkey != NULL) {
  150. xs *sig = NULL;
  151. int s_size;
  152. /* de-base64 */
  153. sig = xs_base64_dec(b64sig, &s_size);
  154. if (sig != NULL) {
  155. EVP_VerifyInit(mdctx, md);
  156. EVP_VerifyUpdate(mdctx, mem, size);
  157. r = EVP_VerifyFinal(mdctx, (unsigned char *)sig, s_size, pkey);
  158. }
  159. }
  160. EVP_MD_CTX_free(mdctx);
  161. EVP_PKEY_free(pkey);
  162. BIO_free(b);
  163. return r;
  164. }
  165. #endif /* XS_IMPLEMENTATION */
  166. #endif /* _XS_OPENSSL_H */