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