Ejemplo n.º 1
0
int gen_hash(
    unsigned char *inbufp,
    int bsize,
    unsigned char *outbufp,
    CRYPT_ALGO_TYPE alg)
{
    CRYPT_CONTEXT hashContext;
    unsigned char hash[40];
    int ansr = -1;

    if (alg != CRYPT_ALGO_SHA1 && alg != CRYPT_ALGO_SHA2)
        return -1;
    memset(hash, 0, 40);
    if (cryptInit_wrapper() != CRYPT_OK)
        return -1;

    if (cryptCreateContext(&hashContext, CRYPT_UNUSED, alg) != CRYPT_OK)
        return -1;
    cryptEncrypt(hashContext, inbufp, bsize);
    cryptEncrypt(hashContext, inbufp, 0);
    if (cryptGetAttributeString(
            hashContext, CRYPT_CTXINFO_HASHVALUE, hash, &ansr) != CRYPT_OK)
    {
        return -1;
    }
    cryptDestroyContext(hashContext);
    if (ansr > 0)
        memcpy(outbufp, hash, ansr);
    return ansr;
}
Ejemplo n.º 2
0
static int gen_sha2(
    uchar * inbufp,
    int bsize,
    uchar * outbufp)
{
    CRYPT_CONTEXT hashContext;
    uchar hash[40];
    int ansr;

    memset(hash, 0, 40);
    if (cryptInit() != CRYPT_OK)
    {
        FATAL(MSG_ERROR, "initializing cryptlib");
    }
    if (cryptCreateContext(&hashContext, CRYPT_UNUSED, CRYPT_ALGO_SHA2) != CRYPT_OK)
    {
        FATAL(MSG_ERROR, "creating cryptlib hash context");
    }
    cryptEncrypt(hashContext, inbufp, bsize);
    cryptEncrypt(hashContext, inbufp, 0);
    cryptGetAttributeString(hashContext, CRYPT_CTXINFO_HASHVALUE, hash, &ansr);
    cryptDestroyContext(hashContext);
    cryptEnd();
    memcpy(outbufp, hash, ansr);
    return ansr;
}
Ejemplo n.º 3
0
static void testStressObjects( void )
	{
	CRYPT_HANDLE *handleArray = malloc( NO_OBJECTS * sizeof( CRYPT_HANDLE ) );
	BYTE hash[ CRYPT_MAX_HASHSIZE ];
	int i, length, status;

	printf( "Running object stress test." );
	assert( handleArray  != NULL );
	for( i = 0; i < NO_OBJECTS; i++ )
		{
		status = cryptCreateContext( &handleArray[ i ], CRYPT_UNUSED,
									 CRYPT_ALGO_SHA );
		if( cryptStatusError( status ) )
			printf( "cryptEncrypt() failed at %d with status %d.\n", i,
					status );
		}
	printf( "." );
	for( i = 0; i < NO_OBJECTS; i++ )
		{
		status = cryptEncrypt( handleArray[ i ], "12345678", 8 );
		if( cryptStatusError( status ) )
			printf( "cryptEncrypt() failed at %d with status %d.\n", i, status );
		}
	printf( "." );
	for( i = 0; i < NO_OBJECTS; i++ )
		{
		status = cryptEncrypt( handleArray[ i ], "", 0 );
		if( cryptStatusError( status ) )
			printf( "cryptEncrypt() failed at %d with status %d.\n", i,
					status );
		}
	printf( "." );
	for( i = 0; i < NO_OBJECTS; i++ )
		{
		status = cryptGetAttributeString( handleArray[ i ],
								CRYPT_CTXINFO_HASHVALUE, hash, &length );
		if( cryptStatusError( status ) )
			printf( "cryptEncrypt() failed at %d with status %d.\n", i,
					status );
		}
	printf( "." );
	for( i = 0; i < NO_OBJECTS; i++ )
		{
		status = cryptDestroyContext( handleArray[ i ] );
		if( cryptStatusError( status ) )
			printf( "cryptEncrypt() failed at %d with status %d.\n", i,
					status );
		}
	free( handleArray );
	puts( "." );
	}
Ejemplo n.º 4
0
unsigned __stdcall processDataThread( void *arg )
	{
	CRYPT_CONTEXT cryptContext;
	BYTE buffer[ 1024 ];
	int threadNo = ( int ) arg;
	int status;

	randSleep();
	memset( buffer, '*', 1024 );
	status = cryptCreateContext( &cryptContext, CRYPT_UNUSED,
								 CRYPT_ALGO_3DES );
	if( cryptStatusOK( status ) )
		{
		randSleep();
		status = cryptSetAttributeString( cryptContext, CRYPT_CTXINFO_KEY,
										  "123456781234567812345678", 24 );
		}
	if( cryptStatusOK( status ) )
		{
		randSleep();
		status = cryptEncrypt( cryptContext, buffer, 1024 );
		}
	if( cryptStatusOK( status ) )
		{
		randSleep();
		status = cryptEncrypt( cryptContext, buffer, 0 );
		}
	if( cryptStatusOK( status ) )
		{
		randSleep();
		status = cryptDestroyContext( cryptContext );
		}
	if( cryptStatusError( status ) )
		printf( "\nEncryption failed with status %d.\n", status );
	else
		printf( "%d ", threadNo );

	_endthreadex( 0 );
	return( 0 );
	}
Ejemplo n.º 5
0
static int fuzzSpecial( const int fuzzType, const char *fuzzFileName )
	{
	CRYPT_CONTEXT cryptPrivKey;
	BYTE buffer[ 8192 ];
	const int minLength = ( fuzzType == 4000 ) ? 2 : 16;
	int length, status;

	/* If we're fuzzing bignum ops, load the private key that we'll need */
	if( fuzzType == 4000 )
		{
		if( !loadRSAContexts( CRYPT_UNUSED, NULL, &cryptPrivKey ) )
			return( CRYPT_ERROR_FAILED );
		}

	/* We're ready to go, start the forkserver and read the mutable data */
#ifndef __WINDOWS__
	__afl_manual_init();
#endif /* !__WINDOWS__ */
	length = readFileData( fuzzFileName, fuzzFileName, buffer, 8192, 
						   minLength, TRUE );
	if( length < minLength )
		return( CRYPT_ERROR_READ );

	/* Process the input file */
	if( fuzzType == 4000 )
		{
		BYTE tmpBuffer[ 8192 ];

#if 0	/* We don't do an early-out in order to check that the bignum code 
		   can actually reject all invalid input values */
		/* Any value that's larger than the modulus will be trivially 
		   rejected so there's no point in trying to process it */
		if( buffer[ 0 ] > 0x9C )
			{
			cryptDestroyContext( cryptPrivKey );
			return( CRYPT_OK );
			}
#endif /* 0 */

		memcpy( tmpBuffer, buffer, length );
		status = cryptDecrypt( cryptPrivKey, buffer, length );
		if( cryptStatusOK( status ) )
			status = cryptEncrypt( cryptPrivKey, buffer, length );
		if( cryptStatusOK( status ) )
			{
			/* Make sure that we got back what we started with */
			assert( !memcmp( buffer, tmpBuffer, length ) );
			}
		cryptDestroyContext( cryptPrivKey );
		}
Ejemplo n.º 6
0
const char *signCMS(
    struct CMS *cms,
    const char *keyfilename,
    bool bad)
{
    bool hashContext_initialized = false;
    CRYPT_CONTEXT hashContext;
    bool sigKeyContext_initialized = false;
    CRYPT_CONTEXT sigKeyContext;
    CRYPT_KEYSET cryptKeyset;
    int signatureLength;
    int tbs_lth;
    char *msg = (char *)0;
    uchar *tbsp;
    uchar *signature = NULL;
    uchar hash[40];
    struct casn *sidp;
    struct Attribute *attrp;
    struct AttrTableDefined *attrtdp;
    struct SignerInfo *sigInfop;

    // signer info
    // firat clear out any old stuff in signerInfos that may have been put
    // there by old code
    while (num_items(&cms->content.signedData.signerInfos.self) > 0)
        eject_casn(&cms->content.signedData.signerInfos.self, 0);
    sigInfop =
        (struct SignerInfo *)
        inject_casn(&(cms->content.signedData.signerInfos.self), 0);

    // write the signature version (3) to the signer info
    write_casn_num(&sigInfop->version.self, 3);

    // find the SID
    if ((sidp = findSID(cms)) == NULL)
        return "finding SID";

    // copy the CMS's SID over to the signature's SID
    copy_casn(&sigInfop->sid.subjectKeyIdentifier, sidp);

    // use sha256 as the algorithm
    write_objid(&sigInfop->digestAlgorithm.algorithm, id_sha256);

    // no parameters to sha256
    write_casn(&sigInfop->digestAlgorithm.parameters.sha256, (uchar *) "", 0);

    // first attribute: content type
    attrp = (struct Attribute *)inject_casn(&sigInfop->signedAttrs.self, 0);
    write_objid(&attrp->attrType, id_contentTypeAttr);
    attrtdp =
        (struct AttrTableDefined *)inject_casn(&attrp->attrValues.self, 0);
    copy_casn(&attrtdp->contentType,
              &cms->content.signedData.encapContentInfo.eContentType);

    // second attribute: message digest
    attrp = (struct Attribute *)inject_casn(&sigInfop->signedAttrs.self, 1);
    write_objid(&attrp->attrType, id_messageDigestAttr);

    // create the hash for the content

    // first pull out the content
    if ((tbs_lth =
         readvsize_casn(&cms->content.signedData.encapContentInfo.eContent.
                        self, &tbsp)) < 0)
        return "getting content";

    // set up the context, initialize crypt
    memset(hash, 0, 40);
    if (cryptInit_wrapper() != CRYPT_OK)
        return "initializing cryptlib";

    // the following calls function f, and if f doesn't return 0 sets
    // msg to m, then breaks out of the loop. Used immediately below.
#define CALL(f,m) if (f != 0) { msg = m; break; }

    // use a "do { ... } while (0)" loop to bracket this code, so we can
    // bail out on failure. (Note that this construct isn't really a
    // loop; it's a way to use break as a more clean version of goto.)
    do
    {
        // first sign the body of the message

        // create the context
        CALL(cryptCreateContext(&hashContext, CRYPT_UNUSED, CRYPT_ALGO_SHA2),
             "creating context");
        hashContext_initialized = true;

        // generate the hash
        CALL(cryptEncrypt(hashContext, tbsp, tbs_lth), "hashing");
        CALL(cryptEncrypt(hashContext, tbsp, 0), "hashing");

        // get the hash value. then we're done, so destroy it
        CALL(cryptGetAttributeString
             (hashContext, CRYPT_CTXINFO_HASHVALUE, hash, &signatureLength),
             "getting first hash");
        CALL(cryptDestroyContext(hashContext),
             "destroying intermediate context");

        // insert the hash as the first attribute
        attrtdp =
            (struct AttrTableDefined *)inject_casn(&attrp->attrValues.self, 0);
        write_casn(&attrtdp->messageDigest, hash, signatureLength);

        // create signing time attribute; mark the signing time as now
        if (getenv("RPKI_NO_SIGNING_TIME") == NULL)
        {
            attrp =
                (struct Attribute *)inject_casn(&sigInfop->signedAttrs.self, 2);
            write_objid(&attrp->attrType, id_signingTimeAttr);
            attrtdp =
                (struct AttrTableDefined *)inject_casn(&attrp->attrValues.self, 0);
            write_casn_time(&attrtdp->signingTime.utcTime, time((time_t *) 0));
        }

        // we are all done with the content
        free(tbsp);

        // now sign the attributes

        // get the size of signed attributes and allocate space for them
        if ((tbs_lth = size_casn(&sigInfop->signedAttrs.self)) < 0)
        {
            msg = "sizing SignerInfo";
            break;
        }
        tbsp = (uchar *) calloc(1, tbs_lth);
        encode_casn(&sigInfop->signedAttrs.self, tbsp);
        *tbsp = ASN_SET;

        // create a new, fresh hash context for hashing the attrs, and hash
        // them
        CALL(cryptCreateContext(&hashContext, CRYPT_UNUSED, CRYPT_ALGO_SHA2),
             "creating hash context");
        CALL(cryptEncrypt(hashContext, tbsp, tbs_lth), "hashing attrs");
        CALL(cryptEncrypt(hashContext, tbsp, 0), "hashing attrs");

        // get the hash value
        CALL(cryptGetAttributeString
             (hashContext, CRYPT_CTXINFO_HASHVALUE, hash, &signatureLength),
             "getting attr hash");

        // get the key and sign it
        CALL(cryptKeysetOpen
             (&cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyfilename,
              CRYPT_KEYOPT_READONLY), "opening key set");
        CALL(cryptCreateContext(&sigKeyContext, CRYPT_UNUSED, CRYPT_ALGO_RSA),
             "creating RSA context");
        sigKeyContext_initialized = true;
        CALL(cryptGetPrivateKey
             (cryptKeyset, &sigKeyContext, CRYPT_KEYID_NAME, "label",
              "password"), "getting key");
        CALL(cryptCreateSignature
             (NULL, 0, &signatureLength, sigKeyContext, hashContext),
             "signing");

        // check the signature to make sure it's right
        signature = (uchar *) calloc(1, signatureLength + 20);

        // second parameter is signatureMaxLength, so we allow a little more
        CALL(cryptCreateSignature
             (signature, signatureLength + 20, &signatureLength, sigKeyContext,
              hashContext), "signing");

        // verify that the signature is right
        CALL(cryptCheckSignature
             (signature, signatureLength, sigKeyContext, hashContext),
             "verifying");

        // end of protected block
    } while (0);

    // done with cryptlib, shut it down
    if (hashContext_initialized)
    {
        cryptDestroyContext(hashContext);
        hashContext_initialized = false;
    }
    if (sigKeyContext_initialized)
    {
        cryptDestroyContext(sigKeyContext);
        sigKeyContext_initialized = false;
    }

    // did we have any trouble above? if so, bail
    if (msg != 0)
    {
        return msg;
    }

    // ok, write the signature back to the object
    struct SignerInfo sigInfo;
    SignerInfo(&sigInfo, (ushort) 0);
    decode_casn(&sigInfo.self, signature);

    // were we supposed to make a bad signature? if so, make it bad
    if (bad)
    {
        uchar *sig;
        int siz = readvsize_casn(&sigInfo.signature, &sig);
        sig[0]++;
        write_casn(&sigInfo.signature, sig, siz);
        free(sig);
    }

    // copy the signature into the object
    copy_casn(&sigInfop->signature, &sigInfo.signature);
    delete_casn(&sigInfo.self);

    // all done with it now
    free(signature);

    // Mark it as encrypted with rsa, no params.
    // See http://www.ietf.org/mail-archive/web/sidr/current/msg04813.html for
    // why we use id_rsadsi_rsaEncryption instead of id_sha_256WithRSAEncryption
    // here.
    write_objid(&sigInfop->signatureAlgorithm.algorithm,
                id_rsadsi_rsaEncryption);
    write_casn(&sigInfop->signatureAlgorithm.parameters.self, (uchar *) "", 0);

    // no errors, we return NULL
    return NULL;
}
Ejemplo n.º 7
0
const char *signCMSBlob(
    struct CMSBlob *cms,
    const char *keyfilename)
{
    bool sigKeyContext_initialized = false;
    CRYPT_CONTEXT sigKeyContext;
    CRYPT_KEYSET cryptKeyset;
    bool hashContext_initialized = false;
    CRYPT_CONTEXT hashContext;
    int tbs_lth;                /* to-be-signed length */
    unsigned char *tbsp = NULL; /* to-be-signed pointer */
    unsigned char *hash[40];    /* stores sha256 message digest */
    int signatureLength;        /* RSA signature length */
    unsigned char *signature = NULL;    /* RSA signature bytes */
    const char *errmsg = NULL;

    struct SignerInfo *signerInfop =
        (struct SignerInfo *)member_casn(&cms->content.signedData.signerInfos.
                                         self, 0);
    struct BlobEncapsulatedContentInfo *encapContentInfop =
        &cms->content.signedData.encapContentInfo;

    if (vsize_casn(&signerInfop->signedAttrs.self) == 0)
    {
        if ((tbs_lth = vsize_casn(&encapContentInfop->eContent)) < 0)
        {
            errmsg = "sizing eContent";
            return errmsg;
        }
        tbsp = (unsigned char *)calloc(1, tbs_lth);
        if (!tbsp)
        {
            errmsg = "out of memory";
            return errmsg;
        }

        tbs_lth = read_casn(&encapContentInfop->eContent, tbsp);
    }
    else
    {
        // get the size of signed attributes and allocate space for them
        if ((tbs_lth = size_casn(&signerInfop->signedAttrs.self)) < 0)
        {
            errmsg = "sizing SignerInfo";
            return errmsg;
        }
        tbsp = (unsigned char *)calloc(1, tbs_lth);
        if (!tbsp)
        {
            errmsg = "out of memory";
            return errmsg;
        }

        // DER-encode signedAttrs
        tbs_lth = encode_casn(&signerInfop->signedAttrs.self, tbsp);
        *tbsp = ASN_SET;        /* replace ASN.1 identifier octet with ASN_SET
                                 * (0x31) */
    }

    // compute SHA-256 of signedAttrs
    if (cryptCreateContext(&hashContext, CRYPT_UNUSED, CRYPT_ALGO_SHA2) < 0 ||
        !(hashContext_initialized = true))
        errmsg = "creating hash context";
    else if (cryptEncrypt(hashContext, tbsp, tbs_lth) < 0 ||
             cryptEncrypt(hashContext, tbsp, 0) < 0)
        errmsg = "hashing attrs";
    else if (cryptGetAttributeString(hashContext, CRYPT_CTXINFO_HASHVALUE,
                                     hash, &signatureLength) < 0)
        errmsg = "getting attr hash";

    // get the key and sign it
    else if (cryptKeysetOpen(&cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
                             keyfilename, CRYPT_KEYOPT_READONLY) < 0)
        errmsg = "opening key set";
    else if (cryptCreateContext(&sigKeyContext, CRYPT_UNUSED,
                                CRYPT_ALGO_RSA) < 0 ||
             !(sigKeyContext_initialized = true))
        errmsg = "creating RSA context";
    else if (cryptGetPrivateKey(cryptKeyset, &sigKeyContext, CRYPT_KEYID_NAME,
                                "label", "password") < 0)
        errmsg = "getting key";
    else if (cryptCreateSignature(NULL, 0, &signatureLength, sigKeyContext,
                                  hashContext) < 0)
        errmsg = "signing";

    // compute signature
    else if ((signature =
              (unsigned char *)calloc(1, signatureLength + 20)) == 0)
        errmsg = "out of memory";
    // second parameter is signatureMaxLength, so we allow a little more
    else if (cryptCreateSignature
             (signature, signatureLength + 20, &signatureLength, sigKeyContext,
              hashContext) < 0)
        errmsg = "signing";
    // verify that the signature is right
    else if (cryptCheckSignature(signature, signatureLength, sigKeyContext,
                                 hashContext) < 0)
        errmsg = "verifying";

    if (hashContext_initialized)
    {
        cryptDestroyContext(hashContext);
        hashContext_initialized = false;
    }
    if (sigKeyContext_initialized)
    {
        cryptDestroyContext(sigKeyContext);
        sigKeyContext_initialized = false;
    }

    if (!errmsg)
    {
        struct SignerInfo sigInfo;
        SignerInfo(&sigInfo, (ushort) 0);
        decode_casn(&sigInfo.self, signature);
        // copy the signature into the object
        copy_casn(&signerInfop->signature, &sigInfo.signature);
        delete_casn(&sigInfo.self);
    }

    if (signature)
        free(signature);
    if (tbsp)
        free(tbsp);

    return errmsg;
}
Ejemplo n.º 8
0
static int testTSP( const CRYPT_SESSION cryptSession,
					const BOOLEAN isServer,
					const BOOLEAN isRecycledConnection,
					const BOOLEAN useAltHash,
					const BOOLEAN localSession )
	{
	int status;

	/* If we're the client, wait for the server to finish initialising */
	if( localSession && !isServer && waitMutex() == CRYPT_ERROR_TIMEOUT )
		{
		printf( "Timed out waiting for server to initialise, line %d.\n", 
				__LINE__ );
		return( FALSE );
		}

	/* If we're the client, create a message imprint to timestamp */
	if( !isServer )
		{
		CRYPT_CONTEXT hashContext;

		/* Create the hash value to add to the TSP request */
		status = cryptCreateContext( &hashContext, CRYPT_UNUSED, 
									 useAltHash ? CRYPT_ALGO_SHA256 : \
												  CRYPT_ALGO_SHA1 );
		if( cryptStatusError( status ) )
			return( FALSE );
		cryptEncrypt( hashContext, "12345678", 8 );
		cryptEncrypt( hashContext, "", 0 );
		if( isRecycledConnection )
			{
			/* If we're moving further data over an existing connection, 
			   delete the message imprint from the previous run */
			status = cryptDeleteAttribute( cryptSession,
										   CRYPT_SESSINFO_TSP_MSGIMPRINT );
			if( cryptStatusError( status ) )
				{
				printf( "cryptDeleteAttribute() failed with error code %d, "
						"line %d.\n", status, __LINE__ );
				return( FALSE );
				}
			}
		status = cryptSetAttribute( cryptSession,
									CRYPT_SESSINFO_TSP_MSGIMPRINT,
									hashContext );
		if( cryptStatusError( status ) )
			{
			printf( "cryptSetAttribute() failed with error code %d, line "
					"%d.\n", status, __LINE__ );
			return( FALSE );
			}
		cryptDestroyContext( hashContext );

		/* If it's a local session, wait for the server to finish 
		   initialising */
		if( localSession && waitMutex() == CRYPT_ERROR_TIMEOUT )
			{
			printf( "Timed out waiting for server to initialise, line %d.\n",
					__LINE__ );
			return( FALSE );
			}		
		}
	else
		{
		/* We're the server, if this is the first connect tell the client 
		   that we're ready to go */
		if( localSession && !isRecycledConnection )
			releaseMutex();
		}

	/* Activate the session and timestamp the message */
#if TSP_SERVER_NO == 11
	cryptSetAttribute( cryptSession, CRYPT_OPTION_NET_READTIMEOUT, 30 );
#endif /* Very slow TSP */
	status = cryptSetAttribute( cryptSession, CRYPT_SESSINFO_ACTIVE, TRUE );
	if( isServer )
		printConnectInfo( cryptSession );
	if( cryptStatusError( status ) )
		{
		printExtError( cryptSession, isServer ? \
					   "SVR: Attempt to activate TSP server session" : \
					   "Attempt to activate TSP client session", status,
					   __LINE__ );
		if( !isServer && isServerDown( cryptSession, status ) )
			{
			puts( "  (Server could be down, faking it and continuing...)\n" );
			cryptDestroySession( cryptSession );
			return( CRYPT_ERROR_FAILED );
			}
		cryptDestroySession( cryptSession );
		return( FALSE );
		}

	/* There's not much more we can do in the client at this point since the 
	   TSP data is only used internally by cryptlib, OTOH if we get to here 
	   then we've received a valid response from the TSA so all is OK */
	if( !isServer )
		{
		CRYPT_ENVELOPE cryptEnvelope;
		BYTE buffer[ BUFFER_SIZE ];
		int bytesCopied;

		status = cryptGetAttribute( cryptSession, CRYPT_SESSINFO_RESPONSE,
									&cryptEnvelope );
		if( cryptStatusError( status ) )
			{
			printExtError( cryptSession, "Attempt to process returned "
						   "timestamp", status, __LINE__ );
			return( FALSE );
			}
		status = cryptPopData( cryptEnvelope, buffer, BUFFER_SIZE,
							   &bytesCopied );
		if( cryptStatusError( status ) )
			{
			printf( "cryptPopData() failed with error code %d, line %d.\n",
					status, __LINE__ );
			return( FALSE );
			}
		printf( "Timestamp data size = %d bytes.\n", bytesCopied );
		debugDump( "tstinfo", buffer, bytesCopied );
		cryptDestroyEnvelope( cryptEnvelope );
		}

	return( TRUE );
	}
Ejemplo n.º 9
0
int funcion3()
{
	int tam, tam2, tamRelleno, status, keySize, valido, i, tamCert, keyMaxSize;
	long int opcion_l2, opcion_l;
	char opcion[5], *claveEncrypt, *buffer, *ptr, *endptr, *rutaToEncrypt, *directorio, *rutaSim, *nombreSim, *rutaEncrypted, *cabecera1, *cabecera2, *varlocal, *varlocal2, *password, *rutaKeyset, *nombreKeyset, *busSim, *bytesRelleno, *bufferCert, *claveExportada;
	FILE *ptrEncrypt, *ptrSim, *ptrEncrypted, *ptrCert;	
	DIR *dir;
	struct stat st;
	struct dirent *ent;

	CRYPT_CERTIFICATE certificado;
	/*Reservo memoria*/
	nombreKeyset=(char *)malloc(50);
	rutaKeyset=(char *)malloc(120);
	password=(char *)malloc(20);
	rutaToEncrypt=(char *)malloc(120);
	directorio=(char *)malloc(120);
	rutaSim=(char *)malloc(120);
	busSim=(char *)malloc(5);
	nombreSim=(char *)malloc(50);
	rutaEncrypted=(char *)malloc(120);
	varlocal=(char *)malloc(3);
	varlocal2=(char *)malloc(3);
	cabecera1=(char *)malloc(3);
	cabecera2=(char *)malloc(3);
	bytesRelleno=(char *)malloc(2);

	system("clear");
	printf("\n ------------------------------------\n");
	printf("|          Cifrar un archivo          |\n");
	printf(" ------------------------------------\n\n");
	fflush(stdout);
	/*PASO 1. Ruta del archivo a cifrar*/
	do {
		valido=0;
		printf("PASO 1. Introduzca la ruta del archivo que desea cifrar: ");
		scanf("%s", rutaToEncrypt);
		if((ptrEncrypt=fopen(rutaToEncrypt, "rb")) == NULL ) {
			printf("No se encuentra el archivo. Revise la ruta :)\n\n");
			valido=1;
			fflush(stdout);
		} 
	} while(valido==1);

	/*Buscamos el caracter '/' dentro de la cadena. Si está, es porque el usuario metió la ruta completa*/
	if((ptr=(strrchr(rutaToEncrypt, '/')))!=NULL) sprintf(rutaEncrypted, "./Archivos encriptados/%s.enc", ptr+1); //Ruta completa
	else sprintf(rutaEncrypted, "./Archivos encriptados/%s.enc", rutaToEncrypt); //El usuario metió el nombre del archivo

	/*PASO 2. Algoritmo de cifrado*/
	do {
		valido=0;
		printf("\nPASO 2. Seleccione el algoritmo de cifrado que desea utilizar:\n");
		printf("  1. AES (por defecto)\n");
		printf("  2. DES\n");
		printf("  3. RSA\n");
		fflush(stdout);
		printf("Opción (Introduzca 0 para la opción por defecto) >> ");	
	
		scanf ("%s", &opcion);

		/*Comprobacion de la validez de la selección (que sea un número)
		(usando strtol, si hay algun caracter no numérico, endptr apunta al primero de ellos,
		lo cual implica que si la cadena apuntada por endptr no tiene longitud 0
		es porque se ha introducido un caracter no numérico)*/
		opcion_l = strtol(opcion,&endptr,10);

		if(strlen(endptr)!=0 || opcion_l < 0 || opcion_l > 3) {
			printf("Ops... tendrás que meter un número entre 1 y 3 la próxima vez ;) Try again!\n");
			fflush(stdout);
			valido=1;
		}
	} while(valido==1);

	/*Distinto tratamiento según el cifrado escogido*/
	CRYPT_CONTEXT contextoEncrypt, contextoPrivado;
	if(opcion_l==1 || opcion_l==0) { //AES
		/*Vamos a comprobar que tam sea multiplo del tamaño de bloque*/
		stat(rutaToEncrypt, &st);
		tam2=st.st_size;
		if(tam2%16!=0)	{
			tam=(int)tam2/16;
			tam++;
			tam=tam*16;	
			tamRelleno=tam-tam2; 
		}
		else {
			tamRelleno=0;
			tam=tam2;
		}

		/*Creamos contextos..*/
		if((status=cryptCreateContext(&contextoEncrypt, CRYPT_UNUSED, CRYPT_ALGO_AES))!=CRYPT_OK) {
			printf("Error al crear el contexto AES. Código %d\n", status);
			fflush(stdout);
			return(-1);
		} else if((status=cryptSetAttributeString(contextoEncrypt, CRYPT_CTXINFO_IV, "1234567891123456", 16))!=CRYPT_OK) {
			printf("Error con el vector de inicialización. Código %d\n", status);
			fflush(stdout);
			return(-1);
		}
		do { //Seleccionamos el tamaño de clave en AES
			valido=0;
			printf("\nPASO 3. Seleccione el tamaño de clave:\n");
			printf("  1. 128 bits (por defecto)\n");
			printf("  2. 192 bits\n");
			printf("  3. 256 bits\n");
			fflush(stdout);
			printf("Opción (Introduzca 0 para la opción por defecto)>> ");	
			scanf ("%s", &opcion);
			/*Comprobacion de la validez de la selección (que sea un número)
			(usando strtol, si hay algun caracter no numérico, endptr apunta al primero de ellos,
			lo cual implica que si la cadena apuntada por endptr no tiene longitud 0
			es porque se ha introducido un caracter no numérico)*/
			opcion_l2 = strtol(opcion,&endptr,10);
			if(strlen(endptr)!=0 || opcion_l2 < 0 || opcion_l2 > 3) {
				printf("Ops... tendrás que meter un número entre 1 y 3 la próxima vez ;) Try again!\n");
				fflush(stdout);
				valido=1;
			}
		} while(valido==1);
		if(opcion_l2==2) {
			if((status=cryptSetAttribute(contextoEncrypt, CRYPT_CTXINFO_KEYSIZE, 192/8))!=CRYPT_OK) { //192 bits
				printf("Error al asignar tamaño de clave. Código %d\n", status);
				fflush(stdout);
				return(-1);
			}
		} else if(opcion_l2==3) {
			if((status=cryptSetAttribute(contextoEncrypt, CRYPT_CTXINFO_KEYSIZE, 256/8))!=CRYPT_OK) { //256 bits
				printf("Error al asignar tamaño de clave. Código %d\n", status);
				fflush(stdout);
				return(-1);
			}
		}
		strcpy(busSim, ".aes");
		/*Será la cabecera que incluiremos al principio del archivo encriptado*/
		cabecera1=cesarEncrypt("aes", 17, varlocal);/*Ciframos "aes" con cifrado césar. Número de posiciones desplazadas: 17 */
	} else if(opcion_l==2) { //DES
		/*Vamos a comprobar que tam sea multiplo del tamaño de bloque*/
		stat(rutaToEncrypt, &st);
		tam2=st.st_size;
		if(tam2%16!=0)	{
			tam=(int)tam2/16;
			tam++;
			tam=tam*16;	
			tamRelleno=tam-tam2; 
		}
		else {
			tamRelleno=0;
			tam=tam2;
		}

		if((status=cryptCreateContext(&contextoEncrypt, CRYPT_UNUSED, CRYPT_ALGO_DES))!=CRYPT_OK) {
			printf("Error al crear el contexto DES. Código %d\n", status);
			fflush(stdout);
			return(-1);
		} else if((status=cryptSetAttributeString(contextoEncrypt, CRYPT_CTXINFO_IV, "12345678", 8))!=CRYPT_OK) {
			printf("Error con el vector de inicialización. Código %d\n", status);
			fflush(stdout);
			return(-1);
		}
		strcpy(busSim, ".des");

		/*Será la cabecera que incluiremos al principio del ptrEncrypt encriptado*/
		cabecera1=cesarEncrypt("des", 17, varlocal);/*Ciframos "des" con cifrado césar. Número de posiciones desplazadas: 17 */
	} else if(opcion_l==3) { //RSA
		stat(rutaToEncrypt, &st);
		tam2=st.st_size;
		if(tam2%128!=0)	{
			tam=(int)tam2/128;
			tam++;
			tam=tam*128;	 
		}
		else {
			tam=tam2;
		}

	}
	if(opcion_l==0 || opcion_l==1) printf("\nPASO 4. Seleccione el modo de funcionamiento:\n");
	if(opcion_l==2) printf("\nPASO 3. Seleccione el modo de funcionamiento:\n");
	
	/*Modo de funcionamiento si se seleccionó cifrado simétrico*/
	if(opcion_l!=3) {
		do {
			valido=0;
			printf("  1. CBC (por defecto)\n");
			printf("  2. ECB\n");
			fflush(stdout);
			printf("Opción (Introduzca 0 para la opción por defecto) >> ");	
			scanf ("%s", &opcion);
			/*Comprobacion de la validez de la selección (que sea un número)
			(usando strtol, si hay algun caracter no numérico, endptr apunta al primero de ellos,
			lo cual implica que si la cadena apuntada por endptr no tiene longitud 0
			es porque se ha introducido un caracter no numérico)*/
			opcion_l2 = strtol(opcion,&endptr,10);
			if(strlen(endptr)!=0 || opcion_l2 < 0 || opcion_l2 > 2) {
				printf("Ops... tendrás que meter un número entre 1 y 2 la próxima vez ;) Try again!\n");
				fflush(stdout);
				valido=1;
				}
		} while(valido==1);
		if(opcion_l2==1) { //CBC
			if((status=cryptSetAttribute(contextoEncrypt, CRYPT_CTXINFO_MODE, CRYPT_MODE_CBC))!=CRYPT_OK) {
				printf("Error al asignar modo de funcionamiento\n");
				fflush(stdout);
				return(-1);
			}
			cabecera2=cesarEncrypt("cbc", 14, varlocal2); /*Ciframos "cbc" con cifrado césar. Número de posiciones desplazadas: 14 */
		} else if(opcion_l2==2) { //ECB
			if((status=cryptSetAttribute(contextoEncrypt, CRYPT_CTXINFO_MODE, CRYPT_MODE_ECB))!=CRYPT_OK) {
				printf("Error al asignar modo de funcionamiento\n");
				fflush(stdout);
				return(-1);
			}
			cabecera2=cesarEncrypt("ecb", 14, varlocal2); /*Ciframos "ecb" con cifrado césar. Número de posiciones desplazadas: 14 */
		}
		/*Password*/
		if(opcion_l==1 || opcion_l==0) printf("\nPASO 5. Introduzca una contraseña >> "); 	
		if(opcion_l==2)	printf("\nPASO 4. Introduzca una contraseña >> "); 
		fflush(stdout);
	
		do {
			valido=0;
			scanf("%s", password);
			if((strlen(password))<2) {
				printf("La contraseña debe tener más de un caracter\n");
				fflush(stdout);
				valido=1;
			}
				
		}while(valido==1);
	

		/*Necesitamos la clave simétrica adecuada*/
		dir = opendir ("./Claves y certificados/");
		if (dir != NULL) {
			i=0;
			/* Nos va a mostrar los archivos que haya dentro de la carpeta Claves y cert. que correspondan*/
			while ((ent = readdir (dir)) != NULL) {
				if((ptr=strstr(ent->d_name, busSim))!=NULL) { 
					strcpy(nombreSim, ent->d_name);
					i++;
				}
			}
			closedir(dir);
		} else {
			/* Problemas al abrir el directorio */
			printf("¿Ha ejecutado ya la opción 2?\n");
			fflush(stdout);
			return(-1);
		}
		if(i==0) {
			printf("No se ha encontrado ninguna clave simétrica. (¿Ha ejecutado ya la opción 2?)\n");
			fflush(stdout);
			return(-1);
		} else if(i==1) {
			printf("\n-> Se ha encontrado 1 clave simétrica. Se usará %s por defecto\n\n", nombreSim);
			fflush(stdout);
			sprintf(rutaSim, "./Claves y certificados/%s", nombreSim);
		} else {
			printf("\n¡¡ Hay %d claves simétricas creadas !!\n", i);
			fflush(stdout);
			i=0;
			dir = opendir ("./Claves y certificados/");	
			/* Nos va a mostrar los archivos que haya dentro de la carpeta Claves y cert. adecuados*/
			while ((ent = readdir (dir)) != NULL) {
				if((ptr=strstr(ent->d_name, busSim))!=NULL) {
					i++; 
					printf("  %d. %s\n", i, ent->d_name);
				}
			}
			closedir(dir);
		
			do{
				valido=0;
				printf("Introduzca el número de la clave simétrica que desea usar >> ");
				scanf("%s", &opcion);
				opcion_l2 = strtol(opcion,&endptr,10);
				if(strlen(endptr)!=0 || opcion_l2 < 1 || opcion_l2 > i) {
					printf("Ops... tendrás que meter un número entre 1 y %d la próxima vez ;) Try again!\n", i);
					fflush(stdout);
					valido=1;
				}
			}while(valido==1);
	
			/*Guardamos el nombre del archivo correspondiente en rutaSim*/
			dir = opendir ("./Claves y certificados/");
			i=0;
			while ((ent = readdir (dir)) != NULL) {
				if((ptr=strstr(ent->d_name, busSim))!=NULL) {
					i++;
					if(opcion_l2==i) { 
						sprintf(rutaSim, "./Claves y certificados/%s", ent->d_name);
					}	
				}
			}
			closedir(dir);
		}
		/*Recuperamos la clave privada y la guardamos en el contextoPrivado ya que la necesitamos*/
		/*Abrimos el keyset*/
		dir = opendir ("./Claves y certificados/");
		if (dir != NULL) {
			i=0;
			/* Nos va a mostrar los archivos .p15 que haya dentro de la carpeta Claves y cert.*/
			while ((ent = readdir (dir)) != NULL) {
				if((ptr=strstr(ent->d_name, ".p15"))!=NULL) { 
					strcpy(nombreKeyset, ent->d_name);
					i++;
				}
			}
			closedir(dir);
		} else {
			/* Problemas al abrir el directorio */
			printf("¿Ha ejecutado ya la opción 1?\n");
			fflush(stdout);
			return(-1);
		}
	
		if(i==0) {
			printf("\nNo se ha encontrado ningún archivo keyset. (¿Ha ejecutado ya la opción 1?)\n");
			fflush(stdout);
			return(-1);
		} else if(i==1) {
			printf("\n-> Se ha encontrado 1 archivo keyset. Se usará %s por defecto\n\n", nombreKeyset);
			fflush(stdout);
			sprintf(rutaKeyset, "./Claves y certificados/%s", nombreKeyset);
		} else {
			printf("\n¡¡ Hay %d archivos keysets creados !!\n", i);
			i=0;
			dir = opendir ("./Claves y certificados/");	
			/* Nos va a mostrar los archivos .p15 que haya dentro de la carpeta Claves y cert.*/
			while ((ent = readdir (dir)) != NULL) {
				if((ptr=strstr(ent->d_name, ".p15"))!=NULL) {
					i++; 
					printf("  %d. %s\n", i, ent->d_name);
				}
			}
			closedir(dir);
			/*El usuario introduce el número que corresponde al archivo keyset que elija*/
			do{
				valido=0;
				printf("Introduzca el número del archivo keyset que desea usar >> ");
				scanf("%s", &opcion);
				opcion_l2 = strtol(opcion,&endptr,10);
				if(strlen(endptr)!=0 || opcion_l2 < 1 || opcion_l2 > i) {
					printf("Ops... tendrás que meter un número entre 1 y %d la próxima vez ;) Try again!\n", i);
					fflush(stdout);
					valido=1;
				}
			}while(valido==1);
			/*Guardamos el nombre del archivo correspondiente en rutaKeyset*/
			dir = opendir ("./Claves y certificados/");
			i=0;
			while ((ent = readdir (dir)) != NULL) {
				if((ptr=strstr(ent->d_name, ".p15"))!=NULL) {
					i++;
					if(opcion_l2==i) { 
						sprintf(rutaKeyset, "./Claves y certificados/%s", ent->d_name);
					}	
				}
			}
			closedir(dir);
		}
	
		/*Abrimos la clave simétrica elejida anteriormente por el usuario*/
		if((ptrSim=fopen(rutaSim, "rb")) == NULL) {
			printf("Error al abrir la clave simétrica. (¿La ha creado ya?)\n");
			fflush(stdout);
			return(-1);
		}
		stat(rutaSim, &st);
		keySize=st.st_size;
		claveEncrypt=(char *)malloc(keySize);
		status=fread(claveEncrypt, 1, keySize, ptrSim);
	
		/*Abrimos keyset, creamos contextos, importamos claves...*/
		CRYPT_KEYSET keyset;
		if((status=cryptKeysetOpen(&keyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE, rutaKeyset, CRYPT_KEYOPT_READONLY))!=CRYPT_OK) {
			printf("Error al abrir el archivo keyset. (¿Ha ejecutado la opción 1?). Código %d\n", status);
			fflush(stdout);
			return(-1);
		}
		if((status=cryptCreateContext(&contextoPrivado, CRYPT_UNUSED, CRYPT_ALGO_RSA))!=CRYPT_OK) {
			printf("Error al crear el contexto. Código %d\n", status);
			fflush(stdout);
			return(-1);
		}
		if((status=cryptGetPrivateKey(keyset, &contextoPrivado, CRYPT_KEYID_NAME, "claveRSA", password))!=CRYPT_OK) {
			printf("Error con la clave privada. (¿Es la misma contraseña que usó en la opción 1?). Código %d\n", status);
			fflush(stdout);
			return(-1);
		}
		if((status=cryptImportKey(claveEncrypt, keySize, contextoPrivado, contextoEncrypt))!=CRYPT_OK) {
			printf("Error al importar la clave. Asegúrese de que se usa el mismo algoritmo. Código %d\n", status);
			fflush(stdout);
			return(-1);
		}
		/*Encriptamos*/
		buffer=(char *)malloc(tam);
		status=fread(buffer, 1, tam, ptrEncrypt);
		if((status=cryptEncrypt(contextoEncrypt, buffer, tam))!=CRYPT_OK) {
			printf("Error al encriptar %s. Código %d\n", rutaToEncrypt, status);
			fflush(stdout);
			return(-1);
		}
	}

	if(opcion_l==3) {

		if((ptrCert=fopen("./Claves y certificados/AES.cert", "rb")) == NULL ) {
			printf("Compruebe que haya generado ya una clave pública\n");
			fflush(stdout);
			return(-1);
		}
		stat("./Claves y certificados/AES.cert", &st);
		tamCert=st.st_size;
		bufferCert=(char *)malloc(tamCert);
		status=fread(bufferCert, 1, tamCert, ptrCert);

		if((status=cryptImportCert(bufferCert, tamCert, CRYPT_UNUSED, &certificado))!=CRYPT_OK) {
			printf("Error al importar el certificado. Código %d\n", status);
			fflush(stdout);
			return(-1);	
		}

		/*Encriptamos de 128 bytes en 128 bytes. Sin embargo, no podemos hacer cryptEncrypt para el mismo certificado/contexto. Pero lo 
dejamos asi ya que nos dara el error -21 esperado en la primera iteracion*/
		tam2=tam/128;
		buffer=(char *)malloc(tam);
		for(i=0; i<tam2; i++) {
			status=fread(buffer, 1, 128, ptrEncrypt);
			if((status=cryptEncrypt(certificado, buffer, 128))!=CRYPT_OK) {	
				printf("Error al cifrar el archivo %s. Código %d\n", rutaToEncrypt, status);
				fflush(stdout);
				return(-1);
			}
			*ptrEncrypt=*(ptrEncrypt+128);
			*buffer=*(buffer+128);
		}
	}

	/*Guardamos el archivo encriptado.*/
	/*Compruebo que exista el directorio. Si lstat devuelve 0 es que existe. Si devuelve otro valor hay que crear el directorio*/
	sprintf(directorio, "./Archivos encriptados");
	if (status = lstat(directorio, &st) != 0) {
		if(status=mkdir(directorio, 0777) != 0) {
			printf("Error al crear el directorio\n");
			fflush(stdout);
			return(-1);
		}	
	}

	/*La ruta la hemos calculado al principio del todo, cuando el usuario metio la ruta del archivo*/
	if((ptrEncrypted=fopen(rutaEncrypted, "wb")) == NULL) {
		printf("Error al crear el archivo\n");
		fflush(stdout);
		return(-1);
	}
	
	/*Vamos a incluir una cabecera en el archivo para saber después los bytes de relleno que hemos añadido*/
	sprintf(bytesRelleno, "%d", tamRelleno);
	if((status=fwrite(bytesRelleno, 1, 2, ptrEncrypted))!=2) {
		printf("Error al guardar el archivo\n");
		fflush(stdout);
		return(-1);
	}

	/*Vamos a incluir una cabecera en el archivo para saber despues que tipo de cifrado se uso*/
	*(cabecera1+3)='\0'; //Nos aseguramos que solo sean 3 bytes
	if((status=fwrite(cabecera1, 1, strlen(cabecera1), ptrEncrypted))!=strlen(cabecera1)) {
		printf("Error al guardar el archivo\n");
		fflush(stdout);
		return(-1);
	}
	/*Vamos a incluir una cabecera en el archivo para saber después el modo de funcionamiento que se uso*/
	*(cabecera2+3)='\0'; //Nos aseguramos que solo sean 3 bytes
	if((status=fwrite(cabecera2, 1, strlen(cabecera2), ptrEncrypted))!=strlen(cabecera2)) {
		printf("Error al guardar el archivo\n");
		fflush(stdout);
		return(-1);
	}
	/*Guardamos los bytes del archivo encriptado en un archivo*/
	if((status=fwrite(buffer, 1, tam, ptrEncrypted))!=tam) {
		printf("Error al guardar el archivo\n");
		fflush(stdout);
		return(-1);
	}

	/*Cerramos descriptores y destruimos lo que sea necesario*/
	fclose(ptrEncrypted);
	fclose(ptrSim);
	fclose(ptrEncrypt);
	//if((status=cryptKeysetClose(keyset))!=CRYPT_OK) {
	//	printf("Error al cerrar el keyset. Código %d\n", status);
	//	fflush(stdout);
	//	return(-1);
	//}

	if((status=cryptDestroyContext(contextoPrivado))!=CRYPT_OK) {
		printf("Error al destruir el contexto. Código %d\n", status);
		fflush(stdout);
		return(-1);
	}

	if((status=cryptDestroyContext(contextoEncrypt))!=CRYPT_OK) {
		printf("Error al destruir el contexto. Código %d\n", status);
		fflush(stdout);
		return(-1);
	}

	return(0);

}
Ejemplo n.º 10
0
int funcion6()
{

	int valido, tamFirma, tam, tamCert, i, status, keySize, keyMaxSize;
	long opcion_l;
	char opcion[5], *ptr, *endptr, *buffer, *rutaToCheck, *rutaFirma, *nombreFirma, *firma, *cert, *nombreCert, *rutaCert;
	FILE *ptrFileToCheck, *ptrFirma, *ptrCert;
	DIR *dir;	
	struct dirent *ent;
	struct stat st;

	/*Reservo memoria*/
	rutaToCheck=(char *)malloc(120);
	rutaFirma=(char *)malloc(120);
	nombreFirma=(char *)malloc(50);
	nombreCert=(char *)malloc(50);
	rutaCert=(char *)malloc(120);

	system("clear");
	printf("\n ------------------------------------\n");
	printf("|          Verificar firma           |\n");
	printf(" ------------------------------------\n\n");
	fflush(stdout);

	/*PASO 1. Ruta del archivo a verificar*/
	do {
		valido=0;
		printf("PASO 1. Introduzca la ruta del archivo que desea verificar: ");
		scanf("%s", rutaToCheck);
		if((ptrFileToCheck=fopen(rutaToCheck, "rb")) == NULL ) {
			printf("No se encuentra el archivo. Revise la ruta :)\n\n");
			valido=1;
			fflush(stdout);
		} 
	} while(valido==1);
	

	/*Necesitamos abrir el archivo y volcarlo a memoria*/
	stat(rutaToCheck, &st);
	tam=st.st_size;
	buffer=(char *)malloc(tam);
	fread(buffer, 1, tam, ptrFileToCheck);


	/*Buscamos el caracter '/' dentro de la cadena. Si está, es porque el usuario metió la ruta completa*/
	if((ptr=(strrchr(rutaToCheck, '/')))!=NULL) sprintf(nombreFirma, "%s.p7s", ptr+1); //Ruta completa
	else sprintf(nombreFirma, "%s.p7s", rutaToCheck); //El usuario metió el nombre del archivo

	/*Ahora tengo que leer la firma. Primero la busco, y si no está, que la seleccione el usuario*/
	dir = opendir ("./Firmas digitales/");
	if (dir != NULL) {
		i=0;
		/* Vamos a buscar los archivos .p7s que correspondan*/
	  	while ((ent = readdir (dir)) != NULL) {
			if((ptr=strstr(ent->d_name, nombreFirma))!=NULL) { 
				i++;
			}
		}
		closedir(dir);
	} else {
	  	/* Problemas al abrir el directorio */
	  	printf("¿Ha ejecutado ya la opción 5?\n");
		fflush(stdout);
	  	return(-1);
	}

	if(i==0) { //Resultados = 0
		printf("\nNo se ha encontrado una firma adecuada\n");
		fflush(stdout);
		printf("Seleccione cuál de los siguientes archivos corresponde con la firma creada en la función 5:\n ");
		fflush(stdout);
		dir = opendir ("./Firmas digitales/");	
		/* Nos va a mostrar los archivos .p15 que haya dentro de la carpeta Claves y cert.*/
	  	while ((ent = readdir (dir)) != NULL) {
			if((ptr=strstr(ent->d_name, ".p7s"))!=NULL) {
				i++; 
				printf("  %d. %s\n", i, ent->d_name);
			}
		}
		closedir(dir);
		/*El usuario introduce el número que corresponde a la firma que elija*/
		do{
			valido=0;
			printf("Introduzca el número de la firma que desea usar >> ");
			scanf("%s", &opcion);
			opcion_l = strtol(opcion,&endptr,10);
			if(strlen(endptr)!=0 || opcion_l < 1 || opcion_l > i) {
				printf("Ops... tendrás que meter un número entre 1 y %d la próxima vez ;) Try again!\n", i);
				fflush(stdout);
				valido=1;
			}
		}while(valido==1);
		/*Guardamos el nombre del archivo correspondiente en rutaKeyset*/
		dir = opendir ("./Firmas digitales/");
		i=0;
	 	while ((ent = readdir (dir)) != NULL) {
			if((ptr=strstr(ent->d_name, ".p7s"))!=NULL) {
				i++;
				if(opcion_l==i) { 
					sprintf(rutaFirma, "./Firmas digitales/%s", ent->d_name);
				}	
			}
		}
		closedir(dir);
		
	} else if(i==1) {
		printf("\n-> Se ha encontrado 1 firma adecuada. Se usará la firma %s por defecto\n\n", nombreFirma);
		fflush(stdout);
		sprintf(rutaFirma, "./Firmas digitales/%s", nombreFirma);
	} 

	/*Abrimos la firma*/
	if((ptrFirma=fopen(rutaFirma, "rb")) < 0) {
		printf("Error al crear el archivo\n");
		fflush(stdout);
		return(-1);
	}
	stat(rutaFirma, &st);
	tamFirma=st.st_size;
	firma=(char *)malloc(tamFirma);
	fread(firma, 1, tamFirma, ptrFirma);

	/*Ahora necesitamos el certificado*/
	dir = opendir ("./Claves y certificados/");
	if (dir != NULL) {
		i=0;
		/* Nos va a mostrar los archivos que haya dentro de la carpeta Claves y cert.*/
	  	while ((ent = readdir (dir)) != NULL) {
			if((ptr=strstr(ent->d_name, ".cert"))!=NULL) { 
				strcpy(nombreCert, ent->d_name);
				i++;
			}
		}
		closedir(dir);
	} else {
	  	/* Problemas al abrir el directorio */
	  	printf("¿Ha ejecutado ya la opción 1?\n");
		fflush(stdout);
	  	return(-1);
	}
	if(i==0) {
		printf("No se ha encontrado ningún certificado. (¿Ha ejecutado ya la opción 1?)\n");
		fflush(stdout);
		return(-1);
	} else if(i==1) {
		printf("\n-> Se ha encontrado 1 certificado. Se usará el certificado %s por defecto\n\n", nombreCert);
		fflush(stdout);
		sprintf(rutaCert, "./Claves y certificados/%s", nombreCert);
	} else {
		printf("\n¡¡Hay %d certificados creados !!\n", i);
		fflush(stdout);
		i=0;
		dir = opendir ("./Claves y certificados/");	
		/* Nos va a mostrar los archivos que haya dentro de la carpeta Claves y cert.*/
	  	while ((ent = readdir (dir)) != NULL) {
			if((ptr=strstr(ent->d_name, ".cert"))!=NULL) {
				i++; 
				printf("  %d. %s\n", i, ent->d_name);
			}
		}
		closedir(dir);
	
		do{
			valido=0;
			printf("Introduzca el número del certificado que desea usar >> ");
			scanf("%s", &opcion);
			opcion_l = strtol(opcion,&endptr,10);
			if(strlen(endptr)!=0 || opcion_l < 1 || opcion_l > i) {
				printf("Ops... tendrás que meter un número entre 1 y %d la próxima vez ;) Try again!\n", i);
				fflush(stdout);
				valido=1;
			}
		}while(valido==1);

		/*Guardamos el nombre del archivo correspondiente en rutaCert*/
		dir = opendir ("./Claves y certificados/");
		i=0;
	 	while ((ent = readdir (dir)) != NULL) {
			if((ptr=strstr(ent->d_name, ".cert"))!=NULL) {
				i++;
				if(opcion_l==i) { 
					sprintf(rutaCert, "./Claves y certificados/%s", ent->d_name);
				}	
			}
		}
		closedir(dir);
	}
	ptrCert=fopen(rutaCert, "rb");
	stat(rutaCert, &st);
	tamCert=st.st_size;
	cert=(char *)malloc(tamCert);
	fread(cert, 1, tamCert, ptrCert);

	/*Importamos certificado*/
	/*Creo el contexto para el hash*/
	CRYPT_CONTEXT contextoHash;
	if((status=cryptCreateContext(&contextoHash, CRYPT_UNUSED, CRYPT_ALGO_SHA1))!=CRYPT_OK) {
		printf("Error al crear el contexto. Código %d\n", status);
		fflush(stdout);
		return(-1);
	}

	/*Hash*/
	if((status=cryptEncrypt(contextoHash, buffer, tam))!=CRYPT_OK) {
		printf("Error al calcular el hash. Código %d\n", status);
		fflush(stdout);
		return(-1);
	}
	if((status=cryptEncrypt(contextoHash, buffer, 0))!=CRYPT_OK) {
		printf("Error al calcular el hash. Código %d\n", status);
		fflush(stdout);
		return(-1);
	}

	CRYPT_CERTIFICATE certificado;
	if((status=cryptImportCert(cert, tamCert, CRYPT_UNUSED, &certificado))!=CRYPT_OK) {
		printf("Error al importar el certificado. Código %d\n", status);
		fflush(stdout);
		return(-1);	
	}
	if((status=cryptCheckSignature(firma, tamFirma, certificado, contextoHash))!=CRYPT_OK) {
		printf("La firma no concuerda. Código %d\n", status);
		fflush(stdout);
		return(-1);	
	}

	/*Destruimos contextos y cerramos lo necesario*/
	if((status=cryptDestroyContext(contextoHash))!=CRYPT_OK) {
		printf("Error al destruir el contexto. Código %d\n", status);
		fflush(stdout);
		return(-1);
	}
	if((status=cryptDestroyCert(certificado))!=CRYPT_OK) {
		printf("Error al destruir el certificado. Código %d\n", status);
		fflush(stdout);
		return(-1);	
	}
	return(0);

}