Search in sources :

Example 91 with Session

use of iaik.pkcs.pkcs11.Session in project xipki by xipki.

the class IaikP11Slot method singleSign.

private byte[] singleSign(Session session, long mechanism, P11Params parameters, byte[] content, IaikP11Identity identity) throws P11TokenException {
    Key signingKey = identity.getSigningKey();
    Mechanism mechanismObj = getMechanism(mechanism, parameters);
    if (LOG.isTraceEnabled()) {
        LOG.debug("sign with signing key:\n{}", signingKey);
    }
    byte[] signature;
    try {
        session.signInit(mechanismObj, signingKey);
        signature = session.sign(content);
    } catch (TokenException ex) {
        throw new P11TokenException(ex.getMessage(), ex);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("signature:\n{}", hex(signature));
    }
    return signature;
}
Also used : P11TokenException(org.xipki.security.exception.P11TokenException) TokenException(iaik.pkcs.pkcs11.TokenException) P11TokenException(org.xipki.security.exception.P11TokenException) RSAPrivateKey(iaik.pkcs.pkcs11.objects.RSAPrivateKey) ECPrivateKey(iaik.pkcs.pkcs11.objects.ECPrivateKey) SM2PrivateKey(iaik.pkcs.pkcs11.objects.SM2PrivateKey) ValuedSecretKey(iaik.pkcs.pkcs11.objects.ValuedSecretKey) Key(iaik.pkcs.pkcs11.objects.Key) DSAPublicKey(iaik.pkcs.pkcs11.objects.DSAPublicKey) RSAPublicKey(iaik.pkcs.pkcs11.objects.RSAPublicKey) SM2PublicKey(iaik.pkcs.pkcs11.objects.SM2PublicKey) ECPublicKey(iaik.pkcs.pkcs11.objects.ECPublicKey) PrivateKey(iaik.pkcs.pkcs11.objects.PrivateKey) DSAPrivateKey(iaik.pkcs.pkcs11.objects.DSAPrivateKey) PublicKey(iaik.pkcs.pkcs11.objects.PublicKey) SecretKey(iaik.pkcs.pkcs11.objects.SecretKey) Mechanism(iaik.pkcs.pkcs11.Mechanism)

Example 92 with Session

use of iaik.pkcs.pkcs11.Session in project xipki by xipki.

the class IaikP11Slot method idExists.

private static boolean idExists(Session session, byte[] keyId) throws P11TokenException {
    Key key = new Key();
    key.getId().setByteArrayValue(keyId);
    Object[] objects;
    try {
        session.findObjectsInit(key);
        objects = session.findObjects(1);
        if (objects.length > 0) {
            return true;
        }
    } catch (TokenException ex) {
        throw new P11TokenException(ex.getMessage(), ex);
    } finally {
        try {
            session.findObjectsFinal();
        } catch (TokenException ex) {
            LogUtil.error(LOG, ex, "session.findObjectsFinal() failed");
        }
    }
    X509PublicKeyCertificate cert = new X509PublicKeyCertificate();
    cert.getId().setByteArrayValue(keyId);
    try {
        session.findObjectsInit(cert);
        objects = session.findObjects(1);
    } catch (TokenException ex) {
        throw new P11TokenException(ex.getMessage(), ex);
    } finally {
        try {
            session.findObjectsFinal();
        } catch (TokenException ex) {
            LogUtil.error(LOG, ex, "session.findObjectsFinal() failed");
        }
    }
    return objects.length > 0;
}
Also used : P11TokenException(org.xipki.security.exception.P11TokenException) TokenException(iaik.pkcs.pkcs11.TokenException) P11TokenException(org.xipki.security.exception.P11TokenException) PKCS11Object(iaik.pkcs.pkcs11.objects.PKCS11Object) X509PublicKeyCertificate(iaik.pkcs.pkcs11.objects.X509PublicKeyCertificate) RSAPrivateKey(iaik.pkcs.pkcs11.objects.RSAPrivateKey) ECPrivateKey(iaik.pkcs.pkcs11.objects.ECPrivateKey) SM2PrivateKey(iaik.pkcs.pkcs11.objects.SM2PrivateKey) ValuedSecretKey(iaik.pkcs.pkcs11.objects.ValuedSecretKey) Key(iaik.pkcs.pkcs11.objects.Key) DSAPublicKey(iaik.pkcs.pkcs11.objects.DSAPublicKey) RSAPublicKey(iaik.pkcs.pkcs11.objects.RSAPublicKey) SM2PublicKey(iaik.pkcs.pkcs11.objects.SM2PublicKey) ECPublicKey(iaik.pkcs.pkcs11.objects.ECPublicKey) PrivateKey(iaik.pkcs.pkcs11.objects.PrivateKey) DSAPrivateKey(iaik.pkcs.pkcs11.objects.DSAPrivateKey) PublicKey(iaik.pkcs.pkcs11.objects.PublicKey) SecretKey(iaik.pkcs.pkcs11.objects.SecretKey)

Example 93 with Session

use of iaik.pkcs.pkcs11.Session in project xipki by xipki.

the class IaikP11Slot method firstLogin.

private void firstLogin(Session session, List<char[]> password) throws P11TokenException {
    try {
        boolean isProtectedAuthenticationPath = session.getToken().getTokenInfo().isProtectedAuthenticationPath();
        if (isProtectedAuthenticationPath || CollectionUtil.isEmpty(password)) {
            LOG.info("verify on PKCS11Module with PROTECTED_AUTHENTICATION_PATH");
            singleLogin(session, null);
        } else {
            LOG.info("verify on PKCS11Module with PIN");
            for (char[] singlePwd : password) {
                singleLogin(session, singlePwd);
            }
            this.password = password;
        }
    } catch (PKCS11Exception ex) {
        // 0x100: user already logged in
        if (ex.getErrorCode() != 0x100) {
            throw new P11TokenException(ex.getMessage(), ex);
        }
    } catch (TokenException ex) {
        throw new P11TokenException(ex.getMessage(), ex);
    }
}
Also used : PKCS11Exception(iaik.pkcs.pkcs11.wrapper.PKCS11Exception) P11TokenException(org.xipki.security.exception.P11TokenException) P11TokenException(org.xipki.security.exception.P11TokenException) TokenException(iaik.pkcs.pkcs11.TokenException)

Example 94 with Session

use of iaik.pkcs.pkcs11.Session in project xipki by xipki.

the class IaikP11Slot method getAllCertificateObjects.

private List<X509PublicKeyCertificate> getAllCertificateObjects(Session session) throws P11TokenException {
    X509PublicKeyCertificate template = new X509PublicKeyCertificate();
    List<Storage> tmpObjects = getObjects(session, template);
    List<X509PublicKeyCertificate> certs = new ArrayList<>(tmpObjects.size());
    for (PKCS11Object tmpObject : tmpObjects) {
        X509PublicKeyCertificate cert = (X509PublicKeyCertificate) tmpObject;
        certs.add(cert);
    }
    return certs;
}
Also used : Storage(iaik.pkcs.pkcs11.objects.Storage) PKCS11Object(iaik.pkcs.pkcs11.objects.PKCS11Object) ArrayList(java.util.ArrayList) X509PublicKeyCertificate(iaik.pkcs.pkcs11.objects.X509PublicKeyCertificate)

Example 95 with Session

use of iaik.pkcs.pkcs11.Session in project rdf2neo by Rothamsted.

the class CypherHandlersIT method testRelations.

/**
 * Tests {@link CyRelationLoadingHandler} to see if relations are mapped from RDF and loaded into Neo4J.
 */
@Test
public void testRelations() throws Exception {
    try (Driver neoDriver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "test"));
        CyRelationLoadingHandler handler = new CyRelationLoadingHandler();
        RdfDataManager rdfMgr = new RdfDataManager(RdfDataManagerTest.TDB_PATH);
        Neo4jDataManager neoMgr = new Neo4jDataManager(neoDriver)) {
        handler.setRdfDataManager(rdfMgr);
        handler.setNeo4jDataManager(neoMgr);
        handler.setRelationTypesSparql(RdfDataManagerTest.SPARQL_REL_TYPES);
        handler.setRelationPropsSparql(RdfDataManagerTest.SPARQL_REL_PROPS);
        Set<QuerySolution> relSparqlRows = new HashSet<>();
        Dataset dataSet = rdfMgr.getDataSet();
        Txn.executeRead(dataSet, () -> SparqlUtils.select(RdfDataManagerTest.SPARQL_REL_TYPES, rdfMgr.getDataSet().getDefaultModel()).forEachRemaining(row -> relSparqlRows.add(row)));
        handler.accept(relSparqlRows);
        Session session = neoDriver.session(AccessMode.READ);
        StatementResult cursor = session.run("MATCH ()-[r]->() RETURN COUNT ( r ) AS ct");
        Assert.assertEquals("Wrong count for relations", 3, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH p = (:TestNode{ iri:$iri1 })-[:relatedTo]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) AS ct", parameters("iri1", iri("ex:1"), "iri2", iri("ex:2")));
        Assert.assertEquals("Wrong count for {1 relatedTo 2}!", 1, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH p = (:SuperTestNode{ iri:$iri1 })-[:derivedFrom]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) AS ct", parameters("iri1", iri("ex:3"), "iri2", iri("ex:1")));
        Assert.assertEquals("Wrong count for {3 derivedFrom 1}!", 1, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH (:TestNode{ iri:$iri1 })-[r:relatedTo]->(:AdditionalLabel{ iri:$iri2 }) RETURN r.note AS note", parameters("iri1", iri("ex:2"), "iri2", iri("ex:3")));
        assertTrue("{2 relatedTo 3} not found!", cursor.hasNext());
        Set<String> values = cursor.next().get("note").asList().stream().map(v -> (String) v).collect(Collectors.toSet());
        Set<String> refValues = new HashSet<>(Arrays.asList(new String[] { "Reified Relation", "Another Note" }));
        assertTrue("reified relation, wrong property value for 'note'!", Sets.difference(values, refValues).isEmpty());
    }
}
Also used : SparqlUtils(info.marcobrandizi.rdfutils.jena.SparqlUtils) Arrays(java.util.Arrays) Driver(org.neo4j.driver.v1.Driver) BeforeClass(org.junit.BeforeClass) AccessMode(org.neo4j.driver.v1.AccessMode) LoggerFactory(org.slf4j.LoggerFactory) HashSet(java.util.HashSet) QuerySolution(org.apache.jena.query.QuerySolution) Session(org.neo4j.driver.v1.Session) Txn(org.apache.jena.system.Txn) Values.parameters(org.neo4j.driver.v1.Values.parameters) Resource(org.apache.jena.rdf.model.Resource) Map(java.util.Map) GraphDatabase(org.neo4j.driver.v1.GraphDatabase) NamespaceUtils.iri(info.marcobrandizi.rdfutils.namespaces.NamespaceUtils.iri) Dataset(org.apache.jena.query.Dataset) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) AuthTokens(org.neo4j.driver.v1.AuthTokens) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Stream(java.util.stream.Stream) StatementResult(org.neo4j.driver.v1.StatementResult) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) StatementResult(org.neo4j.driver.v1.StatementResult) Dataset(org.apache.jena.query.Dataset) Driver(org.neo4j.driver.v1.Driver) QuerySolution(org.apache.jena.query.QuerySolution) HashSet(java.util.HashSet) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Aggregations

Session (com.trilead.ssh2.Session)42 Session (org.neo4j.driver.v1.Session)38 Connection (com.trilead.ssh2.Connection)32 IOException (java.io.IOException)29 Test (org.junit.Test)29 InputStream (java.io.InputStream)27 Driver (org.neo4j.driver.v1.Driver)27 StatementResult (org.neo4j.driver.v1.StatementResult)20 TokenException (iaik.pkcs.pkcs11.TokenException)15 P11TokenException (org.xipki.security.exception.P11TokenException)15 Record (org.neo4j.driver.v1.Record)12 Session (iaik.pkcs.pkcs11.Session)10 SecretKey (iaik.pkcs.pkcs11.objects.SecretKey)10 ValuedSecretKey (iaik.pkcs.pkcs11.objects.ValuedSecretKey)10 DSAPrivateKey (iaik.pkcs.pkcs11.objects.DSAPrivateKey)9 ECPrivateKey (iaik.pkcs.pkcs11.objects.ECPrivateKey)9 PrivateKey (iaik.pkcs.pkcs11.objects.PrivateKey)9 RSAPrivateKey (iaik.pkcs.pkcs11.objects.RSAPrivateKey)9 SM2PrivateKey (iaik.pkcs.pkcs11.objects.SM2PrivateKey)9 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)9