Re: Decrypting openpgp data created by gpg

Lahiru Dissanayake <[email protected]> Thu, 28 Feb 2013 18:41:17 +0800
Newsgroups gmane.comp.encryption.cryptlib
Message-ID <CAHeAcfCJun431vuPbm5yyYp+9LOvckNffiHNd4eCjbkN-8BDOw@mail.gmail.com>
Hi,

I've attached code that reproduces both issues.

Best Regards,
Lahiru

_______________________________________________
Cryptlib mailing list
[email protected] via Mail: [email protected]
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.
main.c (text/x-csrc, 3.7 KB)
//
//  main.c
//  CryptlibTest
//
//  Created by Lahiru Dissanayake on 23/2/13.
//  Copyright (c) 2013 Clault Pte Ltd. All rights reserved.
//

#include <stdio.h>
#include "cryptlib.h"
#include "string.h"

int main(int argc, const char * argv[])
{
    int status;
    status = cryptInit();
    
    //Create an RSA context and generate keys
    CRYPT_CONTEXT keyContext;
    status = cryptCreateContext(&keyContext, CRYPT_UNUSED, CRYPT_ALGO_RSA);
    status = cryptSetAttributeString(keyContext, CRYPT_CTXINFO_LABEL, "test", 4);
    status = cryptGenerateKey(keyContext);
    
    char *message = "plaintext message";
    int messageLength = (int)strlen(message)+1;
    int bufferSize = 1000;
    char envelopedData[bufferSize];
    int bytesCopied;
    
    //Issue with setting public key to verify signature
    
    CRYPT_ENVELOPE cryptEnvelope;
    status = cryptCreateEnvelope( &cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_PGP); //change the last argument to CRYPT_FORMAT_CRYPTLIB and everything works
    status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_SIGNATURE, keyContext );
    status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_DATASIZE, messageLength );
    status = cryptPushData( cryptEnvelope, message, messageLength, &bytesCopied );
    status = cryptFlushData( cryptEnvelope );
    status = cryptPopData( cryptEnvelope, envelopedData, bufferSize, &bytesCopied );
    cryptDestroyEnvelope( cryptEnvelope );
    
    char deEnvelopedData[bufferSize];
    
    status = cryptCreateEnvelope(&cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_AUTO);
    status = cryptPushData(cryptEnvelope, envelopedData, bytesCopied, &bytesCopied);
    status = cryptSetAttribute(cryptEnvelope, CRYPT_ENVINFO_SIGNATURE, keyContext); //NOTE: we get -2 here
    status = cryptFlushData(cryptEnvelope);
    status = cryptPopData(cryptEnvelope, deEnvelopedData, bufferSize, &bytesCopied);
    
    int signatureResult;
    status = cryptGetAttribute(cryptEnvelope, CRYPT_ENVINFO_SIGNATURE_RESULT, &signatureResult); //NOTE: since we didn't get to set the key, we get -1 as the result
    
    cryptDestroyEnvelope(cryptEnvelope);
    
    //-------------------------------------------------------------------------------------
    
    //Issue with not getting correct content type out of encrypted envelopes
    
    status = cryptCreateEnvelope( &cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_PGP); //change the last argument to CRYPT_FORMAT_CRYPTLIB and everything works
    status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_PUBLICKEY, keyContext );
    status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_DATASIZE, messageLength );
    status = cryptSetAttribute(cryptEnvelope, CRYPT_ENVINFO_CONTENTTYPE, CRYPT_CONTENT_COMPRESSEDDATA); //not really compressed data; but that shouldn't matter, should it?
    status = cryptPushData( cryptEnvelope, message, messageLength, &bytesCopied );
    status = cryptFlushData( cryptEnvelope );
    status = cryptPopData( cryptEnvelope, envelopedData, bufferSize, &bytesCopied );
    cryptDestroyEnvelope( cryptEnvelope );
    
    status = cryptCreateEnvelope(&cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_AUTO);
    status = cryptPushData(cryptEnvelope, envelopedData, bytesCopied, &bytesCopied);
    status = cryptSetAttribute(cryptEnvelope, CRYPT_ENVINFO_PRIVATEKEY, keyContext);
    status = cryptFlushData(cryptEnvelope);
    status = cryptPopData(cryptEnvelope, deEnvelopedData, bufferSize, &bytesCopied);
    
    CRYPT_CONTENT_TYPE contentType;
    status = cryptGetAttribute(cryptEnvelope, CRYPT_ENVINFO_CONTENTTYPE, (int*)&contentType); //we get the contentType as CRYPT_CONTENT_DATA
    
    printf("%s", deEnvelopedData);
    
    cryptDestroyContext(keyContext);
    
    cryptEnd();
    
    return 0;
}