Search in sources :

Example 6 with CertifiedKeyPair

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

the class X509KeyWikiStore method retrieve.

/**
 * {@inheritDoc}
 *
 * @param store an {@link org.xwiki.crypto.store.WikiStoreReference} to a space reference.
 */
@Override
public CertifiedKeyPair retrieve(StoreReference store, CertifiedPublicKey certificate, byte[] password) throws KeyStoreException {
    if (!(certificate instanceof X509CertifiedPublicKey)) {
        throw new IllegalArgumentException("Certificate should be X509 certificates.");
    }
    X509CertifiedPublicKey publicKey = (X509CertifiedPublicKey) certificate;
    XWikiContext context = getXWikiContext();
    try {
        CertificateObjectReference certRef = findCertificate(store, publicKey);
        if (certRef == null) {
            return null;
        }
        XWikiDocument document = getDocument(store, certRef, context);
        BaseObject pkObj = document.getXObject(PRIVATEKEYCLASS);
        if (pkObj == null) {
            return null;
        }
        byte[] key = getEncoder().decode(pkObj.getLargeStringValue(PRIVATEKEYCLASS_PROP_KEY));
        if (password != null) {
            return new CertifiedKeyPair(this.encryptor.decrypt(password, key), certificate);
        } else {
            return new CertifiedKeyPair(this.keyFactory.fromPKCS8(key), certificate);
        }
    } catch (Exception e) {
        throw new KeyStoreException("Failed to retrieved private key for certificate [" + publicKey.getSubject().getName() + "]");
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) X509CertifiedPublicKey(org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey) XWikiContext(com.xpn.xwiki.XWikiContext) CertificateObjectReference(org.xwiki.crypto.store.wiki.internal.query.CertificateObjectReference) KeyStoreException(org.xwiki.crypto.store.KeyStoreException) XWikiException(com.xpn.xwiki.XWikiException) IOException(java.io.IOException) CertificateStoreException(org.xwiki.crypto.store.CertificateStoreException) KeyStoreException(org.xwiki.crypto.store.KeyStoreException) BaseObject(com.xpn.xwiki.objects.BaseObject) CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair)

Example 7 with CertifiedKeyPair

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

the class X509KeyWikiStore method retrieve.

/**
 * {@inheritDoc}
 *
 * @param store an {@link org.xwiki.crypto.store.WikiStoreReference} to a document reference.
 */
@Override
public CertifiedKeyPair retrieve(StoreReference store, byte[] password) throws KeyStoreException {
    XWikiContext context = getXWikiContext();
    try {
        XWikiDocument document = context.getWiki().getDocument(getDocumentReference(store), context);
        BaseObject certObj = document.getXObject(X509CertificateWikiStore.CERTIFICATECLASS);
        BaseObject pkObj = document.getXObject(PRIVATEKEYCLASS);
        if (pkObj == null || certObj == null) {
            return null;
        }
        byte[] cert = getEncoder().decode(certObj.getLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE));
        byte[] key = getEncoder().decode(pkObj.getLargeStringValue(PRIVATEKEYCLASS_PROP_KEY));
        if (password != null) {
            return new CertifiedKeyPair(this.encryptor.decrypt(password, key), getCertificateFactory().decode(cert));
        } else {
            return new CertifiedKeyPair(this.keyFactory.fromPKCS8(key), getCertificateFactory().decode(cert));
        }
    } catch (Exception e) {
        throw new KeyStoreException("Failed to retrieved private key from [" + store + "]");
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) KeyStoreException(org.xwiki.crypto.store.KeyStoreException) XWikiException(com.xpn.xwiki.XWikiException) IOException(java.io.IOException) CertificateStoreException(org.xwiki.crypto.store.CertificateStoreException) KeyStoreException(org.xwiki.crypto.store.KeyStoreException) BaseObject(com.xpn.xwiki.objects.BaseObject) CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair)

Example 8 with CertifiedKeyPair

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

the class X509KeyWikiStoreTest method retrieveMissingPrivateKeyFromDocument.

@Test
public void retrieveMissingPrivateKeyFromDocument() 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);
    CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF);
    assertThat(keyPair, nullValue());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) 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 9 with CertifiedKeyPair

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

the class X509KeyWikiStoreTest method retrieveMissingCertificateFromDocument.

@Test
public void retrieveMissingCertificateFromDocument() throws Exception {
    XWikiDocument storeDoc = mock(XWikiDocument.class);
    when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), 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);
    CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF);
    assertThat(keyPair, nullValue());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) 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 10 with CertifiedKeyPair

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

the class X509KeyWikiStoreTest method retrievePrivateKeyFromDocument.

@Test
public void retrievePrivateKeyFromDocument() 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_PRIVATEKEY);
    CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF);
    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)

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