ws_codesignService.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "ws_codesignService.hpp"
  14. #include "jutil.hpp"
  15. Cws_codesignEx::Cws_codesignEx()
  16. {
  17. }
  18. Cws_codesignEx::~Cws_codesignEx()
  19. {
  20. }
  21. void Cws_codesignEx::init(IPropertyTree *cfg, const char *process, const char *service)
  22. {
  23. if(cfg == nullptr)
  24. throw MakeStringException(-1, "Cannot initialize Cws_codesignEx, cfg is NULL");
  25. StringBuffer xpath;
  26. xpath.appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]", process, service);
  27. m_serviceCfg.setown(cfg->getPropTree(xpath.str()));
  28. StringBuffer output, errmsg;
  29. int ret = runExternalCommand(output, errmsg, "gpg --version", "");
  30. if (ret != 0)
  31. throw MakeStringException(-1, "Error running gpg: %s", errmsg.str());
  32. isGPGv1 = strstr(output.str(), "gpg (GnuPG) 1.");
  33. }
  34. bool Cws_codesignEx::onSign(IEspContext &context, IEspSignRequest &req, IEspSignResponse &resp)
  35. {
  36. resp.setRetCode(-1);
  37. StringBuffer keyid(req.getKeyIdentifier());
  38. keyid.trim();
  39. const char* text = req.getText();
  40. if (keyid.length() == 0 || !text || !*text)
  41. {
  42. resp.setErrMsg("Please provide both KeyIdentifier and Text");
  43. return false;
  44. }
  45. if (strstr(keyid.str(), "\""))
  46. {
  47. resp.setErrMsg("Invalid KeyIdentifier");
  48. return false;
  49. }
  50. StringBuffer cmd, output, errmsg;
  51. if (isGPGv1)
  52. cmd.appendf("gpg --list-secret-keys \"%s\"", keyid.str());
  53. else
  54. cmd.appendf("gpg --list-secret-keys --with-keygrip \"%s\"", keyid.str());
  55. int ret = runExternalCommand(output, errmsg, cmd.str(), "");
  56. if (ret != 0 || strstr(output.str(), keyid.str()) == nullptr)
  57. {
  58. resp.setErrMsg("Key not found");
  59. return false;
  60. }
  61. if (!isGPGv1)
  62. {
  63. StringBuffer keygrip;
  64. auto kgptr = strstr(output.str(), "Keygrip = ");
  65. if (kgptr)
  66. keygrip.append(40, kgptr+10);
  67. if (keygrip.length() > 0)
  68. {
  69. output.clear();
  70. errmsg.clear();
  71. cmd.clear().appendf("gpg-connect-agent \"clear_passphrase --mode=normal %s\" /bye", keygrip.str());
  72. runExternalCommand(output, errmsg, cmd.str(), "");
  73. }
  74. }
  75. output.clear();
  76. errmsg.clear();
  77. cmd.clear().appendf("gpg --clearsign -u \"%s\" --yes --batch --passphrase-fd 0", keyid.str());
  78. if (!isGPGv1)
  79. cmd.append(" --pinentry-mode loopback");
  80. VStringBuffer input("%s\n", req.getKeyPass());
  81. input.append(text);
  82. ret = runExternalCommand(output, errmsg, cmd.str(), input.str());
  83. if (ret != 0 || output.length() == 0)
  84. {
  85. UERRLOG("gpg clearsign error: [%d] %s\nOutput: n%s", ret, errmsg.str(), output.str());
  86. resp.setErrMsg("Failed to sign text, please check service log for details");
  87. return false;
  88. }
  89. resp.setRetCode(0);
  90. resp.setSignedText(output.str());
  91. return true;
  92. }