Search in sources :

Example 1 with ImportCert

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

the class SignerCLI method importCertificate.

/**
 * Imports a certificate.
 *
 * @param file     file
 * @param clientId client id
 * @throws Exception if an error occurs
 */
@Command(description = "Imports a certificate")
public void importCertificate(@Param(name = "file", description = "Certificate file (PEM)") String file, @Param(name = "clientId", description = "Member identifier") ClientId clientId) throws Exception {
    Map<String, Object> logData = new LinkedHashMap<>();
    logData.put(CERT_FILE_NAME_PARAM, file);
    logData.put(CLIENT_IDENTIFIER_PARAM, clientId);
    try {
        byte[] certBytes = fileToBytes(file);
        ImportCertResponse response = SignerClient.execute(new ImportCert(certBytes, CertificateInfo.STATUS_REGISTERED, clientId));
        logData.put(KEY_ID_PARAM, response.getKeyId());
        AuditLogger.log(IMPORT_A_CERTIFICATE_FROM_THE_FILE, XROAD_USER, logData);
        System.out.println(response.getKeyId());
    } catch (Exception e) {
        AuditLogger.log(IMPORT_A_CERTIFICATE_FROM_THE_FILE, XROAD_USER, e.getMessage(), logData);
        System.out.println("ERROR: " + e);
    }
}
Also used : ImportCertResponse(ee.ria.xroad.signer.protocol.message.ImportCertResponse) IOException(java.io.IOException) CLIException(asg.cliche.CLIException) LinkedHashMap(java.util.LinkedHashMap) ImportCert(ee.ria.xroad.signer.protocol.message.ImportCert) Command(asg.cliche.Command)

Example 2 with ImportCert

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

the class SignerProxy method importCert.

/**
 * Imports the given byte array as a new certificate with the provided initial status and owner client ID.
 * @param certBytes byte content of the new certificate
 * @param initialStatus initial status of the certificate
 * @param clientId client ID of the certificate owner
 * @return key ID of the new certificate as a String
 * @throws Exception if any errors occur
 */
public static String importCert(byte[] certBytes, String initialStatus, ClientId clientId) throws Exception {
    log.trace("Importing cert from file with length of '{}' bytes", certBytes.length);
    ImportCertResponse response = execute(new ImportCert(certBytes, initialStatus, clientId));
    log.trace("Cert imported successfully, keyId received: {}", response.getKeyId());
    return response.getKeyId();
}
Also used : ImportCertResponse(ee.ria.xroad.signer.protocol.message.ImportCertResponse) ImportCert(ee.ria.xroad.signer.protocol.message.ImportCert)

Example 3 with ImportCert

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

the class GenerateSelfSignedCertRequestHandler method handle.

@Override
protected Object handle(GenerateSelfSignedCert message) throws Exception {
    TokenAndKey tokenAndKey = TokenManager.findTokenAndKey(message.getKeyId());
    if (!TokenManager.isKeyAvailable(tokenAndKey.getKeyId())) {
        throw keyNotAvailable(tokenAndKey.getKeyId());
    }
    if (tokenAndKey.getKey().getPublicKey() == null) {
        throw new CodedException(X_INTERNAL_ERROR, "Key '%s' has no public key", message.getKeyId());
    }
    PublicKey pk = readX509PublicKey(decodeBase64(tokenAndKey.getKey().getPublicKey()));
    String signAlgoId = CryptoUtils.getSignatureAlgorithmId(SIGNATURE_DIGEST_ALGORITHM, tokenAndKey.getSignMechanism());
    X509Certificate cert = new DummyCertBuilder().build(tokenAndKey, message, pk, signAlgoId);
    byte[] certData = cert.getEncoded();
    importCert(new ImportCert(certData, CertificateInfo.STATUS_REGISTERED, message.getMemberId()));
    return new GenerateSelfSignedCertResponse(certData);
}
Also used : CodedException(ee.ria.xroad.common.CodedException) CryptoUtils.readX509PublicKey(ee.ria.xroad.common.util.CryptoUtils.readX509PublicKey) PublicKey(java.security.PublicKey) TokenAndKey(ee.ria.xroad.signer.util.TokenAndKey) GenerateSelfSignedCertResponse(ee.ria.xroad.signer.protocol.message.GenerateSelfSignedCertResponse) X509Certificate(java.security.cert.X509Certificate) ImportCert(ee.ria.xroad.signer.protocol.message.ImportCert)

Aggregations

ImportCert (ee.ria.xroad.signer.protocol.message.ImportCert)3 ImportCertResponse (ee.ria.xroad.signer.protocol.message.ImportCertResponse)2 CLIException (asg.cliche.CLIException)1 Command (asg.cliche.Command)1 CodedException (ee.ria.xroad.common.CodedException)1 CryptoUtils.readX509PublicKey (ee.ria.xroad.common.util.CryptoUtils.readX509PublicKey)1 GenerateSelfSignedCertResponse (ee.ria.xroad.signer.protocol.message.GenerateSelfSignedCertResponse)1 TokenAndKey (ee.ria.xroad.signer.util.TokenAndKey)1 IOException (java.io.IOException)1 PublicKey (java.security.PublicKey)1 X509Certificate (java.security.cert.X509Certificate)1 LinkedHashMap (java.util.LinkedHashMap)1