public_key.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* In-software asymmetric public-key crypto subtype
  2. *
  3. * See Documentation/crypto/asymmetric-keys.txt
  4. *
  5. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  6. * Written by David Howells (dhowells@redhat.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public Licence
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the Licence, or (at your option) any later version.
  12. */
  13. #define pr_fmt(fmt) "PKEY: "fmt
  14. #include <linux/module.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/seq_file.h>
  19. #include <keys/asymmetric-subtype.h>
  20. #include "public_key.h"
  21. MODULE_LICENSE("GPL");
  22. const char *const pkey_algo_name[PKEY_ALGO__LAST] = {
  23. [PKEY_ALGO_DSA] = "DSA",
  24. [PKEY_ALGO_RSA] = "RSA",
  25. };
  26. EXPORT_SYMBOL_GPL(pkey_algo_name);
  27. const struct public_key_algorithm *pkey_algo[PKEY_ALGO__LAST] = {
  28. #if defined(CONFIG_PUBLIC_KEY_ALGO_RSA) || \
  29. defined(CONFIG_PUBLIC_KEY_ALGO_RSA_MODULE)
  30. [PKEY_ALGO_RSA] = &RSA_public_key_algorithm,
  31. #endif
  32. };
  33. EXPORT_SYMBOL_GPL(pkey_algo);
  34. const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = {
  35. [PKEY_ID_PGP] = "PGP",
  36. [PKEY_ID_X509] = "X509",
  37. [PKEY_ID_PKCS7] = "PKCS#7",
  38. };
  39. EXPORT_SYMBOL_GPL(pkey_id_type_name);
  40. /*
  41. * Provide a part of a description of the key for /proc/keys.
  42. */
  43. static void public_key_describe(const struct key *asymmetric_key,
  44. struct seq_file *m)
  45. {
  46. struct public_key *key = asymmetric_key->payload.data[asym_crypto];
  47. if (key)
  48. seq_printf(m, "%s.%s",
  49. pkey_id_type_name[key->id_type], key->algo->name);
  50. }
  51. /*
  52. * Destroy a public key algorithm key.
  53. */
  54. void public_key_destroy(void *payload)
  55. {
  56. struct public_key *key = payload;
  57. int i;
  58. if (key) {
  59. for (i = 0; i < ARRAY_SIZE(key->mpi); i++)
  60. mpi_free(key->mpi[i]);
  61. kfree(key);
  62. }
  63. }
  64. EXPORT_SYMBOL_GPL(public_key_destroy);
  65. /*
  66. * Verify a signature using a public key.
  67. */
  68. int public_key_verify_signature(const struct public_key *pk,
  69. const struct public_key_signature *sig)
  70. {
  71. const struct public_key_algorithm *algo;
  72. BUG_ON(!pk);
  73. BUG_ON(!pk->mpi[0]);
  74. BUG_ON(!pk->mpi[1]);
  75. BUG_ON(!sig);
  76. BUG_ON(!sig->digest);
  77. BUG_ON(!sig->mpi[0]);
  78. algo = pk->algo;
  79. if (!algo) {
  80. if (pk->pkey_algo >= PKEY_ALGO__LAST)
  81. return -ENOPKG;
  82. algo = pkey_algo[pk->pkey_algo];
  83. if (!algo)
  84. return -ENOPKG;
  85. }
  86. if (!algo->verify_signature)
  87. return -ENOTSUPP;
  88. if (sig->nr_mpi != algo->n_sig_mpi) {
  89. pr_debug("Signature has %u MPI not %u\n",
  90. sig->nr_mpi, algo->n_sig_mpi);
  91. return -EINVAL;
  92. }
  93. return algo->verify_signature(pk, sig);
  94. }
  95. EXPORT_SYMBOL_GPL(public_key_verify_signature);
  96. static int public_key_verify_signature_2(const struct key *key,
  97. const struct public_key_signature *sig)
  98. {
  99. const struct public_key *pk = key->payload.data[asym_crypto];
  100. return public_key_verify_signature(pk, sig);
  101. }
  102. /*
  103. * Public key algorithm asymmetric key subtype
  104. */
  105. struct asymmetric_key_subtype public_key_subtype = {
  106. .owner = THIS_MODULE,
  107. .name = "public_key",
  108. .name_len = sizeof("public_key") - 1,
  109. .describe = public_key_describe,
  110. .destroy = public_key_destroy,
  111. .verify_signature = public_key_verify_signature_2,
  112. };
  113. EXPORT_SYMBOL_GPL(public_key_subtype);