* The entry point for the "ca sign" CLI command. * * @return An exit status. */
| 57 | * @return An exit status. |
| 58 | */ |
| 59 | int CASignCommand::Run(const boost::program_options::variables_map&, const std::vector<std::string>& ap) const |
| 60 | { |
| 61 | String requestFile = ApiListener::GetCertificateRequestsDir() + "/" + ap[0] + ".json"; |
| 62 | |
| 63 | if (!Utility::PathExists(requestFile)) { |
| 64 | Log(LogCritical, "cli") |
| 65 | << "No request exists for fingerprint '" << ap[0] << "'."; |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | Dictionary::Ptr request = Utility::LoadJsonFile(requestFile); |
| 70 | |
| 71 | if (!request) |
| 72 | return 1; |
| 73 | |
| 74 | String certRequestText = request->Get("cert_request"); |
| 75 | |
| 76 | std::shared_ptr<X509> certRequest = StringToCertificate(certRequestText); |
| 77 | |
| 78 | if (!certRequest) { |
| 79 | Log(LogCritical, "cli", "Certificate request is invalid. Could not parse X.509 certificate for the 'cert_request' attribute."); |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | std::shared_ptr<X509> certResponse = CreateCertIcingaCA(certRequest); |
| 84 | |
| 85 | BIO *out = BIO_new(BIO_s_mem()); |
| 86 | X509_NAME_print_ex(out, X509_get_subject_name(certRequest.get()), 0, XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB); |
| 87 | |
| 88 | char *data; |
| 89 | long length; |
| 90 | length = BIO_get_mem_data(out, &data); |
| 91 | |
| 92 | String subject = String(data, data + length); |
| 93 | BIO_free(out); |
| 94 | |
| 95 | if (!certResponse) { |
| 96 | Log(LogCritical, "cli") |
| 97 | << "Could not sign certificate for '" << subject << "'."; |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | request->Set("cert_response", CertificateToString(certResponse)); |
| 102 | |
| 103 | Utility::SaveJsonFile(requestFile, 0600, request); |
| 104 | |
| 105 | Log(LogInformation, "cli") |
| 106 | << "Signed certificate for '" << subject << "'."; |
| 107 | |
| 108 | return 0; |
| 109 | } |
nothing calls this directly
no test coverage detected