mini_keyctl.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 2019 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * A tool loads keys to keyring.
  18. */
  19. #include "mini_keyctl_utils.h"
  20. #include <error.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <android-base/parseint.h>
  24. static void Usage(int exit_code) {
  25. fprintf(stderr, "usage: mini-keyctl <action> [args,]\n");
  26. fprintf(stderr, " mini-keyctl add <type> <desc> <data> <keyring>\n");
  27. fprintf(stderr, " mini-keyctl padd <type> <desc> <keyring>\n");
  28. fprintf(stderr, " mini-keyctl unlink <key> <keyring>\n");
  29. fprintf(stderr, " mini-keyctl restrict_keyring <keyring>\n");
  30. fprintf(stderr, " mini-keyctl security <key>\n");
  31. _exit(exit_code);
  32. }
  33. static key_serial_t parseKeyOrDie(const char* str) {
  34. key_serial_t key;
  35. if (!android::base::ParseInt(str, &key)) {
  36. error(1 /* exit code */, 0 /* errno */, "Unparsable key: '%s'\n", str);
  37. }
  38. return key;
  39. }
  40. int main(int argc, const char** argv) {
  41. if (argc < 2) Usage(1);
  42. const std::string action = argv[1];
  43. if (action == "add") {
  44. if (argc != 6) Usage(1);
  45. std::string type = argv[2];
  46. std::string desc = argv[3];
  47. std::string data = argv[4];
  48. std::string keyring = argv[5];
  49. return Add(type, desc, data, keyring);
  50. } else if (action == "padd") {
  51. if (argc != 5) Usage(1);
  52. std::string type = argv[2];
  53. std::string desc = argv[3];
  54. std::string keyring = argv[4];
  55. return Padd(type, desc, keyring);
  56. } else if (action == "restrict_keyring") {
  57. if (argc != 3) Usage(1);
  58. std::string keyring = argv[2];
  59. return RestrictKeyring(keyring);
  60. } else if (action == "unlink") {
  61. if (argc != 4) Usage(1);
  62. key_serial_t key = parseKeyOrDie(argv[2]);
  63. const std::string keyring = argv[3];
  64. return Unlink(key, keyring);
  65. } else if (action == "security") {
  66. if (argc != 3) Usage(1);
  67. const char* key_str = argv[2];
  68. key_serial_t key = parseKeyOrDie(key_str);
  69. std::string context = RetrieveSecurityContext(key);
  70. if (context.empty()) {
  71. perror(key_str);
  72. return 1;
  73. }
  74. fprintf(stderr, "%s\n", context.c_str());
  75. return 0;
  76. } else {
  77. fprintf(stderr, "Unrecognized action: %s\n", action.c_str());
  78. Usage(1);
  79. }
  80. return 0;
  81. }