keyctl_pkey.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Public-key operation keyctls
  3. *
  4. * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/err.h>
  9. #include <linux/key.h>
  10. #include <linux/keyctl.h>
  11. #include <linux/parser.h>
  12. #include <linux/uaccess.h>
  13. #include <keys/user-type.h>
  14. #include "internal.h"
  15. static void keyctl_pkey_params_free(struct kernel_pkey_params *params)
  16. {
  17. kfree(params->info);
  18. key_put(params->key);
  19. }
  20. enum {
  21. Opt_err,
  22. Opt_enc, /* "enc=<encoding>" eg. "enc=oaep" */
  23. Opt_hash, /* "hash=<digest-name>" eg. "hash=sha1" */
  24. };
  25. static const match_table_t param_keys = {
  26. { Opt_enc, "enc=%s" },
  27. { Opt_hash, "hash=%s" },
  28. { Opt_err, NULL }
  29. };
  30. /*
  31. * Parse the information string which consists of key=val pairs.
  32. */
  33. static int keyctl_pkey_params_parse(struct kernel_pkey_params *params)
  34. {
  35. unsigned long token_mask = 0;
  36. substring_t args[MAX_OPT_ARGS];
  37. char *c = params->info, *p, *q;
  38. int token;
  39. while ((p = strsep(&c, " \t"))) {
  40. if (*p == '\0' || *p == ' ' || *p == '\t')
  41. continue;
  42. token = match_token(p, param_keys, args);
  43. if (token == Opt_err)
  44. return -EINVAL;
  45. if (__test_and_set_bit(token, &token_mask))
  46. return -EINVAL;
  47. q = args[0].from;
  48. if (!q[0])
  49. return -EINVAL;
  50. switch (token) {
  51. case Opt_enc:
  52. params->encoding = q;
  53. break;
  54. case Opt_hash:
  55. params->hash_algo = q;
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. }
  61. return 0;
  62. }
  63. /*
  64. * Interpret parameters. Callers must always call the free function
  65. * on params, even if an error is returned.
  66. */
  67. static int keyctl_pkey_params_get(key_serial_t id,
  68. const char __user *_info,
  69. struct kernel_pkey_params *params)
  70. {
  71. key_ref_t key_ref;
  72. void *p;
  73. int ret;
  74. memset(params, 0, sizeof(*params));
  75. params->encoding = "raw";
  76. p = strndup_user(_info, PAGE_SIZE);
  77. if (IS_ERR(p))
  78. return PTR_ERR(p);
  79. params->info = p;
  80. ret = keyctl_pkey_params_parse(params);
  81. if (ret < 0)
  82. return ret;
  83. key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
  84. if (IS_ERR(key_ref))
  85. return PTR_ERR(key_ref);
  86. params->key = key_ref_to_ptr(key_ref);
  87. if (!params->key->type->asym_query)
  88. return -EOPNOTSUPP;
  89. return 0;
  90. }
  91. /*
  92. * Get parameters from userspace. Callers must always call the free function
  93. * on params, even if an error is returned.
  94. */
  95. static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_params,
  96. const char __user *_info,
  97. int op,
  98. struct kernel_pkey_params *params)
  99. {
  100. struct keyctl_pkey_params uparams;
  101. struct kernel_pkey_query info;
  102. int ret;
  103. memset(params, 0, sizeof(*params));
  104. params->encoding = "raw";
  105. if (copy_from_user(&uparams, _params, sizeof(uparams)) != 0)
  106. return -EFAULT;
  107. ret = keyctl_pkey_params_get(uparams.key_id, _info, params);
  108. if (ret < 0)
  109. return ret;
  110. ret = params->key->type->asym_query(params, &info);
  111. if (ret < 0)
  112. return ret;
  113. switch (op) {
  114. case KEYCTL_PKEY_ENCRYPT:
  115. if (uparams.in_len > info.max_dec_size ||
  116. uparams.out_len > info.max_enc_size)
  117. return -EINVAL;
  118. break;
  119. case KEYCTL_PKEY_DECRYPT:
  120. if (uparams.in_len > info.max_enc_size ||
  121. uparams.out_len > info.max_dec_size)
  122. return -EINVAL;
  123. break;
  124. case KEYCTL_PKEY_SIGN:
  125. if (uparams.in_len > info.max_data_size ||
  126. uparams.out_len > info.max_sig_size)
  127. return -EINVAL;
  128. break;
  129. case KEYCTL_PKEY_VERIFY:
  130. if (uparams.in_len > info.max_data_size ||
  131. uparams.in2_len > info.max_sig_size)
  132. return -EINVAL;
  133. break;
  134. default:
  135. BUG();
  136. }
  137. params->in_len = uparams.in_len;
  138. params->out_len = uparams.out_len; /* Note: same as in2_len */
  139. return 0;
  140. }
  141. /*
  142. * Query information about an asymmetric key.
  143. */
  144. long keyctl_pkey_query(key_serial_t id,
  145. const char __user *_info,
  146. struct keyctl_pkey_query __user *_res)
  147. {
  148. struct kernel_pkey_params params;
  149. struct kernel_pkey_query res;
  150. long ret;
  151. ret = keyctl_pkey_params_get(id, _info, &params);
  152. if (ret < 0)
  153. goto error;
  154. ret = params.key->type->asym_query(&params, &res);
  155. if (ret < 0)
  156. goto error;
  157. ret = -EFAULT;
  158. if (copy_to_user(_res, &res, sizeof(res)) == 0 &&
  159. clear_user(_res->__spare, sizeof(_res->__spare)) == 0)
  160. ret = 0;
  161. error:
  162. keyctl_pkey_params_free(&params);
  163. return ret;
  164. }
  165. /*
  166. * Encrypt/decrypt/sign
  167. *
  168. * Encrypt data, decrypt data or sign data using a public key.
  169. *
  170. * _info is a string of supplementary information in key=val format. For
  171. * instance, it might contain:
  172. *
  173. * "enc=pkcs1 hash=sha256"
  174. *
  175. * where enc= specifies the encoding and hash= selects the OID to go in that
  176. * particular encoding if required. If enc= isn't supplied, it's assumed that
  177. * the caller is supplying raw values.
  178. *
  179. * If successful, the amount of data written into the output buffer is
  180. * returned.
  181. */
  182. long keyctl_pkey_e_d_s(int op,
  183. const struct keyctl_pkey_params __user *_params,
  184. const char __user *_info,
  185. const void __user *_in,
  186. void __user *_out)
  187. {
  188. struct kernel_pkey_params params;
  189. void *in, *out;
  190. long ret;
  191. ret = keyctl_pkey_params_get_2(_params, _info, op, &params);
  192. if (ret < 0)
  193. goto error_params;
  194. ret = -EOPNOTSUPP;
  195. if (!params.key->type->asym_eds_op)
  196. goto error_params;
  197. switch (op) {
  198. case KEYCTL_PKEY_ENCRYPT:
  199. params.op = kernel_pkey_encrypt;
  200. break;
  201. case KEYCTL_PKEY_DECRYPT:
  202. params.op = kernel_pkey_decrypt;
  203. break;
  204. case KEYCTL_PKEY_SIGN:
  205. params.op = kernel_pkey_sign;
  206. break;
  207. default:
  208. BUG();
  209. }
  210. in = memdup_user(_in, params.in_len);
  211. if (IS_ERR(in)) {
  212. ret = PTR_ERR(in);
  213. goto error_params;
  214. }
  215. ret = -ENOMEM;
  216. out = kmalloc(params.out_len, GFP_KERNEL);
  217. if (!out)
  218. goto error_in;
  219. ret = params.key->type->asym_eds_op(&params, in, out);
  220. if (ret < 0)
  221. goto error_out;
  222. if (copy_to_user(_out, out, ret) != 0)
  223. ret = -EFAULT;
  224. error_out:
  225. kfree(out);
  226. error_in:
  227. kfree(in);
  228. error_params:
  229. keyctl_pkey_params_free(&params);
  230. return ret;
  231. }
  232. /*
  233. * Verify a signature.
  234. *
  235. * Verify a public key signature using the given key, or if not given, search
  236. * for a matching key.
  237. *
  238. * _info is a string of supplementary information in key=val format. For
  239. * instance, it might contain:
  240. *
  241. * "enc=pkcs1 hash=sha256"
  242. *
  243. * where enc= specifies the signature blob encoding and hash= selects the OID
  244. * to go in that particular encoding. If enc= isn't supplied, it's assumed
  245. * that the caller is supplying raw values.
  246. *
  247. * If successful, 0 is returned.
  248. */
  249. long keyctl_pkey_verify(const struct keyctl_pkey_params __user *_params,
  250. const char __user *_info,
  251. const void __user *_in,
  252. const void __user *_in2)
  253. {
  254. struct kernel_pkey_params params;
  255. void *in, *in2;
  256. long ret;
  257. ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY,
  258. &params);
  259. if (ret < 0)
  260. goto error_params;
  261. ret = -EOPNOTSUPP;
  262. if (!params.key->type->asym_verify_signature)
  263. goto error_params;
  264. in = memdup_user(_in, params.in_len);
  265. if (IS_ERR(in)) {
  266. ret = PTR_ERR(in);
  267. goto error_params;
  268. }
  269. in2 = memdup_user(_in2, params.in2_len);
  270. if (IS_ERR(in2)) {
  271. ret = PTR_ERR(in2);
  272. goto error_in;
  273. }
  274. params.op = kernel_pkey_verify;
  275. ret = params.key->type->asym_verify_signature(&params, in, in2);
  276. kfree(in2);
  277. error_in:
  278. kfree(in);
  279. error_params:
  280. keyctl_pkey_params_free(&params);
  281. return ret;
  282. }