ws_codesignService.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. }
  29. void Cws_codesignEx::clearPassphrase(const char* key)
  30. {
  31. StringBuffer output, errmsg;
  32. VStringBuffer cmd("gpg-connect-agent \"clear_passphrase --mode=normal %s\" /bye", key);
  33. runExternalCommand(output, errmsg, cmd.str(), nullptr);
  34. }
  35. bool Cws_codesignEx::onSign(IEspContext &context, IEspSignRequest &req, IEspSignResponse &resp)
  36. {
  37. resp.setRetCode(-1);
  38. StringBuffer userid(req.getUserID());
  39. userid.trim();
  40. const char* text = req.getText();
  41. if (userid.length() == 0 || !text || !*text)
  42. {
  43. resp.setErrMsg("Please provide both UserID and Text");
  44. return false;
  45. }
  46. if (strstr(userid.str(), "\""))
  47. {
  48. resp.setErrMsg("Invalid UserID");
  49. return false;
  50. }
  51. StringBuffer cmd, output, errmsg;
  52. int ret = runExternalCommand(output, errmsg, "gpg --version", nullptr);
  53. if (ret != 0)
  54. throw MakeStringException(-1, "Error running gpg: %s", errmsg.str());
  55. bool isGPGv1 = strstr(output.str(), "gpg (GnuPG) 1.");
  56. output.clear();
  57. errmsg.clear();
  58. if (isGPGv1)
  59. cmd.appendf("gpg --list-secret-keys \"=%s\"", userid.str()); // = means exact match
  60. else
  61. cmd.appendf("gpg --list-secret-keys --with-keygrip \"=%s\"", userid.str()); // = means exact match
  62. ret = runExternalCommand(output, errmsg, cmd.str(), nullptr);
  63. if (ret != 0 || strstr(output.str(), userid.str()) == nullptr)
  64. {
  65. resp.setErrMsg("Key not found");
  66. return false;
  67. }
  68. StringBuffer keygrip;
  69. if (!isGPGv1)
  70. {
  71. auto kgptr = strstr(output.str(), "Keygrip = ");
  72. if (kgptr)
  73. keygrip.append(40, kgptr+10);
  74. if (keygrip.length() > 0)
  75. clearPassphrase(keygrip.str());
  76. }
  77. output.clear();
  78. errmsg.clear();
  79. cmd.clear().appendf("gpg --clearsign -u \"%s\" --yes --batch --passphrase-fd 0", userid.str());
  80. if (!isGPGv1)
  81. cmd.append(" --pinentry-mode loopback");
  82. VStringBuffer input("%s\n", req.getKeyPass());
  83. input.append(text);
  84. ret = runExternalCommand(output, errmsg, cmd.str(), input.str());
  85. if (ret != 0 || output.length() == 0)
  86. {
  87. UERRLOG("gpg clearsign error: [%d] %s\nOutput: n%s", ret, errmsg.str(), output.str());
  88. resp.setErrMsg("Failed to sign text, please check service log for details");
  89. return false;
  90. }
  91. resp.setRetCode(0);
  92. resp.setSignedText(output.str());
  93. if (!isGPGv1 && keygrip.length() > 0)
  94. clearPassphrase(keygrip.str());
  95. return true;
  96. }
  97. const char* skipn(const char* str, char c, int n)
  98. {
  99. for (int i = 0; i < n && str && *str; i++)
  100. {
  101. str = strchr(str, c);
  102. if (!str)
  103. break;
  104. str++;
  105. }
  106. return str;
  107. }
  108. bool Cws_codesignEx::onListUserIDs(IEspContext &context, IEspListUserIDsRequest &req, IEspListUserIDsResponse &resp)
  109. {
  110. StringBuffer output, errmsg;
  111. int ret = runExternalCommand(output, errmsg, "gpg --version", nullptr);
  112. if (ret != 0)
  113. throw MakeStringException(-1, "Error running gpg: %s", errmsg.str());
  114. bool isGPGv1 = strstr(output.str(), "gpg (GnuPG) 1.");
  115. const char* START = "\nuid:";
  116. if (isGPGv1)
  117. START = "\nsec:";
  118. int startlen = strlen(START);
  119. const int SKIP = 8;
  120. output.clear().append("\n");
  121. errmsg.clear();
  122. ret = runExternalCommand(output, errmsg, "gpg --list-secret-keys --with-colon", nullptr);
  123. if (ret != 0)
  124. throw MakeStringException(-1, "Error running gpg: %s", errmsg.str());
  125. const char* line = output.str();
  126. StringArray uids;
  127. while (line && *line)
  128. {
  129. line = strstr(line, START);
  130. if (!line)
  131. break;
  132. line += startlen;
  133. line = skipn(line, ':', SKIP);
  134. if (!line || !*line)
  135. break;
  136. const char* uid_s = line;
  137. while (*line != '\0' && *line != ':')
  138. line++;
  139. if (line > uid_s)
  140. {
  141. StringBuffer uid(line - uid_s, uid_s);
  142. uid.trim();
  143. if (uid.length() > 0)
  144. uids.append(uid.str());
  145. }
  146. }
  147. uids.sortAscii(false);
  148. const char* current = "";
  149. StringArray& respuserids = resp.getUserIDs();
  150. for (int i = 0; i < uids.length(); i++)
  151. {
  152. if (strcmp(uids.item(i), current) != 0)
  153. {
  154. current = uids.item(i);
  155. respuserids.append(current);
  156. }
  157. }
  158. return true;
  159. }