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() + "]");
}
}
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 + "]");
}
}
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());
}
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());
}
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));
}
Aggregations