| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "mod_sftp.h" |
| 25 |
#include "crypto.h" |
| 26 |
#include "provider.h" |
| 27 |
#include "umac.h" |
| 28 |
|
| 29 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 30 |
# include <openssl/core.h> |
| 31 |
# include <openssl/core_dispatch.h> |
| 32 |
# include <openssl/core_names.h> |
| 33 |
# include <openssl/params.h> |
| 34 |
# include <openssl/provider.h> |
| 35 |
|
| 36 |
static OSSL_PROVIDER *umac_provider = NULL; |
| 37 |
|
| 38 |
static const char *trace_channel = "ssh2"; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
typedef struct umac_ctx_st { |
| 45 |
struct umac_ctx *umac; |
| 46 |
} UMAC_CTX; |
| 47 |
|
| 48 |
static void *umac_ctx_new(void *vctx) { |
| 49 |
UMAC_CTX *ctx; |
| 50 |
|
| 51 |
ctx = OPENSSL_zalloc(sizeof(UMAC_CTX)); |
| 52 |
return ctx; |
| 53 |
} |
| 54 |
|
| 55 |
static void umac_ctx_free(void *vctx) { |
| 56 |
UMAC_CTX *ctx; |
| 57 |
|
| 58 |
ctx = vctx; |
| 59 |
if (ctx->umac != NULL) { |
| 60 |
umac_delete(ctx->umac); |
| 61 |
ctx->umac = NULL; |
| 62 |
} |
| 63 |
|
| 64 |
OPENSSL_free(ctx); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static int umac_md_init(void *vctx) { |
| 71 |
(void) vctx; |
| 72 |
|
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
static const OSSL_PARAM umac_params[] = { |
| 77 |
OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, NULL), |
| 78 |
OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_SIZE, NULL), |
| 79 |
OSSL_PARAM_END |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static int umac64_md_update(void *vctx, const unsigned char *data, size_t len) { |
| 85 |
UMAC_CTX *ctx; |
| 86 |
struct umac_ctx *umac; |
| 87 |
|
| 88 |
ctx = vctx; |
| 89 |
umac = ctx->umac; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if (umac == NULL) { |
| 96 |
umac = umac_new((unsigned char *) data); |
| 97 |
if (umac == NULL) { |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
ctx->umac = umac; |
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
return umac_update(umac, (unsigned char *) data, (long) len); |
| 106 |
} |
| 107 |
|
| 108 |
static int umac64_md_final(void *vctx, unsigned char *out, size_t *out_len, |
| 109 |
size_t outsz) { |
| 110 |
int res = 1; |
| 111 |
struct umac_ctx *ctx; |
| 112 |
unsigned char nonce[8]; |
| 113 |
|
| 114 |
ctx = vctx; |
| 115 |
|
| 116 |
*out_len = outsz; |
| 117 |
|
| 118 |
if (outsz != 0) { |
| 119 |
res = umac_final(ctx, out, nonce); |
| 120 |
} |
| 121 |
|
| 122 |
return res; |
| 123 |
} |
| 124 |
|
| 125 |
static int umac64_get_params(void *provctx, OSSL_PARAM params[]) { |
| 126 |
OSSL_PARAM *p; |
| 127 |
int ok = 1; |
| 128 |
|
| 129 |
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); |
| 130 |
if (p != NULL) { |
| 131 |
if (OSSL_PARAM_set_size_t(p, 32) != 1) { |
| 132 |
ok = 0; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (ok == 1) { |
| 137 |
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); |
| 138 |
if (p != NULL) { |
| 139 |
if (OSSL_PARAM_set_size_t(p, 8) != 1) { |
| 140 |
ok = 0; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return ok; |
| 146 |
} |
| 147 |
|
| 148 |
static const OSSL_PARAM *umac64_gettable_params(void) { |
| 149 |
return umac_params; |
| 150 |
} |
| 151 |
|
| 152 |
static const OSSL_DISPATCH umac64_functions[] = { |
| 153 |
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void)) umac_ctx_new }, |
| 154 |
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void)) umac_ctx_free }, |
| 155 |
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void)) umac_md_init }, |
| 156 |
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void)) umac64_md_update }, |
| 157 |
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void)) umac64_md_final }, |
| 158 |
{ OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void)) umac64_get_params }, |
| 159 |
{ OSSL_FUNC_DIGEST_GETTABLE_PARAMS, (void (*)(void)) umac64_gettable_params }, |
| 160 |
|
| 161 |
{ 0, NULL } |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
static int umac128_md_update(void *vctx, const unsigned char *data, |
| 167 |
size_t len) { |
| 168 |
UMAC_CTX *ctx; |
| 169 |
struct umac_ctx *umac; |
| 170 |
|
| 171 |
ctx = vctx; |
| 172 |
umac = ctx->umac; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if (umac == NULL) { |
| 179 |
umac = umac128_new((unsigned char *) data); |
| 180 |
if (umac == NULL) { |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
ctx->umac = umac; |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
return umac128_update(umac, (unsigned char *) data, (long) len); |
| 189 |
} |
| 190 |
|
| 191 |
static int umac128_md_final(void *vctx, unsigned char *out, size_t *out_len, |
| 192 |
size_t outsz) { |
| 193 |
int res = 1; |
| 194 |
struct umac_ctx *ctx; |
| 195 |
unsigned char nonce[8]; |
| 196 |
|
| 197 |
ctx = vctx; |
| 198 |
|
| 199 |
*out_len = outsz; |
| 200 |
|
| 201 |
if (outsz != 0) { |
| 202 |
res = umac128_final(ctx, out, nonce); |
| 203 |
} |
| 204 |
|
| 205 |
return res; |
| 206 |
} |
| 207 |
|
| 208 |
static int umac128_get_params(void *provctx, OSSL_PARAM params[]) { |
| 209 |
OSSL_PARAM *p; |
| 210 |
int ok = 1; |
| 211 |
|
| 212 |
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); |
| 213 |
if (p != NULL) { |
| 214 |
if (OSSL_PARAM_set_size_t(p, 64) != 1) { |
| 215 |
ok = 0; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if (ok == 1) { |
| 220 |
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); |
| 221 |
if (p != NULL) { |
| 222 |
if (OSSL_PARAM_set_size_t(p, 16) != 1) { |
| 223 |
ok = 0; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return ok; |
| 229 |
} |
| 230 |
|
| 231 |
static const OSSL_PARAM *umac128_gettable_params(void) { |
| 232 |
return umac_params; |
| 233 |
} |
| 234 |
|
| 235 |
static const OSSL_DISPATCH umac128_functions[] = { |
| 236 |
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void)) umac_ctx_new }, |
| 237 |
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void)) umac_ctx_free }, |
| 238 |
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void)) umac_md_init }, |
| 239 |
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void)) umac128_md_update }, |
| 240 |
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void)) umac128_md_final }, |
| 241 |
{ OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void)) umac128_get_params }, |
| 242 |
{ OSSL_FUNC_DIGEST_GETTABLE_PARAMS, (void (*)(void)) umac128_gettable_params }, |
| 243 |
|
| 244 |
{ 0, NULL } |
| 245 |
}; |
| 246 |
|
| 247 |
static const OSSL_ALGORITHM umac_digests[] = { |
| 248 |
{ "umac64", NULL, umac64_functions }, |
| 249 |
{ "umac128", NULL, umac128_functions }, |
| 250 |
|
| 251 |
{ NULL, NULL, NULL } |
| 252 |
}; |
| 253 |
|
| 254 |
static const OSSL_ALGORITHM *umac_provider_operations(void *provctx, |
| 255 |
int operation_id, int *no_cache) { |
| 256 |
*no_cache = 0; |
| 257 |
|
| 258 |
if (operation_id == OSSL_OP_DIGEST) { |
| 259 |
return umac_digests; |
| 260 |
} |
| 261 |
|
| 262 |
return NULL; |
| 263 |
} |
| 264 |
|
| 265 |
static const OSSL_DISPATCH umac_provider_functions[] = { |
| 266 |
{ OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void)) umac_provider_operations }, |
| 267 |
|
| 268 |
{ 0, NULL } |
| 269 |
}; |
| 270 |
|
| 271 |
static int umac_provider_init(const OSSL_CORE_HANDLE *core, |
| 272 |
const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { |
| 273 |
*out = umac_provider_functions; |
| 274 |
*provctx = (void *) core; |
| 275 |
|
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
#endif |
| 280 |
|
| 281 |
int sftp_provider_init(void) { |
| 282 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 283 |
if (OSSL_PROVIDER_add_builtin(NULL, "umac", umac_provider_init) != 1) { |
| 284 |
pr_log_debug(DEBUG1, MOD_SFTP_VERSION |
| 285 |
": error registering 'umac' OpenSSL provider: %s", |
| 286 |
sftp_crypto_get_errors()); |
| 287 |
|
| 288 |
} else { |
| 289 |
pr_trace_msg(trace_channel, 9, "%s", "registered 'umac' OpenSSL provider"); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
umac_provider = OSSL_PROVIDER_load(NULL, "umac"); |
| 294 |
if (umac_provider == NULL) { |
| 295 |
pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION |
| 296 |
": error loading 'umac' OpenSSL provider: %s", sftp_crypto_get_errors()); |
| 297 |
|
| 298 |
} else { |
| 299 |
pr_trace_msg(trace_channel, 9, "%s", "loaded 'umac' OpenSSL provider"); |
| 300 |
} |
| 301 |
#endif |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
void sftp_provider_free(void) { |
| 307 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 308 |
if (umac_provider != NULL) { |
| 309 |
OSSL_PROVIDER_unload(umac_provider); |
| 310 |
umac_provider = NULL; |
| 311 |
} |
| 312 |
#endif |
| 313 |
} |