/src/openssl111/crypto/hmac/hm_pmeth.c
| Line | Count | Source (jump to first uncovered line) | 
| 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 |  |  | 
| 10 |  | #include <stdio.h> | 
| 11 |  | #include "internal/cryptlib.h" | 
| 12 |  | #include <openssl/x509.h> | 
| 13 |  | #include <openssl/x509v3.h> | 
| 14 |  | #include <openssl/evp.h> | 
| 15 |  | #include <openssl/hmac.h> | 
| 16 |  | #include <openssl/err.h> | 
| 17 |  | #include "crypto/evp.h" | 
| 18 |  |  | 
| 19 |  | /* HMAC pkey context structure */ | 
| 20 |  |  | 
| 21 |  | typedef struct { | 
| 22 |  |     const EVP_MD *md;           /* MD for HMAC use */ | 
| 23 |  |     ASN1_OCTET_STRING ktmp;     /* Temp storage for key */ | 
| 24 |  |     HMAC_CTX *ctx; | 
| 25 |  | } HMAC_PKEY_CTX; | 
| 26 |  |  | 
| 27 |  | static int pkey_hmac_init(EVP_PKEY_CTX *ctx) | 
| 28 | 19.8k | { | 
| 29 | 19.8k |     HMAC_PKEY_CTX *hctx; | 
| 30 |  |  | 
| 31 | 19.8k |     if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) { | 
| 32 | 0 |         CRYPTOerr(CRYPTO_F_PKEY_HMAC_INIT, ERR_R_MALLOC_FAILURE); | 
| 33 | 0 |         return 0; | 
| 34 | 0 |     } | 
| 35 | 19.8k |     hctx->ktmp.type = V_ASN1_OCTET_STRING; | 
| 36 | 19.8k |     hctx->ctx = HMAC_CTX_new(); | 
| 37 | 19.8k |     if (hctx->ctx == NULL) { | 
| 38 | 0 |         OPENSSL_free(hctx); | 
| 39 | 0 |         return 0; | 
| 40 | 0 |     } | 
| 41 |  |  | 
| 42 | 19.8k |     ctx->data = hctx; | 
| 43 | 19.8k |     ctx->keygen_info_count = 0; | 
| 44 |  |  | 
| 45 | 19.8k |     return 1; | 
| 46 | 19.8k | } | 
| 47 |  |  | 
| 48 |  | static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx); | 
| 49 |  |  | 
| 50 |  | static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) | 
| 51 | 17.5k | { | 
| 52 | 17.5k |     HMAC_PKEY_CTX *sctx, *dctx; | 
| 53 |  |  | 
| 54 |  |     /* allocate memory for dst->data and a new HMAC_CTX in dst->data->ctx */ | 
| 55 | 17.5k |     if (!pkey_hmac_init(dst)) | 
| 56 | 0 |         return 0; | 
| 57 | 17.5k |     sctx = EVP_PKEY_CTX_get_data(src); | 
| 58 | 17.5k |     dctx = EVP_PKEY_CTX_get_data(dst); | 
| 59 | 17.5k |     dctx->md = sctx->md; | 
| 60 | 17.5k |     if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx)) | 
| 61 | 0 |         goto err; | 
| 62 | 17.5k |     if (sctx->ktmp.data) { | 
| 63 | 0 |         if (!ASN1_OCTET_STRING_set(&dctx->ktmp, | 
| 64 | 0 |                                    sctx->ktmp.data, sctx->ktmp.length)) | 
| 65 | 0 |             goto err; | 
| 66 | 0 |     } | 
| 67 | 17.5k |     return 1; | 
| 68 | 0 | err: | 
| 69 |  |     /* release HMAC_CTX in dst->data->ctx and memory allocated for dst->data */ | 
| 70 | 0 |     pkey_hmac_cleanup (dst); | 
| 71 | 0 |     return 0; | 
| 72 | 17.5k | } | 
| 73 |  |  | 
| 74 |  | static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx) | 
| 75 | 19.8k | { | 
| 76 | 19.8k |     HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx); | 
| 77 |  |  | 
| 78 | 19.8k |     if (hctx != NULL) { | 
| 79 | 19.8k |         HMAC_CTX_free(hctx->ctx); | 
| 80 | 19.8k |         OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length); | 
| 81 | 19.8k |         OPENSSL_free(hctx); | 
| 82 | 19.8k |         EVP_PKEY_CTX_set_data(ctx, NULL); | 
| 83 | 19.8k |     } | 
| 84 | 19.8k | } | 
| 85 |  |  | 
| 86 |  | static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | 
| 87 | 179 | { | 
| 88 | 179 |     ASN1_OCTET_STRING *hkey = NULL; | 
| 89 | 179 |     HMAC_PKEY_CTX *hctx = ctx->data; | 
| 90 | 179 |     if (!hctx->ktmp.data) | 
| 91 | 0 |         return 0; | 
| 92 | 179 |     hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp); | 
| 93 | 179 |     if (!hkey) | 
| 94 | 0 |         return 0; | 
| 95 | 179 |     EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey); | 
| 96 |  |  | 
| 97 | 179 |     return 1; | 
| 98 | 179 | } | 
| 99 |  |  | 
| 100 |  | static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count) | 
| 101 | 10.5k | { | 
| 102 | 10.5k |     HMAC_PKEY_CTX *hctx = EVP_MD_CTX_pkey_ctx(ctx)->data; | 
| 103 | 10.5k |     if (!HMAC_Update(hctx->ctx, data, count)) | 
| 104 | 0 |         return 0; | 
| 105 | 10.5k |     return 1; | 
| 106 | 10.5k | } | 
| 107 |  |  | 
| 108 |  | static int hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) | 
| 109 | 2.10k | { | 
| 110 | 2.10k |     HMAC_PKEY_CTX *hctx = ctx->data; | 
| 111 | 2.10k |     HMAC_CTX_set_flags(hctx->ctx, | 
| 112 | 2.10k |                        EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT)); | 
| 113 | 2.10k |     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT); | 
| 114 | 2.10k |     EVP_MD_CTX_set_update_fn(mctx, int_update); | 
| 115 | 2.10k |     return 1; | 
| 116 | 2.10k | } | 
| 117 |  |  | 
| 118 |  | static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | 
| 119 |  |                         EVP_MD_CTX *mctx) | 
| 120 | 9.05k | { | 
| 121 | 9.05k |     unsigned int hlen; | 
| 122 | 9.05k |     HMAC_PKEY_CTX *hctx = ctx->data; | 
| 123 | 9.05k |     int l = EVP_MD_CTX_size(mctx); | 
| 124 |  |  | 
| 125 | 9.05k |     if (l < 0) | 
| 126 | 0 |         return 0; | 
| 127 | 9.05k |     *siglen = l; | 
| 128 | 9.05k |     if (!sig) | 
| 129 | 0 |         return 1; | 
| 130 |  |  | 
| 131 | 9.05k |     if (!HMAC_Final(hctx->ctx, sig, &hlen)) | 
| 132 | 0 |         return 0; | 
| 133 | 9.05k |     *siglen = (size_t)hlen; | 
| 134 | 9.05k |     return 1; | 
| 135 | 9.05k | } | 
| 136 |  |  | 
| 137 |  | static int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | 
| 138 | 4.39k | { | 
| 139 | 4.39k |     HMAC_PKEY_CTX *hctx = ctx->data; | 
| 140 | 4.39k |     ASN1_OCTET_STRING *key; | 
| 141 | 4.39k |     switch (type) { | 
| 142 |  |  | 
| 143 | 179 |     case EVP_PKEY_CTRL_SET_MAC_KEY: | 
| 144 | 179 |         if ((!p2 && p1 > 0) || (p1 < -1)) | 
| 145 | 0 |             return 0; | 
| 146 | 179 |         if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1)) | 
| 147 | 0 |             return 0; | 
| 148 | 179 |         break; | 
| 149 |  |  | 
| 150 | 2.10k |     case EVP_PKEY_CTRL_MD: | 
| 151 | 2.10k |         hctx->md = p2; | 
| 152 | 2.10k |         break; | 
| 153 |  |  | 
| 154 | 2.10k |     case EVP_PKEY_CTRL_DIGESTINIT: | 
| 155 | 2.10k |         key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr; | 
| 156 | 2.10k |         if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md, | 
| 157 | 2.10k |                           ctx->engine)) | 
| 158 | 0 |             return 0; | 
| 159 | 2.10k |         break; | 
| 160 |  |  | 
| 161 | 2.10k |     default: | 
| 162 | 0 |         return -2; | 
| 163 |  |  | 
| 164 | 4.39k |     } | 
| 165 | 4.39k |     return 1; | 
| 166 | 4.39k | } | 
| 167 |  |  | 
| 168 |  | static int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx, | 
| 169 |  |                               const char *type, const char *value) | 
| 170 | 0 | { | 
| 171 | 0 |     if (!value) { | 
| 172 | 0 |         return 0; | 
| 173 | 0 |     } | 
| 174 | 0 |     if (strcmp(type, "key") == 0) | 
| 175 | 0 |         return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value); | 
| 176 | 0 |     if (strcmp(type, "hexkey") == 0) | 
| 177 | 0 |         return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value); | 
| 178 | 0 |     return -2; | 
| 179 | 0 | } | 
| 180 |  |  | 
| 181 |  | const EVP_PKEY_METHOD hmac_pkey_meth = { | 
| 182 |  |     EVP_PKEY_HMAC, | 
| 183 |  |     0, | 
| 184 |  |     pkey_hmac_init, | 
| 185 |  |     pkey_hmac_copy, | 
| 186 |  |     pkey_hmac_cleanup, | 
| 187 |  |  | 
| 188 |  |     0, 0, | 
| 189 |  |  | 
| 190 |  |     0, | 
| 191 |  |     pkey_hmac_keygen, | 
| 192 |  |  | 
| 193 |  |     0, 0, | 
| 194 |  |  | 
| 195 |  |     0, 0, | 
| 196 |  |  | 
| 197 |  |     0, 0, | 
| 198 |  |  | 
| 199 |  |     hmac_signctx_init, | 
| 200 |  |     hmac_signctx, | 
| 201 |  |  | 
| 202 |  |     0, 0, | 
| 203 |  |  | 
| 204 |  |     0, 0, | 
| 205 |  |  | 
| 206 |  |     0, 0, | 
| 207 |  |  | 
| 208 |  |     0, 0, | 
| 209 |  |  | 
| 210 |  |     pkey_hmac_ctrl, | 
| 211 |  |     pkey_hmac_ctrl_str | 
| 212 |  | }; |