openssl3.2 - 官方demo学习 - cms - cms_uncomp.c(官方应用实现错误, 需要修正)

文章目录

    • openssl3.2 - 官方demo学习 - cms - cms_uncomp.c(官方应用实现错误, 需要修正)
    • 概述
    • 笔记
    • END

openssl3.2 - 官方demo学习 - cms - cms_uncomp.c(官方应用实现错误, 需要修正)

概述

cms数据解压缩
请注意函数定义 CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)
参数3才是输出的BIO

笔记

/*! \file cms_uncomp.c 
\note cms数据解压缩
openssl3.2 - 官方demo学习 - cms - cms_uncomp.c(官方应用实现错误, 需要修正)
请注意函数定义 CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)
参数3才是输出的BIO
*/

/*
 * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

/* Simple S/MIME uncompression example */
#include 
#include 
#include 
#include "my_openSSL_lib.h"

int main(int argc, char **argv)
{
    BIO *in = NULL, *out = NULL;
    CMS_ContentInfo *cms = NULL;
    int ret = EXIT_FAILURE;

    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    /* Open compressed content */

    in = BIO_new_file("smcomp.txt", "r");

    if (!in)
        goto err;

    /* Sign content */
    /*! SMIME_write_CMS(out, cms, in, flags) */
    /*! CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, int flags, BIO **bcont, CMS_ContentInfo **ci); */
    /*! cms = SMIME_read_CMS(in, NULL); */
    cms = SMIME_read_CMS_ex(in, CMS_STREAM, NULL, NULL); /*! 如果要给flag, 就要调用 SMIME_read_CMS_ex*/
    /*!
    * SMIME_read_CMS_ex一定读到了上一个程序压缩过的数据 smcomp.txt
    -		b	0x00000204edec2fb0 {length=143 data=0x00000204ede764b0 "0€\x6\v*咹嗺\r\x1\t\x10\x1\t爛0€\x2\x1" max=168 ...}	buf_mem_st *
		length	143	unsigned __int64
+		data	0x00000204ede764b0 "0€\x6\v*咹嗺\r\x1\t\x10\x1\t爛0€\x2\x1"	char *
		max	168	unsigned __int64
		flags	0	unsigned long

    */

    if (!cms)
        goto err;

    out = BIO_new_file("smuncomp.txt", "w");
    if (!out)
        goto err;

    /* Uncompress S/MIME message */
    /*! flags = CMS_STREAM */
    /*! cms = CMS_compress(in, NID_zlib_compression, flags); */
    /*!
    CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags);
    int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags);
    */

    /*! 现在能肯定解压缩不对, 估计是没按照压缩时的标记进行解压缩 */
    /*int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)
                   */

    /*!
    * 官方demo居然写错了..., 这让大部分新手用户懵逼啊...
    * 单步调试了好久, 才明白官方实现调用CMS_uncompress()时, 参数给错了
    * 官方原版实现 if (!CMS_uncompress(cms, out, NULL, 0)), 这句错了
    * 现在只要库是对的, 库的应用方面出问题是难不住俺的:P
    */

    if (!CMS_uncompress(cms, NULL, out, CMS_STREAM /*0*/)) /*!< 修正官方实现 */
    {
        // 对 cms_comp_c工程压缩的数据进行解压缩, 居然错了...
        goto err;
    }
        

    ret = EXIT_SUCCESS;
 err:
    if (ret != EXIT_SUCCESS) {
        fprintf(stderr, "Error Uncompressing Data\n");
        ERR_print_errors_fp(stderr);
    }

    CMS_ContentInfo_free(cms);
    BIO_free(in);
    BIO_free(out);
    return ret;
}

END

你可能感兴趣的:(openSSL,openSSL)