Search in sources :

Example 41 with KeyInfo

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

the class ImportCertRequestHandler method importCertificate.

private String importCertificate(X509Certificate cert, String initialStatus, ClientId memberId) throws Exception {
    String publicKey = encodeBase64(cert.getPublicKey().getEncoded());
    // Find the key based on the public key of the cert
    for (TokenInfo tokenInfo : TokenManager.listTokens()) {
        for (KeyInfo keyInfo : tokenInfo.getKeyInfo()) {
            if (matchesPublicKeyOrExistingCert(publicKey, cert, keyInfo)) {
                String keyId = keyInfo.getId();
                log.debug("Importing certificate under key '{}'", keyId);
                importCertificateToKey(keyInfo, cert, initialStatus, memberId);
                return keyId;
            }
        }
    }
    throw CodedException.tr(X_KEY_NOT_FOUND, "key_not_found_for_certificate", "Could not find key that has public key that matches the " + "public key of certificate");
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) TokenInfo(ee.ria.xroad.signer.protocol.dto.TokenInfo)

Example 42 with KeyInfo

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

the class TokenManagerMergeTest method shouldAddOcspResponse.

@Test
public void shouldAddOcspResponse() throws IOException {
    assertTrue("test setup failure", Files.exists(ADDED_KEY_FILE_PATH));
    final String testKeyId = "70726f6475636572";
    KeyInfo beforeKeyInfo = TokenManager.getKeyInfo(testKeyId);
    assertNotNull("test setup failure", beforeKeyInfo);
    final String testCertId = "e82e0b2b184d4387c2afd83708d4cfeaeb872cf7";
    CertificateInfo beforeCertInfo = TokenManager.getCertificateInfo(testCertId);
    assertNotNull("test setup failure", beforeCertInfo);
    // assert no ocsp response exists before test
    assertNull("test setup failure", beforeCertInfo.getOcspBytes());
    OCSPResp shouldMatchResponse = mock(OCSPResp.class);
    final byte[] shouldMatchOcspResponseBytes = "some example string  11 2 34".getBytes();
    when(shouldMatchResponse.getEncoded()).thenReturn(shouldMatchOcspResponseBytes);
    TokenManager.setOcspResponse(testCertId, shouldMatchResponse);
    final int beforeCertCount = TokenManager.getAllCerts().size();
    Files.copy(ADDED_KEY_CERT_FILE_PATH, testingFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    TokenManager.merge(addedCerts -> {
    });
    // make sure the merge actually reads the file, otherwise the ocsp response will of course be there
    assertEquals("merge did not add expected cert", beforeCertCount + 1, TokenManager.getAllCerts().size());
    assertArrayEquals("ocsp response bytes does not match", shouldMatchOcspResponseBytes, TokenManager.getCertificateInfo(testCertId).getOcspBytes());
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) CertificateInfo(ee.ria.xroad.signer.protocol.dto.CertificateInfo) OCSPResp(org.bouncycastle.cert.ocsp.OCSPResp) Test(org.junit.Test)

Example 43 with KeyInfo

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

the class TokenManager method getKeyInfo.

/**
 * @param clientId the client id
 * @return the list of keys for the given client id
 */
public static synchronized List<KeyInfo> getKeyInfo(ClientId clientId) {
    log.trace("getKeyInfo({})", clientId);
    List<KeyInfo> keyInfo = new ArrayList<>();
    for (Token token : currentTokens) {
        if (token.isInActive()) {
            // Ignore inactive (not usable) tokens
            continue;
        }
        for (Key key : token.getKeys()) {
            if (!key.isValidForSigning()) {
                // Ignore authentication keys
                continue;
            }
            for (Cert cert : key.getCerts()) {
                if (cert.isInvalid()) {
                    // Ignore inactive and invalid certificates
                    continue;
                }
                if (certBelongsToMember(cert.toDTO(), clientId)) {
                    log.debug("Found key '{}' for client '{}'", key.getId(), cert.getMemberId());
                    keyInfo.add(key.toDTO());
                }
            }
        }
    }
    return keyInfo;
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) ArrayList(java.util.ArrayList) Token(ee.ria.xroad.signer.model.Token) Cert(ee.ria.xroad.signer.model.Cert) Key(ee.ria.xroad.signer.model.Key) TokenAndKey(ee.ria.xroad.signer.util.TokenAndKey)

Example 44 with KeyInfo

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

the class SoftwareTokenWorker method updateKeys.

private void updateKeys() throws Exception {
    for (KeyInfo keyInfo : listKeys(tokenId)) {
        String keyId = keyInfo.getId();
        setKeyAvailable(keyId, true);
        if (privateKeys.containsKey(keyId)) {
            continue;
        }
        try {
            initializePrivateKey(keyId);
        } catch (Exception e) {
            setKeyAvailable(keyId, false);
            log.trace("Failed to load private key from key store", e);
        }
    }
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CodedException(ee.ria.xroad.common.CodedException)

Example 45 with KeyInfo

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

the class SignerProxy method generateKey.

/**
 * Generate a new key for the token with the given ID.
 * @param tokenId ID of the token
 * @param keyLabel label of the key
 * @return generated key KeyInfo object
 * @throws Exception if any errors occur
 */
public static KeyInfo generateKey(String tokenId, String keyLabel) throws Exception {
    log.trace("Generating key for token '{}'", tokenId);
    KeyInfo keyInfo = execute(new GenerateKey(tokenId, keyLabel));
    log.trace("Received key with keyId '{}' and public key '{}'", keyInfo.getId(), keyInfo.getPublicKey());
    return keyInfo;
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) GenerateKey(ee.ria.xroad.signer.protocol.message.GenerateKey)

Aggregations

KeyInfo (ee.ria.xroad.signer.protocol.dto.KeyInfo)58 TokenInfo (ee.ria.xroad.signer.protocol.dto.TokenInfo)32 CertificateInfo (ee.ria.xroad.signer.protocol.dto.CertificateInfo)17 Test (org.junit.Test)16 CodedException (ee.ria.xroad.common.CodedException)12 TokenTestUtils (org.niis.xroad.securityserver.restapi.util.TokenTestUtils)12 CertRequestInfo (ee.ria.xroad.signer.protocol.dto.CertRequestInfo)9 TokenInfoAndKeyId (ee.ria.xroad.signer.protocol.dto.TokenInfoAndKeyId)9 Before (org.junit.Before)9 ArrayList (java.util.ArrayList)7 ClientId (ee.ria.xroad.common.identifier.ClientId)6 DeviationAwareRuntimeException (org.niis.xroad.restapi.exceptions.DeviationAwareRuntimeException)6 SignerNotReachableException (org.niis.xroad.restapi.service.SignerNotReachableException)6 KeyUsageInfo (ee.ria.xroad.signer.protocol.dto.KeyUsageInfo)5 HashMap (java.util.HashMap)5 ResourceNotFoundException (org.niis.xroad.restapi.openapi.ResourceNotFoundException)5 AuthKeyInfo (ee.ria.xroad.signer.protocol.dto.AuthKeyInfo)4 TokenManager.getKeyInfo (ee.ria.xroad.signer.tokenmanager.TokenManager.getKeyInfo)4 CertificateTestUtils (org.niis.xroad.securityserver.restapi.util.CertificateTestUtils)4 GeneratedCertRequestInfo (ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo)3