Search in sources :

Example 1 with CertificateStoreException

use of org.xwiki.crypto.store.CertificateStoreException 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);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) X509CertifiedPublicKey(org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey) CertificateStoreException(org.xwiki.crypto.store.CertificateStoreException) CertificateObjectReference(org.xwiki.crypto.store.wiki.internal.query.CertificateObjectReference) XWikiException(com.xpn.xwiki.XWikiException) CertificateStoreException(org.xwiki.crypto.store.CertificateStoreException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 2 with CertificateStoreException

use of org.xwiki.crypto.store.CertificateStoreException in project xwiki-platform by xwiki.

the class X509KeyWikiStore method storeKeyPair.

private void storeKeyPair(StoreReference store, CertifiedPublicKey certificate, byte[] privateKey) throws KeyStoreException {
    XWikiContext context = getXWikiContext();
    XWikiDocument document;
    try {
        document = storeCertificate(store, certificate, context);
    } catch (CertificateStoreException e) {
        throw new KeyStoreException("Error while preparing certificate to store a key pair in [" + store + "]", e);
    }
    try {
        BaseObject obj = document.getXObject(PRIVATEKEYCLASS);
        if (obj == null) {
            obj = document.newXObject(PRIVATEKEYCLASS, context);
        }
        obj.setLargeStringValue(PRIVATEKEYCLASS_PROP_KEY, getEncoder().encode(privateKey, 64));
        context.getWiki().saveDocument(document, context);
    } catch (IOException e) {
        throw new KeyStoreException("Error while preparing private key for [" + document.getDocumentReference() + "]", e);
    } catch (XWikiException e) {
        throw new KeyStoreException("Error while saving key pair for [" + document.getDocumentReference() + "]", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) CertificateStoreException(org.xwiki.crypto.store.CertificateStoreException) KeyStoreException(org.xwiki.crypto.store.KeyStoreException) IOException(java.io.IOException) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 CertificateStoreException (org.xwiki.crypto.store.CertificateStoreException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 IOException (java.io.IOException)1 X509CertifiedPublicKey (org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey)1 KeyStoreException (org.xwiki.crypto.store.KeyStoreException)1 CertificateObjectReference (org.xwiki.crypto.store.wiki.internal.query.CertificateObjectReference)1