/* crypto/hmac/hmactest.c */ /* Copyright (C) 1995-1998 Eric Young ([email protected]) * All rights reserved. * * This package is an SSL implementation written * by Eric Young ([email protected]). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson ([email protected]). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young ([email protected])" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson ([email protected])" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/x509.h>
#include <openssl/pkcs7.h>
#include "../e_os.h"
int main(int argc, char *argv[])
{
BIO *ca_cert_bio = NULL, *cert_bio = NULL, *priv_key_bio = NULL,
*sentData_bio = NULL, *encodedSignedData_bio = NULL,
*decodedSignedData_bio = NULL, *receivedData_bio = NULL,
*printedEncodedSignedData_bio = NULL;
char *ca_cert_str = "-----BEGIN CERTIFICATE-----\n"
"MIIFSDCCBPCgAwIBAgIJAP8Z155xHLquMAkGByqGSM44BAMwgYkxCzAJBgNVBAYT\n"
"AlVTMQswCQYDVQQIDAJDQTESMBAGA1UEBwwJU3Vubnl2YWxlMQwwCgYDVQQKDANS\n"
"VEkxFDASBgNVBAsMC0VuZ2luZWVyaW5nMRUwEwYDVQQDDAxZdXNoZW5nIFlhbmcx\n"
"HjAcBgkqhkiG9w0BCQEWD3l1c2hlbmdAcnRpLmNvbTAeFw0xMjA0MjAyMzQzMzNa\n"
"Fw0xNTA0MjAyMzQzMzNaMIGJMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExEjAQ\n"
"BgNVBAcMCVN1bm55dmFsZTEMMAoGA1UECgwDUlRJMRQwEgYDVQQLDAtFbmdpbmVl\n"
"cmluZzEVMBMGA1UEAwwMWXVzaGVuZyBZYW5nMR4wHAYJKoZIhvcNAQkBFg95dXNo\n"
"ZW5nQHJ0aS5jb20wggNHMIICOgYHKoZIzjgEATCCAi0CggEBAKvLLIM0woItvh7I\n"
"xLjYI9el6x8dTm2786MS7dUMIoPobqsor4PcxIW7azJn7fvrcu/IK8MPeGc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jBBgwFoAUAC0HG/ca5wgF0rpNhroO0s7V3y8wCQYHKoZI\n"
"zjgEAwNHADBEAiAc3vmgLcg9ESnWzzjSzxjDXWzDIXOby6P69t64lCJw6wIgTkhp\n"
"12t/wvEx0MMSacNXjL36pfhyyi9LJYGyxSQ1xwY=\n"
"-----END CERTIFICATE-----\n";
X509 *ca_cert_x = NULL, *cert_x = NULL;
X509_STORE *cert_store = NULL;
STACK_OF(X509) *cert_stack = NULL;
char *priv_key_str = "-----BEGIN PRIVATE KEY-----\n"
"MIICZQIBADCCAjoGByqGSM44BAEwggItAoIBAQCryyyDNMKCLb4eyMS42CPXpesf\n"
"HU5tu/OjEu3VDCKD6G6rKK+D3MSFu2syZ+3763LvyCvDD3hnNJMQbiF+5zdYd106\n"
"hXbsCTLY8eP1V3CdR4H3KU1pzKII4DE4ZM4+Pg3zqI9UdTJEEARGBJEZE3ksjiUd\n"
"jjTDumGfoHNZO9JB0jNTFJ2alXANAkm4DeOOV1tBalRg2JXIq3bRIz8VPjCGOwXh\n"
"f7BitFizLBWoxiQ5yqkzaGIZklRK9NXKtwOBapfW0qFS6yTulmD5u0URYvgcudWh\n"
"/Ys7wk89aXznrZMB+8a5D61XtjsRkDrBSedXKEaiSHLdUCcxTYSPr8x8oFljAiEA\n"
"laNUSlnNSSbGfVF9LUE+BTfWdWtklVrSXQG+k55xjXMCggEBAIKuyf9iv9GLw40P\n"
"eIO0k42KyZH3VJXJ975E6eqFt0N621n1hAdFLazJmX2wP7+M19ne+rhlyJHkRv3n\n"
"6CRm5zTEocn+f5meqBQN7N8IJu4eEA1WOnnHLsYqcPyeqH7LS5cbMmREChvmJAqI\n"
"dUafrqNcqNiTeibgMyxIbrH1qocAXoYzIhnAbwjoAByr09C1kreXSzrbB+bOuT12\n"
"hT7p0NNE3+YA52Y//mRWsM1DcY2ptteQn5SPfQYDMfH0SkJ+9PCrywNB+KLP3y/H\n"
"8y84yAhmG03nEw20SmFVUms9tt6Bb3jGI2AJ2/e/DX9Frdt4U5mSbG/INUYHDcuD\n"
"TnkpS7wEIgIgbY+G79nUdltEsdszsUjD2QuLTXii8QVyAsD7A6Wj554=\n"
"-----END PRIVATE KEY-----\n";
EVP_PKEY *priv_key_evp = NULL;
unsigned int data = 0;
PKCS7 *signedData = NULL, *receivedData = NULL;
int len = 0, retcode = 0;
unsigned char asn1_buf[1024], asn1_parsed_buf[65536];
char receivedData_buf[21];
OpenSSL_add_all_algorithms();
/* set up CA */
ca_cert_bio = BIO_new(BIO_s_mem());
if (BIO_write(ca_cert_bio, ca_cert_str, strlen(ca_cert_str)) <= 0) {
printf("Error BIO_write\n");
goto end;
}
ca_cert_x = PEM_read_bio_X509_AUX(ca_cert_bio, NULL, NULL, NULL);
if (ca_cert_x == NULL) {
printf("Error PEM_read_bio_X509_AUX\n");
goto end;
}
cert_store = X509_STORE_new();
if (cert_store == NULL) {
printf("Error X509_STORE_new\n");
goto end;
}
if (X509_STORE_add_cert(cert_store, ca_cert_x) <= 0) {
printf("Error X509_STORE_add_cert\n");
goto end;
}
/* set up cert */
cert_bio = BIO_new(BIO_s_mem());
if (BIO_write(cert_bio, cert_str, strlen(cert_str)) <= 0) {
printf("Error BIO_write\n");
goto end;
}
cert_x = PEM_read_bio_X509_AUX(cert_bio, NULL, NULL, NULL);
if (cert_x == NULL) {
printf("Error PEM_read_bio_X509_AUX\n");
goto end;
}
cert_stack = sk_X509_new_null();
if (cert_stack == NULL) {
printf("Error sk_new_null\n");
goto end;
}
if (sk_X509_push(cert_stack, cert_x) <= 0) {
printf("Error sk_push\n");
goto end;
}
/* set up private key */
priv_key_bio = BIO_new(BIO_s_mem());
if (BIO_write(priv_key_bio, priv_key_str, strlen(priv_key_str)) <= 0) {
printf("Error BIO_write\n");
goto end;
}
priv_key_evp = PEM_read_bio_PrivateKey(priv_key_bio, NULL, NULL, NULL);
if (priv_key_evp == NULL) {
printf("Error PEM_read_bio_PrivateKey\n");
goto end;
}
for (data = 5; data < 15; data++) {
/* sign the data */
sentData_bio = BIO_new(BIO_s_mem());
if (BIO_write(sentData_bio, &data, 4) <= 0) {
printf("Error BIO_write\n");
goto end;
}
signedData = PKCS7_sign(cert_x, priv_key_evp, NULL, sentData_bio,
PKCS7_NOCERTS);
if (signedData == NULL) {
printf("Error PKCS7_sign\n");
goto end;
}
/* encode signed data */
encodedSignedData_bio = BIO_new(BIO_s_mem());
if (!i2d_PKCS7_bio(encodedSignedData_bio, signedData)) {
printf("Error i2d_PKCS7_bio\n");
goto end;
}
len = BIO_number_written(encodedSignedData_bio);
if (BIO_read(encodedSignedData_bio, asn1_buf, len) <= 0) {
printf("Error BIO_read\n");
goto end;
}
/* print asn1 */
printedEncodedSignedData_bio = BIO_new(BIO_s_mem());
if (ASN1_parse(printedEncodedSignedData_bio, asn1_buf, len, 0) <= 0) {
printf("Error ASN1_parse\n");
goto end;
}
if (BIO_read(printedEncodedSignedData_bio, asn1_parsed_buf,
BIO_number_written(printedEncodedSignedData_bio)) <= 0) {
printf("Error BIO_read\n");
goto end;
}
printf("asn1 for %d: %s\n", data, asn1_parsed_buf);
/* decode signed data */
decodedSignedData_bio = BIO_new(BIO_s_mem());
if (BIO_write(decodedSignedData_bio, asn1_buf, len) <= 0) {
printf("Error BIO_write\n");
goto end;
}
receivedData = d2i_PKCS7_bio(decodedSignedData_bio, NULL);
if (receivedData == NULL) {
printf("Error d2i_PKCS7_bio\n");
goto end;
}
/* verify the signature */
receivedData_bio = BIO_new(BIO_s_mem());
retcode = PKCS7_verify(receivedData, cert_stack, cert_store, NULL,
receivedData_bio, 0);
if (retcode == 1) {
printf("PKCS7_verify ok\n");
} else {
printf("PKCS7_verify failed with retcode %d!\n", retcode);
goto end;
}
/* verify received data = sent data */
if (BIO_read(receivedData_bio, receivedData_buf,
BIO_number_written(receivedData_bio)) <= 0) {
printf("Error BIO_read\n");
goto end;
}
printf("sent %d, received %d\n", data, *((int *)receivedData_buf));
if (receivedData != NULL) {
PKCS7_free(receivedData);
receivedData = NULL;
}
if (signedData != NULL) {
PKCS7_free(signedData);
signedData = NULL;
}
if (receivedData_bio != NULL) {
BIO_free(receivedData_bio);
receivedData_bio = NULL;
}
if (decodedSignedData_bio != NULL) {
BIO_free(decodedSignedData_bio);
decodedSignedData_bio = NULL;
}
if (printedEncodedSignedData_bio != NULL) {
BIO_free(printedEncodedSignedData_bio);
printedEncodedSignedData_bio = NULL;
}
if (encodedSignedData_bio != NULL) {
BIO_free(encodedSignedData_bio);
encodedSignedData_bio = NULL;
}
if (sentData_bio != NULL) {
BIO_free(sentData_bio);
sentData_bio = NULL;
}
}
end:
if (receivedData_bio != NULL) {
BIO_free(receivedData_bio);
}
if (receivedData != NULL) {
PKCS7_free(receivedData);
}
if (decodedSignedData_bio != NULL) {
BIO_free(decodedSignedData_bio);
}
if (encodedSignedData_bio != NULL) {
BIO_free(encodedSignedData_bio);
}
if (signedData != NULL) {
PKCS7_free(signedData);
}
if (sentData_bio != NULL) {
BIO_free(sentData_bio);
}
if (priv_key_evp != NULL) {
EVP_PKEY_free(priv_key_evp);
}
if (priv_key_bio != NULL) {
BIO_free(priv_key_bio);
}
if (cert_stack != NULL) {
sk_X509_pop_free(cert_stack, X509_free);
}
if (cert_bio != NULL) {
BIO_free(cert_bio);
}
if (cert_store != NULL) {
X509_STORE_free(cert_store);
}
if (ca_cert_x != NULL) {
X509_free(ca_cert_x);
}
if (ca_cert_bio != NULL) {
BIO_free(ca_cert_bio);
}
return(0);
}
Attached is a reproducer. To build and run, replace test/hmactest.c with this file. I reproduced on i86Linux2.6 gcc4.1.1 against OpenSSL 1.0.1c and 1.0.0g. This test attempts to sign, encode, decode, and verify messages using the PKCS7 API. The messages are single integers. Every integer works as expected, except for 10. When I sign 10 and verify the signature, the output of the verify is 2573. This is because the result of the encoding is 2573 encoded. If you run the test, you'll see that the asn1 for 10 shows this: 54:d=5 hl=2 l= 5 prim: OCTET STRING [HEX DUMP]:0D0A000000 0D0A in little endian is 2573. Note that l = 5 instead of 4. If it were 4, then the HEX DUMP would be 0A000000, which would be correct. It appears that there's an extra byte of 0D that's part of the encoded string. If the attached code has problems, please let me know. Yusheng
Attached is a reproducer. To build and run, replace test/hmactest.c with this file. I reproduced on i86Linux2.6 gcc4.1.1 against OpenSSL 1.0.1c and 1.0.0g. This test attempts to sign, encode, decode, and verify messages using the PKCS7 API. The messages are single integers. Every integer works as expected, except for 10. When I sign 10 and verify the signature, the output of the verify is 2573. This is because the result of the encoding is 2573 encoded. If you run the test, you’ll see that the asn1 for 10 shows this: 54:d=5 hl=2 l= 5 prim: OCTET STRING [HEX DUMP]:0D0A000000 0D0A in little endian is 2573. Note that l = 5 instead of 4. If it were 4, then the HEX DUMP would be 0A000000, which would be correct. It appears that there’s an extra byte of 0D that’s part of the encoded string. If the attached code has problems, please let me know. Yusheng |
