Cifrado de contenido en Alfresco

En muchas ocasiones es necesario el cifrado del contenido en Alfresco, en este sentido ya Alfresco en la versión 4.0 puede cifrar propiedades (http://wiki.alfresco.com/wiki/Data_Encryption) y también hay un módulo para utilizar el cifrado (http://addons.alfresco.com/addons/alfresco-encryption-module).

En este caso vamos a realizar dos acciones que cifren y descifren el contenido de un documento (propiedad content) usando el algoritmo de cifrado simétrico AES. Estos sirven para la versión 3.4 de Alfresco y siguientes.

El código también se encuentra en: http://code.google.com/p/alfcrypto

Algunas cosas importantes primero: Este software es una versión alpha o beta o como queráis llamarla pero sobre todo es un código hecho de forma más o menos rápida y por tanto no hay garantía ninguna de funcionamiento, se ha probado solo con algunos documentos MS-Word y PDF. Además, ya he detectado un problema, cuando se descifra el tipo MIME en el que se guarda la copia desencriptada es plain/text (no es que no funcione, si descargais el documento se puede abrir/editar, etc) por lo que hay que incluir en el modelo de datos una propiedad que guarde el valor original y lo restaure posteriormente (ya lo haré cuando consiga algo de tiempo). Se ha realizado solamente con carácter educativo y por lo tanto no lo recomiendo para su uso en sistemas de producción sin realizar antes algunas modificaciones y pruebas.

Otro apunte más, como bien comenta Toni de la Fuente (blyx.com), existen algunas restricciones derivadas del cifrado, la primera es en la previsualización, evidentemente no existe cuando los documentos están cifrados y no tiene sentido descifrar para previsualizar ya que rompería la seguridad. Tampoco es posible la indexación full-text ya que no es posible al igual que en contenidos de imágenes (JPG, GIF, etc.), es más, es una buena idea cambiar el tipo MIME a algún tipo que Alfresco no indexe y cuando sea descifrado reponer el tipo MIME original. El uso de este sistema sería para documentos que son necesariamente obligados a ser cifrados y solo se tenga acceso a ellos por personal especial (Recursos Humanos, I+D+I, datos con carácter especial de protección de datos, etc.) que una vez descifrados (en otra ubicación a la original principalmente) sean descargados y borrados (la copia descifrada) vaciando la papelera e incluso modificando la configuración para que no sean guardados en esta. Además recomendaría que o bien el cifrado, o el descifrado se aloje en otras unidades de disco diferentes usando para ello Content Store Selector (en este último caso solo para las versiones Enterprise).

Con todo esto, comencemos:

Lo primero que hay que hacer es construir la clase de cifrado que en este caso se llamará crypto.java:

/*
 * alfcrypto is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * alfcrypto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see .
 */
package com.fegor.alfresco.security.crypto;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;

import org.apache.log4j.Logger;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class Crypto {
    private final Logger logger = Logger.getLogger(Crypto.class);

    String password = null;
    public final static int SALT_LEN = 8;
    byte[] vector_init = null;
    byte[] salt_pos = null;

    byte[] input;
    byte[] output;

    Cipher eCipher = null;
    Cipher deCipher = null;

    private final int KEYLEN_BITS = 128;
    private final int ITERATIONS = 65536;

    /**
     * Constructor
     */
    public Crypto() {
    }

    /**
     * Encryption configuration
     *
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws NoSuchPaddingException
     * @throws InvalidParameterSpecException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     * @throws InvalidKeyException
     */
    public void configEncrypt() throws NoSuchAlgorithmException,
            InvalidKeySpecException, NoSuchPaddingException,
            InvalidParameterSpecException, IllegalBlockSizeException,
            BadPaddingException, UnsupportedEncodingException,
            InvalidKeyException {
        SecretKeyFactory factory = null;
        SecretKey tmp = null;

        salt_pos = new byte[SALT_LEN];
        SecureRandom rnd = new SecureRandom();
        rnd.nextBytes(salt_pos);

        if (logger.isDebugEnabled())
            logger.debug(this.getClass().getName() + «: [salt: «
                    + (new String(Hex.encodeHex(salt_pos))) + «]»);

        factory = SecretKeyFactory.getInstance(«PBKDF2WithHmacSHA1»);

        /*
         * http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files
         * .shtml
         */
        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt_pos,
                ITERATIONS, KEYLEN_BITS);
        tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), «AES»);

        eCipher = Cipher.getInstance(«AES/CBC/PKCS5Padding»);
        eCipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = eCipher.getParameters();

        vector_init = params.getParameterSpec(IvParameterSpec.class).getIV();

        if (logger.isDebugEnabled())
            logger.debug(this.getClass().getName() + «: [vector ini: «
                    + (new String(Hex.encodeHex(vector_init))) + «]»);
    }

    /**
     * Decryption configuration
     *
     * @param initvec
     * @param salt
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws InvalidAlgorithmParameterException
     * @throws DecoderException
     */
    public void configDecrypt(String initvec, String salt)
            throws NoSuchAlgorithmException, InvalidKeySpecException,
            NoSuchPaddingException, InvalidKeyException,
            InvalidAlgorithmParameterException, DecoderException {
        SecretKeyFactory factory = null;
        SecretKey tmp = null;
        SecretKey secret = null;

        salt_pos = Hex.decodeHex(salt.toCharArray());

        if (logger.isDebugEnabled())
            logger.debug(this.getClass().getName() + «: [salt: «
                    + (new String(Hex.encodeHex(salt_pos))) + «]»);

        vector_init = Hex.decodeHex(initvec.toCharArray());
        if (logger.isDebugEnabled())
            logger.debug(this.getClass().getName() + «: [vector ini: «
                    + (new String(Hex.encodeHex(vector_init))) + «]»);

        /*
         * http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files
         * .shtml
         */
        factory = SecretKeyFactory.getInstance(«PBKDF2WithHmacSHA1»);
        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt_pos,
                ITERATIONS, KEYLEN_BITS);

        tmp = factory.generateSecret(spec);
        secret = new SecretKeySpec(tmp.getEncoded(), «AES»);

        deCipher = Cipher.getInstance(«AES/CBC/PKCS5Padding»);
        deCipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(
                vector_init));
    }

    /**
     * Cipher input
     *
     * @param input
     *            – the cleartext file to be encrypted
     * @param output
     *            – the encrypted data file
     * @throws IOException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws ShortBufferException
     */
    public void Cipher() throws IOException, IllegalBlockSizeException,
            BadPaddingException, ShortBufferException {
        try {
            this.output = eCipher.doFinal(this.input);
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
    }

    /**
     * Decipher input
     *
     * @param input
     *            – the cleartext file to be encrypted
     * @param output
     *            – the encrypted data file
     * @throws IOException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws ShortBufferException
     */
    public void Decipher() throws IOException, IllegalBlockSizeException,
            BadPaddingException, ShortBufferException {
        try {
            this.output = deCipher.doFinal(this.input);
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
    }

    /*
     * Methods setter and getter
     */
    public void setInput(byte[] input) {
        this.input = input;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return (new String(Hex.encodeHex(salt_pos)));
    }

    public String getVectorInit() {
        return (new String(Hex.encodeHex(vector_init)));
    }

    public byte[] getOutput() {
        return this.output;
    }
}

Como se observa es una clase normal con los métodos para configurar el cifrado y descifrado y la llamada para realizar las acciones correspondientes.

Utilizaremos dos aspectos para saber que documentos están cifrados y cuales han sido descifrados, el modelo de datos a utilizar será el siguiente:

   
    Alfresco Crypto Model
    Fernando González Ruano (twitter://fegorama)
    1.0
   
        <import uri="http://www.alfresco.org/model/dictionary/1.0"
            prefix=»d» />
       
   
   
        <namespace uri="http://www.fegorsoft.com/model/alfcrypto/1.0"
            prefix=»acr» />
   
   
       
            Ciphered
           
               
                    d:text
                    false
                   
                        false
                        false
                        false
                   
               
               
                    d:text
                    false
                   
                        false
                        false
                        false
                   
                               
           
       
       
            Deciphered
       
   

Para llamar a esta clase se necesitan dos acciones de Alfresco, estas serán CipherContent.java y DecipherContent.java:

Fichero: CipherContent.java
/*
 * alfcrypto is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * alfcrypto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see .
 */
package com.fegor.alfresco.action;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.util.HashMap;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.fegor.alfresco.model.AlfCryptoModel;
import com.fegor.alfresco.security.crypto.Crypto;
import com.google.gdata.util.common.util.Base64;

/**
 * CryptoRepo Action
 *
 * @author fegor
 *
 */
public class CipherContent extends ActionExecuterAbstractBase {

    private final Logger logger = Logger.getLogger(CipherContent.class);

    /*
     * Services
     */
    private ContentService contentService;
    private NodeService nodeService;

    private String password;
    //
    // TODO Poder usar más algoritmos que AES
    //
    // private String algorithm;

    private String salt;
    private String vector_init;

    /*
     * (non-Javadoc)
     *
     * @see
     * org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl
     * (org.alfresco.service.cmr.action.Action,
     * org.alfresco.service.cmr.repository.NodeRef)
     */
    @Override
    protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
        if (!nodeService.hasAspect(actionedUponNodeRef,
                AlfCryptoModel.ASPECT_CIPHERED)) {
            if (logger.isDebugEnabled()) {
                logger.debug(this.getClass().getName() + «: [Action for: «
                        + actionedUponNodeRef + » is ciphering…]»);
            }
            if (actionedUponNodeRef != null)
                try {
                    this.cryptoFileCipher(actionedUponNodeRef);
                } catch (ContentIOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.alfresco.repo.action.ParameterizedItemAbstractBase#
     * addParameterDefinitions(java.util.List)
     */
    @Override
    protected void addParameterDefinitions(List arg0) {
    }

    /**
     * Crypto file for nodeRef
     *
     * @param nodeRef
     * @throws IOException
     * @throws ContentIOException
     */
    private void cryptoFileCipher(NodeRef nodeRef) throws ContentIOException,
            IOException {
        ContentReader contentReader = this.contentService.getReader(nodeRef,
                ContentModel.PROP_CONTENT);
        ContentWriter contentWriter = this.contentService.getWriter(nodeRef,
                ContentModel.PROP_CONTENT, true);

        if (contentReader != null) {
            Crypto crypto = new Crypto();
            crypto.setPassword(this.password);
            byte[] crb = IOUtils.toByteArray(contentReader
                    .getContentInputStream());

            try {
                crypto.configEncrypt();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidParameterSpecException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            crypto.setInput(crb);
            try {
                crypto.Cipher();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (ShortBufferException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            contentWriter.putContent(Base64.encode(crypto.getOutput()));

            this.salt = crypto.getSalt();
            this.vector_init = crypto.getVectorInit();

            this.removeAspect(nodeRef);
            this.addAspect(nodeRef);

        } else {
            if (logger.isDebugEnabled())
                logger.debug(this.getClass().getName()
                        + «: [contentReader is null]»);
        }
    }

    /**
     * Remove aspect Deciphered
     *
     * @param nodeRef
     */
    private void removeAspect(NodeRef nodeRef) {
        if (nodeService.hasAspect(nodeRef, AlfCryptoModel.ASPECT_DECIPHERED)) {
            nodeService.removeAspect(nodeRef, AlfCryptoModel.ASPECT_DECIPHERED);
        }
    }

    /**
     * Add aspect Ciphered
     *
     * @param nodeRef
     */
    private void addAspect(NodeRef nodeRef) {
        HashMap properties = new HashMap(
                1, 1.0f);
        properties.put(AlfCryptoModel.PROP_SALT, this.salt);
        properties.put(AlfCryptoModel.PROP_VECTOR_INIT, this.vector_init);
        if (!nodeService.hasAspect(nodeRef, AlfCryptoModel.ASPECT_CIPHERED)) {
            nodeService.addAspect(nodeRef, AlfCryptoModel.ASPECT_CIPHERED,
                    properties);
        }
    }

    /**
     * @param contentService
     */
    public void setContentService(ContentService contentService) {
        this.contentService = contentService;
    }

    /**
     * @param nodeService
     */
    public void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }

    /**
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param algorithm
     */
    //
    // TODO Poder usar más algoritmos que AES
    //
    // public void setAlgorithm(String algorithm) {
    // this.algorithm = algorithm;
    // }
}

Fichero: DecipherContent.java
/*
 * alfcrypto is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * alfcrypto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see .
 */
package com.fegor.alfresco.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.apache.commons.codec.DecoderException;
import org.apache.log4j.Logger;

import com.fegor.alfresco.model.AlfCryptoModel;
import com.fegor.alfresco.security.crypto.Crypto;
import com.google.gdata.util.common.util.Base64;
import com.google.gdata.util.common.util.Base64DecoderException;

/**
 * DecryptoRepo Action
 *
 * @author fegor
 *
 */
public class DecipherContent extends ActionExecuterAbstractBase {

    private final Logger logger = Logger.getLogger(DecipherContent.class);

    /*
     * Services
     */
    private ContentService contentService;
    private NodeService nodeService;

    private String password;
    //
    // TODO Poder usar más algoritmos que AES
    //
    // private String algorithm;

    private String salt;
    private String vector_init;

    /*
     * (non-Javadoc)
     *
     * @see
     * org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl
     * (org.alfresco.service.cmr.action.Action,
     * org.alfresco.service.cmr.repository.NodeRef)
     */
    @Override
    protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
        if (nodeService.hasAspect(actionedUponNodeRef,
                AlfCryptoModel.ASPECT_CIPHERED)) {
            if (logger.isDebugEnabled()) {
                logger.debug(this.getClass().getName() + «: [Action for: «
                        + actionedUponNodeRef + » is deciphering…]»);
            }
            if (actionedUponNodeRef != null)
                try {
                    this.cryptoFileDecipher(actionedUponNodeRef);
                } catch (InvalidAlgorithmParameterException e) {
                    e.printStackTrace();
                } catch (DecoderException e) {
                    e.printStackTrace();
                }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.alfresco.repo.action.ParameterizedItemAbstractBase#
     * addParameterDefinitions(java.util.List)
     */
    @Override
    protected void addParameterDefinitions(List arg0) {
    }

    /**
     * Scan file for nodeRef
     *
     * @param nodeRef
     * @throws DecoderException
     * @throws InvalidAlgorithmParameterException
     */
    private void cryptoFileDecipher(NodeRef nodeRef)
            throws InvalidAlgorithmParameterException, DecoderException {
        ContentReader contentReader = this.contentService.getReader(nodeRef,
                ContentModel.PROP_CONTENT);
        ContentWriter contentWriter = this.contentService.getWriter(nodeRef,
                ContentModel.PROP_CONTENT, true);

        if (contentReader != null) {

            byte[] crb = contentReader.getContentString().getBytes();
            Crypto crypto = new Crypto();
            crypto.setPassword(this.password);
            this.salt = (String) nodeService.getProperty(nodeRef,
                    AlfCryptoModel.PROP_SALT);
            this.vector_init = (String) nodeService.getProperty(nodeRef,
                    AlfCryptoModel.PROP_VECTOR_INIT);

            try {
                crypto.configDecrypt(this.vector_init, this.salt);
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            }

            try {
                crypto.setInput(Base64.decode(crb));
            } catch (Base64DecoderException e1) {
                e1.printStackTrace();
            }

            try {
                crypto.Decipher();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (ShortBufferException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            contentWriter.setMimetype(«text/plain»);
            contentWriter.putContent((InputStream) (new ByteArrayInputStream(
                    crypto.getOutput())));

            this.removeAspect(nodeRef);
            this.addAspect(nodeRef);

        } else {
            if (logger.isDebugEnabled())
                logger.debug(this.getClass().getName()
                        + «: [contentReader is null]»);
        }
    }

    /**
     * Remove aspect Ciphered
     *
     * @param nodeRef
     */
    private void removeAspect(NodeRef nodeRef) {
        if (nodeService.hasAspect(nodeRef, AlfCryptoModel.ASPECT_CIPHERED)) {
            nodeService.removeAspect(nodeRef, AlfCryptoModel.ASPECT_CIPHERED);
        }
    }

    /**
     * Add aspect Deciphered
     *
     * @param nodeRef
     */
    private void addAspect(NodeRef nodeRef) {
        if (!nodeService.hasAspect(nodeRef, AlfCryptoModel.ASPECT_CIPHERED)) {
            nodeService
                    .addAspect(nodeRef, AlfCryptoModel.ASPECT_CIPHERED, null);
        }
    }

    /**
     * @param contentService
     */
    public void setContentService(ContentService contentService) {
        this.contentService = contentService;
    }

    /**
     * @param nodeService
     */
    public void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }

    /**
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param algorithm
     */
    //
    // TODO Poder usar más algoritmos que AES
    //
    // public void setAlgorithm(String algorithm) {
    // this.algorithm = algorithm;
    // }
}

El siguiente paso es configurar y registrar las acciones en Alfresco:

Fichero: actions-context.xml

    
       
            true
       
       
           
                classpath:alfresco/extension/alfcrypto.properties
           
       
   
   
    <bean id="alfcrypto.cipher.action" class="com.fegor.alfresco.action.CipherContent"
        parent=»action-executer»>
       
           
       
       
           
       
       
        <!–
       
            ${alfviral.algorithm}
       
         –>
       
            ${alfviral.password}
       
   

    <bean id="alfcrypto.decipher.action" class="com.fegor.alfresco.action.DecipherContent"
        parent=»action-executer»>
       
           
       
       
           
       
       
        <!–
       
            ${alfviral.algorithm}
       
         –>
       
            ${alfviral.password}
       
       

Fichero: model-context.xml

       
   
       
           
                alfresco/module/alfcrypto/model/alfcryptoModel.xml
           
       
     


Fichero: webclient-context.xml

    
       
           
                alfresco.module.alfcrypto.messages.alfcrypto
           
       
    
  
    <bean id="alfcrypto.webclient.configBootstrap" class="org.alfresco.web.config.WebClientConfigBootstrap"
        init-method=»init»>
       
           
                classpath:alfresco/module/alfcrypto/ui/web-client-config-custom.xml
           
       
    

Fichero: web-client-config-custom.xml

  
     
        
        
     
  
  
  
   
  
  
  
     
        
        
        
  
  

Fichero: alfcrypto.properties (messages)
alfcrypto.cipher.action.title=Cifrar
alfcrypto.cipher.action.description=Cifrado del contenido

alfcrypto.decipher.action.title=Descifrar
alfcrypto.decipher.action.description=Descifrado del contenido

alfcrypto.label.ciphered=Cifrado
alfcrypto.label.deciphered=Descifrado

Y por último construir el fichero de configuración:

Fichero: alfcrypto.properties
# La elección de algoritmo (alfcrypto.algorithm) no está implementado todavía
alfcrypto.algorithm=AES
alfcrypto.password=estoesunaclavesecreta

A partir de aquí podemos utilizar el sistema para cifrar y descifrar, para ello podemos usar reglas para que el contenido en una carpeta sea cifrado, crear un workflow para descifrar el contenido de los documentos cifrados, etc.

Solo cifra el contenido, no las propiedades de los documentos.

Más sobre este tema:
http://es.wikipedia.org/wiki/Advanced_Encryption_Standard
http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
http://wiki.alfresco.com/wiki/Data_Encryption
http://addons.alfresco.com/addons/alfresco-encryption-module

2 respuestas en “Cifrado de contenido en Alfresco”

  1. Efectivamente (pondré un comentario ahora en el blog) las previews no son visibles ya que habría que desencriptar para previsualizar, algo que rompería con la seguridad. Se entiende que la carpeta en la que se encripten los documentos no serían previsualizados ya que solo serían visualizados en alguna carpeta especial donde se desencriptasen y donde solo algunas personas con permisos especiales pudieran acceder (p.e. personal de RRHH, proyectos de I+D, etc.)

    Igualmente sucede con la indexación, el contenido no se indexaría igual que ocurre con imágenes, etc. Incluso lo mejor sería establecer el tipo MIME a algún formato que directamente Alfresco no se moleste en intentar indexar (octec-stream por ejemplo) y posteriormente (en la desencriptación) volver a restaurar por su tipo MIME original.

    El sistema de encriptación lo veo más bien para solo algunos documentos que se quieren tener en «custodia» y que por su carácter deban estar con un «punto» más de seguridad.

    Gracias por tu comentario Toni-blyx 😉

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *