// SPDX-License-Identifier: GPL-2.0-or-later /* Public-key operation keyctls * * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com)
*/
/* * Parse the information string which consists of key=val pairs.
*/ staticint keyctl_pkey_params_parse(struct kernel_pkey_params *params)
{ unsignedlong token_mask = 0;
substring_t args[MAX_OPT_ARGS]; char *c = params->info, *p, *q; int token;
while ((p = strsep(&c, " \t"))) { if (*p == '\0' || *p == ' ' || *p == '\t') continue;
token = match_token(p, param_keys, args); if (token == Opt_err) return -EINVAL; if (__test_and_set_bit(token, &token_mask)) return -EINVAL;
q = args[0].from; if (!q[0]) return -EINVAL;
switch (token) { case Opt_enc:
params->encoding = q; break;
case Opt_hash:
params->hash_algo = q; break;
default: return -EINVAL;
}
}
return 0;
}
/* * Interpret parameters. Callers must always call the free function * on params, even if an error is returned.
*/ staticint keyctl_pkey_params_get(key_serial_t id, constchar __user *_info, struct kernel_pkey_params *params)
{
key_ref_t key_ref; void *p; int ret;
if (!params->key->type->asym_query) return -EOPNOTSUPP;
return 0;
}
/* * Get parameters from userspace. Callers must always call the free function * on params, even if an error is returned.
*/ staticint keyctl_pkey_params_get_2(conststruct keyctl_pkey_params __user *_params, constchar __user *_info, int op, struct kernel_pkey_params *params)
{ struct keyctl_pkey_params uparams; struct kernel_pkey_query info; int ret;
/* * Encrypt/decrypt/sign * * Encrypt data, decrypt data or sign data using a public key. * * _info is a string of supplementary information in key=val format. For * instance, it might contain: * * "enc=pkcs1 hash=sha256" * * where enc= specifies the encoding and hash= selects the OID to go in that * particular encoding if required. If enc= isn't supplied, it's assumed that * the caller is supplying raw values. * * If successful, the amount of data written into the output buffer is * returned.
*/ long keyctl_pkey_e_d_s(int op, conststruct keyctl_pkey_params __user *_params, constchar __user *_info, constvoid __user *_in, void __user *_out)
{ struct kernel_pkey_params params; void *in, *out; long ret;
ret = keyctl_pkey_params_get_2(_params, _info, op, ¶ms); if (ret < 0) goto error_params;
ret = -EOPNOTSUPP; if (!params.key->type->asym_eds_op) goto error_params;
switch (op) { case KEYCTL_PKEY_ENCRYPT:
params.op = kernel_pkey_encrypt; break; case KEYCTL_PKEY_DECRYPT:
params.op = kernel_pkey_decrypt; break; case KEYCTL_PKEY_SIGN:
params.op = kernel_pkey_sign; break; default:
BUG();
}
in = memdup_user(_in, params.in_len); if (IS_ERR(in)) {
ret = PTR_ERR(in); goto error_params;
}
ret = -ENOMEM;
out = kmalloc(params.out_len, GFP_KERNEL); if (!out) goto error_in;
ret = params.key->type->asym_eds_op(¶ms, in, out); if (ret < 0) goto error_out;
if (copy_to_user(_out, out, ret) != 0)
ret = -EFAULT;
/* * Verify a signature. * * Verify a public key signature using the given key, or if not given, search * for a matching key. * * _info is a string of supplementary information in key=val format. For * instance, it might contain: * * "enc=pkcs1 hash=sha256" * * where enc= specifies the signature blob encoding and hash= selects the OID * to go in that particular encoding. If enc= isn't supplied, it's assumed * that the caller is supplying raw values. * * If successful, 0 is returned.
*/ long keyctl_pkey_verify(conststruct keyctl_pkey_params __user *_params, constchar __user *_info, constvoid __user *_in, constvoid __user *_in2)
{ struct kernel_pkey_params params; void *in, *in2; long ret;
ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY,
¶ms); if (ret < 0) goto error_params;
ret = -EOPNOTSUPP; if (!params.key->type->asym_verify_signature) goto error_params;
in = memdup_user(_in, params.in_len); if (IS_ERR(in)) {
ret = PTR_ERR(in); goto error_params;
}
in2 = memdup_user(_in2, params.in2_len); if (IS_ERR(in2)) {
ret = PTR_ERR(in2); goto error_in;
}
params.op = kernel_pkey_verify;
ret = params.key->type->asym_verify_signature(¶ms, in, in2);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.