标签:
1 // Pkcs7DetachedTest.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include <stdio.h> 5 #include <string.h> 6 #include <stdlib.h> 7 #include <openssl/err.h> 8 #include <openssl/objects.h> 9 #include <openssl/evp.h> 10 #include <openssl/x509.h> 11 #include <openssl/pkcs7.h> 12 #include <openssl/pem.h> 13 #include <openssl/pkcs12.h> 14 #pragma comment(lib, "libeay32.lib") 15 #pragma comment(lib, "ssleay32.lib") 16 17 18 int WtData(char * filename, char * pbData,int nDataLen) 19 { 20 FILE *stream; 21 if( ( stream = fopen( filename, "wb" ) ) == NULL ) 22 return -1; 23 fwrite(pbData, 1, nDataLen, stream); 24 fclose( stream ); 25 return 0; 26 } 27 28 29 30 int main() 31 { 32 PKCS7 *p7; 33 34 int len; 35 36 unsigned char *der,*p; 37 38 FILE *fp; 39 40 X509 *x; 41 42 BIO *in; 43 44 PKCS7_SIGNER_INFO *si; 45 46 47 EVP_PKEY *pkey = NULL; 48 49 fp = fopen ("prikey.txt", "r"); 50 if (fp == NULL) 51 exit (1); 52 pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL); 53 fclose (fp); 54 55 if (pkey == NULL) 56 { 57 ERR_print_errors_fp (stderr); 58 exit (1); 59 } 60 61 62 63 p7=PKCS7_new(); 64 PKCS7_set_detached(p7, 0); 65 66 PKCS7_set_type(p7, NID_pkcs7_signed); 67 68 PKCS7_content_new(p7, NID_pkcs7_data); 69 70 PKCS7_ctrl(p7, PKCS7_OP_SET_DETACHED_SIGNATURE, 1, NULL); 71 72 73 p7->d.sign->cert=sk_X509_new_null(); 74 75 in=BIO_new_file("x509.txt","r"); 76 77 x=PEM_read_bio_X509(in,NULL,NULL,NULL); 78 79 sk_X509_push(p7->d.sign->cert,x); 80 81 si=PKCS7_sign_add_signer(p7, x, pkey, EVP_sha1(), PKCS7_NOCERTS|PKCS7_DETACHED|PKCS7_NOSMIMECAP|PKCS7_NOATTR); 82 83 //没有放真正的签名值,待处理 84 85 86 len=i2d_PKCS7(p7,NULL); 87 88 der=(unsigned char*) malloc (len); 89 90 p=der; 91 92 len=i2d_PKCS7(p7, &p); 93 94 95 96 fp=fopen("p7_sign.cer", "wb"); 97 98 fwrite(der,1,len,fp); 99 100 fclose(fp); 101 102 free (der); 103 104 BIO_free(in); 105 106 PKCS7_free(p7); 107 108 return 0; 109 110 111 }
标签:
原文地址:http://www.cnblogs.com/ggxxjj123/p/4515286.html