Search in sources :

Example 1 with CertifiedKeyPair

use of org.xwiki.crypto.pkix.params.CertifiedKeyPair in project xwiki-platform by xwiki.

the class X509KeyWikiStoreTest method retrieveEncryptedPrivateKeyFromDocument.

@Test
public void retrieveEncryptedPrivateKeyFromDocument() throws Exception {
    XWikiDocument storeDoc = mock(XWikiDocument.class);
    when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
    BaseObject certObj = mock(BaseObject.class);
    when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS)).thenReturn(certObj);
    when(certObj.getLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE)).thenReturn(ENCODED_CERTIFICATE);
    BaseObject pkObj = mock(BaseObject.class);
    when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
    when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
    CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF, PASSWORD);
    assertThat(keyPair, notNullValue());
    assertThat(keyPair.getPrivateKey(), equalTo(privateKey));
    assertThat(keyPair.getCertificate(), equalTo((CertifiedPublicKey) certificate));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) CertifiedPublicKey(org.xwiki.crypto.pkix.params.CertifiedPublicKey) X509CertifiedPublicKey(org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair) Test(org.junit.Test)

Example 2 with CertifiedKeyPair

use of org.xwiki.crypto.pkix.params.CertifiedKeyPair in project xwiki-platform by xwiki.

the class X509KeyWikiStoreTest method retrieveMissingCertificateFromSpace.

@Test
public void retrieveMissingCertificateFromSpace() throws Exception {
    CertifiedKeyPair keyPair = store.retrieve(SPACE_STORE_REF, certificate);
    assertThat(keyPair, nullValue());
}
Also used : CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair) Test(org.junit.Test)

Example 3 with CertifiedKeyPair

use of org.xwiki.crypto.pkix.params.CertifiedKeyPair in project xwiki-platform by xwiki.

the class X509KeyWikiStoreTest method setUp.

@Before
public void setUp() throws Exception {
    EntityReferenceProvider valueProvider = mock(EntityReferenceProvider.class);
    when(valueProvider.getDefaultReference(EntityType.WIKI)).thenReturn(WIKI_REFERENCE);
    when(valueProvider.getDefaultReference(EntityType.SPACE)).thenReturn(SPACE_REFERENCE);
    when(valueProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DOCUMENT_REFERENCE);
    mocker.registerComponent(EntityReferenceProvider.class, "current", valueProvider);
    Provider<XWikiContext> xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    xcontext = mock(XWikiContext.class);
    when(xcontextProvider.get()).thenReturn(xcontext);
    xwiki = mock(com.xpn.xwiki.XWiki.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    BinaryStringEncoder encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
    when(encoder.encode(PRIVATEKEY, 64)).thenReturn(ENCODED_PRIVATEKEY);
    when(encoder.decode(ENCODED_PRIVATEKEY)).thenReturn(PRIVATEKEY);
    when(encoder.encode(ENCRYPTED_PRIVATEKEY, 64)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
    when(encoder.decode(ENCODED_ENCRYPTED_PRIVATEKEY)).thenReturn(ENCRYPTED_PRIVATEKEY);
    when(encoder.encode(CERTIFICATE, 64)).thenReturn(ENCODED_CERTIFICATE);
    when(encoder.decode(ENCODED_CERTIFICATE)).thenReturn(CERTIFICATE);
    when(encoder.encode(SUBJECT_KEYID)).thenReturn(ENCODED_SUBJECTKEYID);
    when(encoder.decode(ENCODED_SUBJECTKEYID)).thenReturn(SUBJECT_KEYID);
    privateKey = mock(PrivateKeyParameters.class);
    when(privateKey.getEncoded()).thenReturn(PRIVATEKEY);
    AsymmetricKeyFactory keyFactory = mocker.getInstance(AsymmetricKeyFactory.class);
    when(keyFactory.fromPKCS8(PRIVATEKEY)).thenReturn(privateKey);
    PrivateKeyPasswordBasedEncryptor encryptor = mocker.getInstance(PrivateKeyPasswordBasedEncryptor.class);
    when(encryptor.encrypt(PASSWORD, privateKey)).thenReturn(ENCRYPTED_PRIVATEKEY);
    when(encryptor.decrypt(PASSWORD, ENCRYPTED_PRIVATEKEY)).thenReturn(privateKey);
    certificate = mock(X509CertifiedPublicKey.class);
    when(certificate.getSerialNumber()).thenReturn(SERIAL);
    when(certificate.getIssuer()).thenReturn(new DistinguishedName(ISSUER));
    when(certificate.getSubject()).thenReturn(new DistinguishedName(SUBJECT));
    when(certificate.getEncoded()).thenReturn(CERTIFICATE);
    CertificateFactory certificateFactory = mocker.getInstance(CertificateFactory.class, "X509");
    when(certificateFactory.decode(CERTIFICATE)).thenReturn(certificate);
    X509Extensions extensions = mock(X509Extensions.class);
    when(certificate.getExtensions()).thenReturn(extensions);
    when(extensions.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
    when(certificate.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
    keyPair = new CertifiedKeyPair(privateKey, certificate);
    QueryManager queryManager = mocker.getInstance(QueryManager.class);
    query = mock(Query.class);
    when(query.bindValue(any(String.class), any())).thenReturn(query);
    when(query.setWiki(WIKI)).thenReturn(query);
    when(queryManager.createQuery(any(String.class), any(String.class))).thenReturn(query);
    store = mocker.getComponentUnderTest();
}
Also used : BinaryStringEncoder(org.xwiki.crypto.BinaryStringEncoder) Query(org.xwiki.query.Query) DistinguishedName(org.xwiki.crypto.pkix.params.x509certificate.DistinguishedName) PrivateKeyParameters(org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) X509Extensions(org.xwiki.crypto.pkix.params.x509certificate.extension.X509Extensions) CertificateFactory(org.xwiki.crypto.pkix.CertificateFactory) AsymmetricKeyFactory(org.xwiki.crypto.AsymmetricKeyFactory) EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) X509CertifiedPublicKey(org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey) QueryManager(org.xwiki.query.QueryManager) PrivateKeyPasswordBasedEncryptor(org.xwiki.crypto.password.PrivateKeyPasswordBasedEncryptor) CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair) Before(org.junit.Before)

Example 4 with CertifiedKeyPair

use of org.xwiki.crypto.pkix.params.CertifiedKeyPair in project xwiki-platform by xwiki.

the class X509KeyWikiStoreTest method retrieveEncryptedPrivateKeyFromSpace.

@Test
public void retrieveEncryptedPrivateKeyFromSpace() throws Exception {
    XWikiDocument storeDoc = mock(XWikiDocument.class);
    when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
    BaseObject pkObj = mock(BaseObject.class);
    when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
    when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
    when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[] { "space." + ENCODED_SUBJECTKEYID, 0 }));
    CertifiedKeyPair keyPair = store.retrieve(SPACE_STORE_REF, certificate, PASSWORD);
    assertThat(keyPair, notNullValue());
    assertThat(keyPair.getPrivateKey(), equalTo(privateKey));
    assertThat(keyPair.getCertificate(), equalTo((CertifiedPublicKey) certificate));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) CertifiedPublicKey(org.xwiki.crypto.pkix.params.CertifiedPublicKey) X509CertifiedPublicKey(org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair) Test(org.junit.Test)

Example 5 with CertifiedKeyPair

use of org.xwiki.crypto.pkix.params.CertifiedKeyPair in project xwiki-platform by xwiki.

the class X509KeyWikiStoreTest method retrievePrivateKeyFromSpace.

@Test
public void retrievePrivateKeyFromSpace() throws Exception {
    XWikiDocument storeDoc = mock(XWikiDocument.class);
    when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
    BaseObject pkObj = mock(BaseObject.class);
    when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
    when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_PRIVATEKEY);
    when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[] { "space." + ENCODED_SUBJECTKEYID, 0 }));
    CertifiedKeyPair keyPair = store.retrieve(SPACE_STORE_REF, certificate);
    assertThat(keyPair, notNullValue());
    assertThat(keyPair.getPrivateKey(), equalTo(privateKey));
    assertThat(keyPair.getCertificate(), equalTo((CertifiedPublicKey) certificate));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) CertifiedPublicKey(org.xwiki.crypto.pkix.params.CertifiedPublicKey) X509CertifiedPublicKey(org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair) Test(org.junit.Test)

Aggregations

CertifiedKeyPair (org.xwiki.crypto.pkix.params.CertifiedKeyPair)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 BaseObject (com.xpn.xwiki.objects.BaseObject)9 Test (org.junit.Test)8 DocumentReference (org.xwiki.model.reference.DocumentReference)7 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)7 X509CertifiedPublicKey (org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey)6 CertifiedPublicKey (org.xwiki.crypto.pkix.params.CertifiedPublicKey)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 XWikiException (com.xpn.xwiki.XWikiException)2 IOException (java.io.IOException)2 CertificateStoreException (org.xwiki.crypto.store.CertificateStoreException)2 KeyStoreException (org.xwiki.crypto.store.KeyStoreException)2 XWiki (com.xpn.xwiki.XWiki)1 Before (org.junit.Before)1 AsymmetricKeyFactory (org.xwiki.crypto.AsymmetricKeyFactory)1 BinaryStringEncoder (org.xwiki.crypto.BinaryStringEncoder)1 PrivateKeyParameters (org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters)1 PrivateKeyPasswordBasedEncryptor (org.xwiki.crypto.password.PrivateKeyPasswordBasedEncryptor)1 CertificateFactory (org.xwiki.crypto.pkix.CertificateFactory)1