secret_keyring.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * QEMU crypto secret support
  3. *
  4. * Copyright 2020 Yandex N.V.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include <asm/unistd.h>
  22. #include <linux/keyctl.h>
  23. #include "qapi/error.h"
  24. #include "qom/object_interfaces.h"
  25. #include "trace.h"
  26. #include "crypto/secret_keyring.h"
  27. static inline
  28. long keyctl_read(int32_t key, uint8_t *buffer, size_t buflen)
  29. {
  30. return syscall(__NR_keyctl, KEYCTL_READ, key, buffer, buflen, 0);
  31. }
  32. static void
  33. qcrypto_secret_keyring_load_data(QCryptoSecretCommon *sec_common,
  34. uint8_t **output,
  35. size_t *outputlen,
  36. Error **errp)
  37. {
  38. QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(sec_common);
  39. uint8_t *buffer = NULL;
  40. long retcode;
  41. *output = NULL;
  42. *outputlen = 0;
  43. if (!secret->serial) {
  44. error_setg(errp, "'serial' parameter must be provided");
  45. return;
  46. }
  47. retcode = keyctl_read(secret->serial, NULL, 0);
  48. if (retcode <= 0) {
  49. goto keyctl_error;
  50. }
  51. buffer = g_new0(uint8_t, retcode);
  52. retcode = keyctl_read(secret->serial, buffer, retcode);
  53. if (retcode < 0) {
  54. g_free(buffer);
  55. goto keyctl_error;
  56. }
  57. *outputlen = retcode;
  58. *output = buffer;
  59. return;
  60. keyctl_error:
  61. error_setg_errno(errp, errno,
  62. "Unable to read serial key %08x",
  63. secret->serial);
  64. }
  65. static void
  66. qcrypto_secret_prop_set_key(Object *obj, Visitor *v,
  67. const char *name, void *opaque,
  68. Error **errp)
  69. {
  70. QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(obj);
  71. int32_t value;
  72. visit_type_int32(v, name, &value, errp);
  73. if (!value) {
  74. error_setg(errp, "'serial' should not be equal to 0");
  75. }
  76. secret->serial = value;
  77. }
  78. static void
  79. qcrypto_secret_prop_get_key(Object *obj, Visitor *v,
  80. const char *name, void *opaque,
  81. Error **errp)
  82. {
  83. QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(obj);
  84. int32_t value = secret->serial;
  85. visit_type_int32(v, name, &value, errp);
  86. }
  87. static void
  88. qcrypto_secret_keyring_complete(UserCreatable *uc, Error **errp)
  89. {
  90. object_property_set_bool(OBJECT(uc), "loaded", true, errp);
  91. }
  92. static void
  93. qcrypto_secret_keyring_class_init(ObjectClass *oc, void *data)
  94. {
  95. QCryptoSecretCommonClass *sic = QCRYPTO_SECRET_COMMON_CLASS(oc);
  96. sic->load_data = qcrypto_secret_keyring_load_data;
  97. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  98. ucc->complete = qcrypto_secret_keyring_complete;
  99. object_class_property_add(oc, "serial", "int32_t",
  100. qcrypto_secret_prop_get_key,
  101. qcrypto_secret_prop_set_key,
  102. NULL, NULL);
  103. }
  104. static const TypeInfo qcrypto_secret_info = {
  105. .parent = TYPE_QCRYPTO_SECRET_COMMON,
  106. .name = TYPE_QCRYPTO_SECRET_KEYRING,
  107. .instance_size = sizeof(QCryptoSecretKeyring),
  108. .class_size = sizeof(QCryptoSecretKeyringClass),
  109. .class_init = qcrypto_secret_keyring_class_init,
  110. .interfaces = (InterfaceInfo[]) {
  111. { TYPE_USER_CREATABLE },
  112. { }
  113. }
  114. };
  115. static void
  116. qcrypto_secret_register_types(void)
  117. {
  118. type_register_static(&qcrypto_secret_info);
  119. }
  120. type_init(qcrypto_secret_register_types);