martes, 14 de abril de 2026

Ubuntu 24. Instalar certificados de tarjeta pkcs#11. Problemas de certificados caducados

 Ya se vio en una entrada anterior como hacerlo con ubuntu 18

Veamos ahora para Ubuntu 24


1. Instalación de drivers y reconocimiento de la tarjeta

#1. Instalar drivers
sudo apt update
sudo apt install pcscd pcsc-tools libccid opensc

#2. Activar el servicio
sudo systemctl enable --now pcscd

#3. Conectar el lector y meter el DNIe
pcs_scan

#4. Verificar que se detecta el lector y aparece DNIe Spain (eid)

#5. Salir con Ctrl + C


Y el pcs_scan da esta salida:

pcsc_scan
PC/SC device scanner
V 1.7.1 (c) 2001-2022, Ludovic Rousseau <ludovic.rousseau@free.fr>
Using reader plug'n play mechanism
Scanning present readers...
0: Identiv uTrust 2700 R Smart Card Reader [CCID Interface] (53691541213332) 00 00
 
Tue Apr 14 08:48:06 2026
 Reader 0: Identiv uTrust 2700 R Smart Card Reader [CCID Interface] (53691541213332) 00 00
  Event number: 25
  Card state: Card inserted, 
  ATR: 3B 7F 96 00 00 00 6A 44 4E 49 65 10 01 01 55 04 21 03 90 00

ATR: 3B 7F 96 00 00 00 6A 44 4E 49 65 10 01 01 55 04 21 03 90 00
+ TS = 3B --> Direct Convention
+ T0 = 7F, Y(1): 0111, K: 15 (historical bytes)
  TA(1) = 96 --> Fi=512, Di=32, 16 cycles/ETU
    250000 bits/s at 4 MHz, fMax for Fi = 5 MHz => 312500 bits/s
  TB(1) = 00 --> VPP is not electrically connected
  TC(1) = 00 --> Extra guard time: 0
+ Historical bytes: 00 6A 44 4E 49 65 10 01 01 55 04 21 03 90 00
  Category indicator byte: 00 (compact TLV data object)
    Tag: 6, len: A (pre-issuing data)
      Data: 44 4E 49 65 10 01 01 55 04 21
    Mandatory status indicator (3 last bytes)
      LCS (life card cycle): 03 (Initialisation state)
      SW: 9000 (Normal processing.)

Possibly identified card (using /home/eduard/.cache/smartcard_list.txt):
3B 7F 96 00 00 00 6A 44 4E 49 65 10 01 01 55 04 21 
#Autenticación
pkcs11-tool --module /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so \
--read-object --type cert --label CertAutenticacion \
| openssl x509 -inform DER -noout -dates

# Firma
pkcs11-tool --module /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so \
--read-object --type cert --label CertFirmaDigital \
| openssl x509 -inform DER -noout -dates
"color: #6600ee; font-weight: bold;">03
90 00 3B 7F 96 00 00 00 6A 44 4E 49 65 10 01 01 55 04 .. 03 90 00 DNIE Spain (eID) http://www.dnielectronico.es/PortalDNIe/


2. Comprobar que el sistema ve los certificados y que no estan caducados

Se ejecuta:

pkcs11-tool --module /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so -O

y nos da información sobre los certificados de autenticación, firma y certificados intermedios:
  • CertAutentication (comprobaremos nuestros datos de autenticación)
  • CertFirmaDigital (mostrará nuestrso datos de firma)
  • Pueden aparecer otros certificados intermedios como CertCAIntermediaDGP
Y ahora debemos comprobar que los certificados de autenticación y firma no estén caducados

#Autenticación
pkcs11-tool --module /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so \
--read-object --type cert --label CertAutenticacion \
| openssl x509 -inform DER -noout -dates

# Firma
pkcs11-tool --module /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so \
--read-object --type cert --label CertFirmaDigital \
| openssl x509 -inform DER -noout -dates

Y para cada sentencia contesta las fechas de validez:

Using slot 0 with a present token (0x0)
notBefore=Feb 12 10:29:00 2024 GMT
notAfter=Feb 12 10:29:00 2026 GMT


3. Problema de certificado caducado.

He renovado el certificado y me dice que está caducado. Parece ser que se guarda el primer certificdo en la cache. Para ello conviene borrar caché y reinciar el servicio

#1. Borrar la caché del opensc
sudo rm -rf ~/.cache/opensc

#2. Matar y reiniciar el servicio pcscd
sudo pkill -f opensc
sudo systemctl restart pcscd

#3. Esperar 2-3 segundos y volver a verificar
pkcs11-tool --module /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so \
--read-object --type cert --label CertAutenticacion \
| openssl x509 -inform DER -noout -dates


Y ahora ya sale bien:

Using slot 0 with a present token (0x0)
notBefore=Apr 14 07:41:32 2026 GMT
notAfter=Apr 14 07:41:32 2028 GMT

4. Instalación en Mozilla

Con esto ya funciona en Chrome y Brave, para que funcione en Mozilla Firefox, podemos tener el mismo problema de que se guarde el certificado antiguo en la caché.

4.1 Ver donde se guardan los certificados de Mozilla

Primeramente vamos a ver donde estan los certificados de Mozilla

#1. Ver donde estan los certificados
ls ~/.mozilla/firefox/

#2. Y devuelve_
  35j7fxvk.default   
  72s3mnjv.default-release-1588245081987
  'Crash Reports'   
  installs.ini  
  'Pending Pings'   
  profiles.ini

Los dos candidatos son :

35j7fxvk.default   
72s3mnjv.default-release-1588245081987

Ahora hacemos 

cat ~/.mozilla/firefox/profiles.ini

y nos nuestra

[Install4F96D1932A9F858E]
Default=72s3mnjv.default-release-1588245081987
Locked=1

[Profile1]
Name=default
IsRelative=1
Path=35j7fxvk.default
Default=1

[Profile0]
Name=default-release
IsRelative=1
Path=72s3mnjv.default-release-1588245081987

[General]
StartWithLastProfile=1
Version=2

con lo que el candidato es 35j7fxvk.default

Y para listar los certificados o si tiene el OpenSC

#1. Listar certificados instalados
certutil -L -d sql:$HOME/.mozilla/firefox/35j7fxvk.default

#2. Ver si está el OpenSC instalado
modutil -dbdir sql:$HOME/.mozilla/firefox/35j7fxvk.default -list

4.2 Borrar la BD del perfil (solo si continua con los certificados caducados)

rm ~/.mozilla/firefox/35j7fxvk.default/cert9.db
rm ~/.mozilla/firefox/35j7fxvk.default/key4.db
rm ~/.mozilla/firefox/35j7fxvk.default/pkcs11.txt

Prime

Abrimos mozilla y en la url "about:preferences#privacy"



miércoles, 8 de abril de 2026

Python ASYNC (III). Ejecutando otros procesos

 1. Llamada a comandos de la shell SINCRONA

Veamos como se llama a un proceso síncronamente ya sea por el ecomando entero o pasándole parámetros:

def run_text(cmd):
    '''
    Executes a command and captures its output as text.
    Parameters:
        cmd: A list of command arguments to execute (e.g., ["certutil", "-L", "-d", "sql:/home/user/.pki/nssdb"]).
    Returns:
    The standard output of the command as a string if successful, or None if an error occurs.
    Example usage:
        # List certificates in the NSS database
        output = run_text(["certutil", "-L", "-d", "sql:/home/user/.pki/nssdb"])
    '''
    try:
        res = subprocess.run(
            cmd,
            capture_output=True,
            text=True,
            check=True
        )
        return res.stdout
    except subprocess.CalledProcessError as e:
        print(f"Error ejecutando: {' '.join(cmd)}", file=sys.stderr)
        return None


def run_bytes(cmd, input_data=None):
    '''
    Executes a command and captures its output as bytes.
    Parameters:
        cmd: A list of command arguments to execute (e.g., ["openssl", "x509", "-in", "cert.pem", "-outform", "DER"]).
        input_data: Optional bytes to be sent to the command's standard input.
    Returns:
    The standard output of the command as bytes if successful, or None if an error occurs.
    Example usage:
        # Get the PEM-encoded certificate for a given nickname from the NSS database
        pem_bytes = run_bytes(["certutil", "-L", "-d", "sql:/home/user/.pki/nssdb", "-n", "My Certificate", "-a"])
    '''
    try:
        res = subprocess.run(
            cmd,
            input=input_data,
            capture_output=True,
            check=True
        )
        return res.stdout
    except subprocess.CalledProcessError:
        return None

 2. Llamada a comandos de la shell ASINCRONA

Veamos como se llama a un proceso asíncronamente ya sea por el ecomando entero o pasándole parámetros:

async def run_text_async(cmd: list[str]) -> str | None:
    '''
    Executes a command asynchronously and captures its output as text.
    Parameters:
        cmd: A list of command arguments to execute (e.g., ["ls", "-l"]).
    Returns:
        The standard output of the command as a string if successful, or None if an error occurs.
    Example usage:
        # List files in the current directory asynchronously
        output = await run_text_async(["ls", "-l"])    
    '''
    try:
        proc = await asyncio.create_subprocess_exec(
            *cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
        stdout, stderr = await proc.communicate()
        if proc.returncode != 0:
            print(f"Error ejecutando: {' '.join(cmd)}", file=sys.stderr)
            if stderr:
                print(stderr.decode(errors="ignore").strip(), file=sys.stderr)
            return None
        return stdout.decode(errors="ignore")
    except Exception as e:
        print(f"Excepción ejecutando {' '.join(cmd)}: {e}", file=sys.stderr)
        return None


async def run_bytes_async(cmd: list[str], input_data: bytes | None = None) -> bytes | None:
    '''
    Executes a command asynchronously and captures its output as bytes.
    Parameters:
        cmd: A list of command arguments to execute (e.g., ["openssl", "x509", "-in", "cert.pem", "-outform", "DER"]).
        input_data: Optional bytes to be sent to the command's standard input.
    Returns:
        The standard output of the command as bytes if successful, or None if an error occurs.
    Example usage:
        # Get the PEM-encoded certificate for a given nickname from the NSS database asynchronously
        pem_bytes = await run_bytes_async(["certutil", "-L", "-d", "    
    '''
    try:
        proc = await asyncio.create_subprocess_exec(
            *cmd,
            stdin=asyncio.subprocess.PIPE if input_data is not None else None,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
        stdout, stderr = await proc.communicate(input=input_data)
        if proc.returncode != 0:
            print(f"Error ejecutando: {' '.join(cmd)}", file=sys.stderr)
            if stderr:
                print(stderr.decode(errors="ignore").strip(), file=sys.stderr)
            return None
        return stdout
    except Exception as e:
        print(f"Excepción ejecutando {' '.join(cmd)}: {e}", file=sys.stderr)
        return None




martes, 7 de abril de 2026

Python ASYNC (II). El event loop

1. Introducción

El event loop es el organizador de las tareas que se ejecutan dentro de é.

 El event loop normalmente se crea desde un una función síncrona con asyncio.run() 

def funcion_sync():
    resultado = asyncio.run(mi_funcion_async())
    print(resultado)


2. Entornos FastAPI (como FastHTML)

En este caso ya se ha creado un event loop. Para ello lo mas normal es utilizar funciones asíncronas.

Los puntos de partida para llamadas a las funciones suelen ser los "endpoints"(o urls) y conviene definirlas como asíncronas:

@app.post("/softprop/validate_login")
async def post_login(req, login: xmclasses.Login):
''' 1. Verifica usuario y contraseña ''' ldap_authenticated= await xmldap.autheticate_by_login(login.usuari, login.paraula_de_pass) if not ldap_authenticated: print ("returning to first login page") return fh.Div("Error en l'usuari o la paraula de pas", cls="alert alert-danger")


Pero puede haber casos que  "recojamos el event loop con asyncio.get_event_loop()  y ejcutemos funciones dendro de él con asyncio.run_coroutine_threadsafe() y recogiendo el resultado con .result() .

import asyncio

loop = asyncio.get_event_loop()
future = asyncio.run_coroutine_threadsafe(mi_funcion_async(), loop)
resultado = future.result()





Python ASYNC (I). Introducción

 1. Introducción

Async ejecuta una tarea mientras otra tarea está esperando. No es paraleleismo real pues no utiliza varios núcleos.


Veamos un ejemplo de programa sj́ncrono y su homólogo asíncrono.

La versión síncrona ejecuta el proceso tarea() dos veces que espera 2 segundos cada vez por tanto tarda 4 segundos

import time

def tarea():
    time.sleep(2)
    print("Hecho")

tarea()
tarea()


La versión asíncrona solo tarda 2 segundos pues cuando lanza la primera tarea(), no se espera a que termine para lanzar la segunda tarea() 


import asyncio

async def tarea():
    await asyncio.sleep(2)
    print("Hecho")

async def main():
    await asyncio.gather(
        tarea(),
        tarea()
    )

asyncio.run(main())


2. Cuando usar async?

  • Redes (HTTP, APIs)
  • Escaneo de red
  • I/O (archivos, sockets)
  • Bots, servidores

No sirve para:

  • CPU intensivo (usar multiprocessing mejor)
Librerías async importantes:
  • asyncio (core)
  • aiohttp (HTTP)
  • asyncssh (SSH)
  • aiomysql, asyncpg (DB)
  • aiofiles (archivos)

3. Conceptos clave

1. Las funciones asíncronas se definen con async def

async def mi_funcion():
    pass

2. El comando async ejecuta funciones asíncronas dentro de otras funciones asíncronas

async def mi_funcion():
    await my_async_func()

3. Para el main utilizar asyncio.run(my_async_fun()) (es el event loop)

if __name__ == "__main__":
    asyncio.run(run_from_yaml())

4. courutines, problematica de su ejecución. Si una función asíncrono no se la llama con async, entonces es una courutina y no se ejecuta aún

import asyncio

async def tarea():
    await asyncio.sleep(2)
    return 'hola'

async def main():
    #MALA llamada, a1 es una corrutina y no se ejecuta aún
    #  a1 es una referencia a la función tarea()
    a1= tarea()
 
    #BUENA llamada
    a2=await tarea()
    
asyncio.run(main())

5. ejecución de varias fuinciones (a la vez)con asyncio.gather

import asyncio

async def main():
    await asyncio.gather(tarea1(), tarea2(), tarea3()) 
    
asyncio.run(main())

6. await asyncio.sleep(1) para parar la ejecución dentro de una función asíncrona pero sin parar otras funciones que se ejecutan en paralelo

import asyncio

async def my_async_func():
    #Para 1 sefundo esta tarea solo y no el resto de funciones asíncronas
    await asyncio.sleep(1)