Example #1
0
  /**
   * Generate certificate request.
   *
   * @param keyId key id
   * @param memberId member id
   * @param usage usage
   * @param subjectName subject name
   * @throws Exception if an error occurs
   */
  @Command(description = "Generate certificate request")
  public void generateCertRequest(
      @Param(name = "keyId", description = "Key ID") String keyId,
      @Param(name = "memberId", description = "Member identifier") ClientId memberId,
      @Param(name = "usage", description = "Key usage (a - auth, s - sign)") String usage,
      @Param(name = "subjectName", description = "Subject name") String subjectName)
      throws Exception {
    KeyUsageInfo keyUsage = "a".equals(usage) ? KeyUsageInfo.AUTHENTICATION : KeyUsageInfo.SIGNING;

    Map<String, Object> logData = new LinkedHashMap<>();
    logData.put(KEY_ID_PARAM, keyId);
    logData.put(CLIENT_IDENTIFIER_PARAM, memberId);
    logData.put(KEY_USAGE_PARAM, keyUsage.name());
    logData.put(SUBJECT_NAME_PARAM, subjectName);

    GenerateCertRequestResponse response;

    try {
      GenerateCertRequest request = new GenerateCertRequest(keyId, memberId, keyUsage, subjectName);
      response = SignerClient.execute(request);

      AuditLogger.log(GENERATE_A_CERT_REQUEST_EVENT, XROAD_USER, logData);
    } catch (Exception e) {
      AuditLogger.log(GENERATE_A_CERT_REQUEST_EVENT, XROAD_USER, e.getMessage(), logData);

      throw e;
    }

    bytesToFile(keyId + ".csr", response.getCertRequest());
  }
Example #2
0
  /**
   * Generates a certificate request for the given key and with provided parameters.
   *
   * @param keyId ID of the key
   * @param memberId client ID of the certificate owner
   * @param keyUsage specifies whether the certificate is for signing or authentication
   * @param subjectName subject name of the certificate
   * @return byte content of the certificate request
   * @throws Exception if any errors occur
   */
  public static byte[] generateCertRequest(
      String keyId, ClientId memberId, KeyUsageInfo keyUsage, String subjectName) throws Exception {
    GenerateCertRequestResponse response =
        execute(new GenerateCertRequest(keyId, memberId, keyUsage, subjectName));

    byte[] certRequestBytes = response.getCertRequest();

    LOG.trace("Cert request with length of {} bytes generated", certRequestBytes.length);

    return certRequestBytes;
  }