hm_pmeth.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/hmac.h>
  15. #include <openssl/err.h>
  16. #include "crypto/evp.h"
  17. /* HMAC pkey context structure */
  18. typedef struct {
  19. const EVP_MD *md; /* MD for HMAC use */
  20. ASN1_OCTET_STRING ktmp; /* Temp storage for key */
  21. HMAC_CTX *ctx;
  22. } HMAC_PKEY_CTX;
  23. static int pkey_hmac_init(EVP_PKEY_CTX *ctx)
  24. {
  25. HMAC_PKEY_CTX *hctx;
  26. if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
  27. CRYPTOerr(CRYPTO_F_PKEY_HMAC_INIT, ERR_R_MALLOC_FAILURE);
  28. return 0;
  29. }
  30. hctx->ktmp.type = V_ASN1_OCTET_STRING;
  31. hctx->ctx = HMAC_CTX_new();
  32. if (hctx->ctx == NULL) {
  33. OPENSSL_free(hctx);
  34. return 0;
  35. }
  36. ctx->data = hctx;
  37. ctx->keygen_info_count = 0;
  38. return 1;
  39. }
  40. static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx);
  41. static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  42. {
  43. HMAC_PKEY_CTX *sctx, *dctx;
  44. /* allocate memory for dst->data and a new HMAC_CTX in dst->data->ctx */
  45. if (!pkey_hmac_init(dst))
  46. return 0;
  47. sctx = EVP_PKEY_CTX_get_data(src);
  48. dctx = EVP_PKEY_CTX_get_data(dst);
  49. dctx->md = sctx->md;
  50. if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
  51. goto err;
  52. if (sctx->ktmp.data) {
  53. if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
  54. sctx->ktmp.data, sctx->ktmp.length))
  55. goto err;
  56. }
  57. return 1;
  58. err:
  59. /* release HMAC_CTX in dst->data->ctx and memory allocated for dst->data */
  60. pkey_hmac_cleanup (dst);
  61. return 0;
  62. }
  63. static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx)
  64. {
  65. HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  66. if (hctx != NULL) {
  67. HMAC_CTX_free(hctx->ctx);
  68. OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
  69. OPENSSL_free(hctx);
  70. EVP_PKEY_CTX_set_data(ctx, NULL);
  71. }
  72. }
  73. static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  74. {
  75. ASN1_OCTET_STRING *hkey = NULL;
  76. HMAC_PKEY_CTX *hctx = ctx->data;
  77. if (!hctx->ktmp.data)
  78. return 0;
  79. hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
  80. if (!hkey)
  81. return 0;
  82. EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
  83. return 1;
  84. }
  85. static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  86. {
  87. HMAC_PKEY_CTX *hctx = EVP_MD_CTX_pkey_ctx(ctx)->data;
  88. if (!HMAC_Update(hctx->ctx, data, count))
  89. return 0;
  90. return 1;
  91. }
  92. static int hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  93. {
  94. HMAC_PKEY_CTX *hctx = ctx->data;
  95. HMAC_CTX_set_flags(hctx->ctx,
  96. EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT));
  97. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  98. EVP_MD_CTX_set_update_fn(mctx, int_update);
  99. return 1;
  100. }
  101. static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  102. EVP_MD_CTX *mctx)
  103. {
  104. unsigned int hlen;
  105. HMAC_PKEY_CTX *hctx = ctx->data;
  106. int l = EVP_MD_CTX_size(mctx);
  107. if (l < 0)
  108. return 0;
  109. *siglen = l;
  110. if (!sig)
  111. return 1;
  112. if (!HMAC_Final(hctx->ctx, sig, &hlen))
  113. return 0;
  114. *siglen = (size_t)hlen;
  115. return 1;
  116. }
  117. static int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  118. {
  119. HMAC_PKEY_CTX *hctx = ctx->data;
  120. ASN1_OCTET_STRING *key;
  121. switch (type) {
  122. case EVP_PKEY_CTRL_SET_MAC_KEY:
  123. if ((!p2 && p1 > 0) || (p1 < -1))
  124. return 0;
  125. if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
  126. return 0;
  127. break;
  128. case EVP_PKEY_CTRL_MD:
  129. hctx->md = p2;
  130. break;
  131. case EVP_PKEY_CTRL_DIGESTINIT:
  132. key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
  133. if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md,
  134. ctx->engine))
  135. return 0;
  136. break;
  137. default:
  138. return -2;
  139. }
  140. return 1;
  141. }
  142. static int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
  143. const char *type, const char *value)
  144. {
  145. if (!value) {
  146. return 0;
  147. }
  148. if (strcmp(type, "key") == 0)
  149. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  150. if (strcmp(type, "hexkey") == 0)
  151. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  152. return -2;
  153. }
  154. const EVP_PKEY_METHOD hmac_pkey_meth = {
  155. EVP_PKEY_HMAC,
  156. 0,
  157. pkey_hmac_init,
  158. pkey_hmac_copy,
  159. pkey_hmac_cleanup,
  160. 0, 0,
  161. 0,
  162. pkey_hmac_keygen,
  163. 0, 0,
  164. 0, 0,
  165. 0, 0,
  166. hmac_signctx_init,
  167. hmac_signctx,
  168. 0, 0,
  169. 0, 0,
  170. 0, 0,
  171. 0, 0,
  172. pkey_hmac_ctrl,
  173. pkey_hmac_ctrl_str
  174. };