sign.xs

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 94 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/ELF/sign.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9  
10 #include "openssl/objects.h"
11 #include "openssl/rsa.h"
12  
13 #include "openssl/err.h"
14 #include "openssl/x509.h"
15 #include "openssl/x509_vfy.h"
16 #include "openssl/pem.h"
17 #include "openssl/bio.h"
18 #include "openssl/sha.h"
19  
20 MODULE = ELF::sign PACKAGE = ELF::sign
21  
22 PKCS7 *
23 PEM_read_bio_PKCS7(bp, x, cb, u)
24 BIO * bp
25 PKCS7 ** x
26 pem_password_cb *cb
27 void *u
28  
29 unsigned char*
30 datasign(data, certificate, privateKey)
31 SV* data;
32 X509 *certificate;
33 EVP_PKEY *privateKey;
34 CODE:
35 0 unsigned long bufLength = 0;
36 0 unsigned char *buf = NULL;
37 0 int64_t filesize = 0;
38 0 PKCS7 *sig7 = NULL;
39 0 BIO *sigRaw = BIO_new(BIO_s_mem()), *digestRaw = NULL;
40 0 const EVP_MD * digestSha512 = NULL;
41  
42 0 ST(0) = sv_newmortal();
43 STRLEN datalen;
44 0 char * dataptr = SvPVbyte(data, datalen);
45 0 ▶ if (!(digestRaw = BIO_new_mem_buf(dataptr, datalen))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(digestRaw = BIO_new_mem_buf(dataptr, datalen))) { 0 0 0
46 0 sv_setpv(ST(0), 0);
47 //sv_setpvn(ST(0), "BIO_new_mem_buf", 15);
48 0 goto end;
49 }
50 0 int flags = 0x00 | PKCS7_NOSMIMECAP | PKCS7_BINARY | PKCS7_STREAM; // | PKCS7_DETACHED;
51 0 sig7 = PKCS7_sign(NULL, NULL, NULL, digestRaw, flags);
52 0 ▶ if (!sig7) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sig7) { 0 0 0
53 0 sv_setpv(ST(0), 0);
54 //sv_setpvn(ST(0), "PKCS7_sign", 10);
55 0 goto end;
56 }
57 0 digestSha512 = EVP_get_digestbyname("SHA512");
58 0 PKCS7_sign_add_signer(sig7, certificate, privateKey, digestSha512, flags);
59 0 PKCS7_final(sig7, digestRaw, flags);
60 0 ▶ if (!PEM_write_bio_PKCS7(sigRaw, sig7)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PEM_write_bio_PKCS7(sigRaw, sig7)) { 0 0 0
61 0 sv_setpv(ST(0), 0);
62 //sv_setpvn(ST(0), "PEM_write_bio_PKCS7", 19);
63 0 goto end;
64 }
65 0 bufLength = BIO_get_mem_data(sigRaw, &buf);
66 0 sv_setpvn(ST(0), buf, bufLength);
67 0 end:
68 0 ▶ if (sigRaw)
Branch: 1 branch
BranchTrueFalseTotal
if (sigRaw) 0 0 0
69 0 BIO_free(sigRaw);
70 0 ▶ if (digestRaw)
Branch: 1 branch
BranchTrueFalseTotal
if (digestRaw) 0 0 0
71 0 BIO_free(digestRaw);
72 0 ▶ if (sig7)
Branch: 1 branch
BranchTrueFalseTotal
if (sig7) 0 0 0
73 0 PKCS7_free(sig7);
74  
75 unsigned char*
76 dataverify(data, certificate, p7)
77 SV* data;
78 X509 *certificate;
79 PKCS7 *p7;
80 CODE:
81 0 BIO *digestRaw = NULL;
82 0 STACK_OF(PKCS7_SIGNER_INFO) *signerStack = NULL;
83 0 int64_t filesize = 0;
84 PKCS7_SIGNER_INFO *si;
85 int i;
86 char buf[64*1024];
87  
88 0 ST(0) = sv_newmortal();
89 STRLEN datalen;
90 0 char * dataptr = SvPVbyte(data, datalen);
91 0 ▶ if (!(digestRaw = BIO_new_mem_buf(dataptr, datalen))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(digestRaw = BIO_new_mem_buf(dataptr, datalen))) { 0 0 0
92 0 sv_setpvn(ST(0), "BIO_new_mem_buf", 15);
93 0 goto end;
94 }
95 0 signerStack = PKCS7_get_signer_info(p7);
96 0 ▶ if (signerStack == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (signerStack == NULL) { 0 0 0
97 0 sv_setpvn(ST(0), "No signatures", 15);
98 0 goto end;
99 }
100 0 BIO * p7bio=PKCS7_dataInit(p7,digestRaw);
101 for (;;) {
102 0 i=BIO_read(p7bio,buf,sizeof(buf));
103 0 ▶ if (i <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i <= 0) break; 0 0 0
104 }
105 0 ▶ for (i=0; i<sk_PKCS7_SIGNER_INFO_num(signerStack); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<sk_PKCS7_SIGNER_INFO_num(signerStack); i++) { 0 0 0
106 ASN1_UTCTIME *tm;
107 char *str1,*str2;
108 int rc;
109 0 si = sk_PKCS7_SIGNER_INFO_value(signerStack,i);
110 //printf("Verifying\n");
111 // TODO:XXX:FIXME: Das prueft nur obs vom angegebenen Certificat ist, nicht aber ob das zu einer CA passt!
112 // ... vermutlich wie in http://docs.huihoo.com/doxygen/openssl/1.0.1c/pk7__doit_8c_source.html#l00960 ?
113 0 ▶ if ((rc= PKCS7_signatureVerify(p7bio, p7, si, certificate)) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((rc= PKCS7_signatureVerify(p7bio, p7, si, certificate)) != 1) { 0 0 0
114 0 unsigned long err = 0;
115 0 err = ERR_get_error();
116 0 ▶ if (err == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == 0) { 0 0 0
117 0 sv_setpvn(ST(0), "PKCS7_signatureVerify: No get_error", 35);
118 } else {
119 char error[200];
120 0 snprintf(error, sizeof(error), "Verify failed num=%d error=%s", err, ERR_error_string(err, 0));
121 0 sv_setpvn(ST(0), error, strlen(error));
122 }
123 0 goto end;
124 }
125 }
126  
127 0 sv_setpv(ST(0), 0);
128 0 end:
129 0 ▶ if (p7bio)
Branch: 1 branch
BranchTrueFalseTotal
if (p7bio) 0 0 0
130 0 BIO_free(p7bio);
131 0 ▶ if (digestRaw)
Branch: 1 branch
BranchTrueFalseTotal
if (digestRaw) 0 0 0
132 0 BIO_free(digestRaw);
133 0 ▶ if (p7)
Branch: 1 branch
BranchTrueFalseTotal
if (p7) 0 0 0
134 0 PKCS7_free(p7);
« blib/lib/ELF/sign.pm ↑ Summary