Search in sources :

Example 1 with GenerateCertRequest

use of ee.ria.xroad.signer.protocol.message.GenerateCertRequest in project X-Road by nordic-institute.

the class SignerCLI method generateCertRequest.

/**
 * Generate certificate request.
 *
 * @param keyId       key id
 * @param memberId    member id
 * @param usage       usage
 * @param subjectName subject name
 * @param format      request format
 * @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, @Param(name = "format", description = "Format of request (der/pem)") String format) throws Exception {
    KeyUsageInfo keyUsage = "a".equals(usage) ? KeyUsageInfo.AUTHENTICATION : KeyUsageInfo.SIGNING;
    CertificateRequestFormat requestFormat = format.equalsIgnoreCase("der") ? CertificateRequestFormat.DER : CertificateRequestFormat.PEM;
    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);
    logData.put(CSR_FORMAT_PARAM, requestFormat.name());
    GenerateCertRequestResponse response;
    try {
        GenerateCertRequest request = new GenerateCertRequest(keyId, memberId, keyUsage, subjectName, requestFormat);
        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());
}
Also used : GenerateCertRequestResponse(ee.ria.xroad.signer.protocol.message.GenerateCertRequestResponse) CertificateRequestFormat(ee.ria.xroad.signer.protocol.message.CertificateRequestFormat) GenerateCertRequest(ee.ria.xroad.signer.protocol.message.GenerateCertRequest) KeyUsageInfo(ee.ria.xroad.signer.protocol.dto.KeyUsageInfo) IOException(java.io.IOException) CLIException(asg.cliche.CLIException) LinkedHashMap(java.util.LinkedHashMap) Command(asg.cliche.Command)

Example 2 with GenerateCertRequest

use of ee.ria.xroad.signer.protocol.message.GenerateCertRequest in project X-Road by nordic-institute.

the class SignerProxy method generateCertRequest.

/**
 * 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
 * @param format the format of the request
 * @return GeneratedCertRequestInfo containing details and content of the certificate request
 * @throws Exception if any errors occur
 */
public static GeneratedCertRequestInfo generateCertRequest(String keyId, ClientId memberId, KeyUsageInfo keyUsage, String subjectName, CertificateRequestFormat format) throws Exception {
    GenerateCertRequestResponse response = execute(new GenerateCertRequest(keyId, memberId, keyUsage, subjectName, format));
    byte[] certRequestBytes = response.getCertRequest();
    log.trace("Cert request with length of {} bytes generated", certRequestBytes.length);
    return new GeneratedCertRequestInfo(response.getCertReqId(), response.getCertRequest(), response.getFormat(), memberId, keyUsage);
}
Also used : GenerateCertRequestResponse(ee.ria.xroad.signer.protocol.message.GenerateCertRequestResponse) GenerateCertRequest(ee.ria.xroad.signer.protocol.message.GenerateCertRequest)

Aggregations

GenerateCertRequest (ee.ria.xroad.signer.protocol.message.GenerateCertRequest)2 GenerateCertRequestResponse (ee.ria.xroad.signer.protocol.message.GenerateCertRequestResponse)2 CLIException (asg.cliche.CLIException)1 Command (asg.cliche.Command)1 KeyUsageInfo (ee.ria.xroad.signer.protocol.dto.KeyUsageInfo)1 CertificateRequestFormat (ee.ria.xroad.signer.protocol.message.CertificateRequestFormat)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1