use of org.xwiki.crypto.store.wiki.internal.query.CertificateObjectReference in project xwiki-platform by xwiki.
the class AbstractX509WikiStore method storeCertificate.
/**
* Create or update a certificate into the appropriate document of the given store, and return the unsaved document.
*
* @param store the reference of a document or a space where the certificate should be stored.
* @param certificate the certificate to store.
* @param context the XWikiContext.
* @return the XWiki document to be saved where the object was updated or created.
* @throws CertificateStoreException on error.
*/
protected XWikiDocument storeCertificate(StoreReference store, CertifiedPublicKey certificate, XWikiContext context) throws CertificateStoreException {
if (!(certificate instanceof X509CertifiedPublicKey)) {
throw new IllegalArgumentException("Certificate should be X509 certificates.");
}
X509CertifiedPublicKey publicKey = (X509CertifiedPublicKey) certificate;
try {
CertificateObjectReference certRef = findCertificate(store, publicKey);
XWikiDocument document;
BaseObject obj;
if (certRef != null) {
document = getDocument(store, certRef, context);
obj = document.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, certRef.getObjectNumber());
} else {
document = context.getWiki().getDocument(getDocumentReference(store, publicKey), context);
obj = document.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, context);
byte[] keyId = publicKey.getSubjectKeyIdentifier();
if (keyId != null) {
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, this.base64.encode(keyId));
}
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, publicKey.getIssuer().getName());
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, publicKey.getSerialNumber().toString());
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, publicKey.getSubject().getName());
}
obj.setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, this.base64.encode(certificate.getEncoded(), 64));
return document;
} catch (Exception e) {
throw new CertificateStoreException("Error while preparing certificate for store [" + store + "]", e);
}
}
use of org.xwiki.crypto.store.wiki.internal.query.CertificateObjectReference 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.store.wiki.internal.query.CertificateObjectReference in project xwiki-platform by xwiki.
the class AbstractX509WikiStore method findCertificate.
/**
* Find the reference of the XObject storing a matching certificate in the given store.
*
* @param store the reference to a document or space used to store certificates.
* @param publicKey the certificate to find the storage for.
* @return a reference to the XObject storing a matching certificate, or null if none were found.
* @throws CertificateStoreException on error.
*/
protected CertificateObjectReference findCertificate(StoreReference store, X509CertifiedPublicKey publicKey) throws CertificateStoreException {
byte[] keyId = publicKey.getSubjectKeyIdentifier();
CertificateObjectReference certRef;
if (keyId != null) {
certRef = new X509CertificateReferenceKeyIdentifierQuery(resolveStore(store), this.base64, this.queryManager, this.entityReferenceSerializer).getReference(keyId);
} else {
certRef = new X509CertificateReferenceIssuerAndSerialQuery(resolveStore(store), this.base64, this.queryManager, this.entityReferenceSerializer).getReference(publicKey.getIssuer(), publicKey.getSerialNumber());
}
return certRef;
}
Aggregations