static VALUE ossl_pkcs7_copy(VALUE self, VALUE other) { PKCS7 *a, *b, *pkcs7; rb_check_frozen(self); if (self == other) return self; GetPKCS7(self, a); SafeGetPKCS7(other, b); pkcs7 = PKCS7_dup(b); if (!pkcs7) { ossl_raise(ePKCS7Error, NULL); } DATA_PTR(self) = pkcs7; PKCS7_free(a); return self; }
static LUA_FUNCTION(openssl_pkcs7_parse) { PKCS7 * p7 = CHECK_OBJECT(1, PKCS7, "openssl.pkcs7"); STACK_OF(X509) *certs = NULL; STACK_OF(X509_CRL) *crls = NULL; int i = OBJ_obj2nid(p7->type); lua_newtable(L); AUXILIAR_SET(L, -1, "type", OBJ_nid2ln(i), string); switch (i) { case NID_pkcs7_signed: { PKCS7_SIGNED *sign = p7->d.sign; certs = sign->cert ? sign->cert : NULL; crls = sign->crl ? sign->crl : NULL; AUXILIAR_SET(L, -1, "version", ASN1_INTEGER_get(sign->version), integer); AUXILIAR_SET(L, -1, "detached", PKCS7_is_detached(p7), boolean); lua_pushstring(L, "md_algs"); openssl_sk_x509_algor_totable(L, sign->md_algs); lua_rawset(L, -3); if (sign->signer_info) { int j, n; n = sk_PKCS7_SIGNER_INFO_num(sign->signer_info); lua_pushstring(L, "signer_info"); lua_newtable(L); for (j = 0; j < n; j++) { PKCS7_SIGNER_INFO *info = sk_PKCS7_SIGNER_INFO_value(sign->signer_info, j); lua_pushinteger(L, j + 1); openssl_push_pkcs7_signer_info(L, info); lua_rawset(L, -3); } lua_rawset(L, -3); } if (!PKCS7_is_detached(p7)) { PKCS7* c = sign->contents; c = PKCS7_dup(c); AUXILIAR_SETOBJECT(L, c, "openssl.pkcs7", -1, "contents"); } } break; case NID_pkcs7_signedAndEnveloped: certs = p7->d.signed_and_enveloped->cert; crls = p7->d.signed_and_enveloped->crl; break; case NID_pkcs7_enveloped: { /* BIO * mem = BIO_new(BIO_s_mem()); BIO * v_p7bio = PKCS7_dataDecode(p7,pkey,NULL,NULL); BUF_MEM *bptr = NULL; unsigned char src[4096]; int len; while((len = BIO_read(v_p7bio,src,4096))>0){ BIO_write(mem, src, len); } BIO_free(v_p7bio); BIO_get_mem_ptr(mem, &bptr); if((int)*puiDataLen < bptr->length) { *puiDataLen = bptr->length; ret = SAR_MemoryErr; }else{ *puiDataLen = bptr->length; memcpy(pucData,bptr->data, bptr->length); } */ } break; case NID_pkcs7_digest: { PKCS7_DIGEST* d = p7->d.digest; ASN1_OCTET_STRING *as = ASN1_STRING_dup(d->digest); PUSH_OBJECT(as, "openssl.asn1_string"); lua_setfield(L, -2, "digest"); } break; case NID_pkcs7_data: { ASN1_OCTET_STRING *as = ASN1_STRING_dup(p7->d.data); PUSH_OBJECT(as, "openssl.asn1_string"); lua_setfield(L, -2, "data"); } break; default: break; } /* NID_pkcs7_signed or NID_pkcs7_signedAndEnveloped */ if (certs != NULL) { lua_pushstring(L, "certs"); openssl_sk_x509_totable(L, certs); lua_rawset(L, -3); } if (crls != NULL) { lua_pushstring(L, "crls"); openssl_sk_x509_crl_totable(L, crls); lua_rawset(L, -3); } return 1; }
/* int openssl_signerinfo_parse(lua_State*L) { PKCS7_SIGNER_INFO * si = CHECK_OBJECT(1,PKCS7_SIGNER_INFO,"openssl.pkcs7_signer_info"); si-> } */ static LUA_FUNCTION(openssl_pkcs7_parse) { PKCS7 * p7 = CHECK_OBJECT(1, PKCS7, "openssl.pkcs7"); STACK_OF(X509) *certs = NULL; STACK_OF(X509_CRL) *crls = NULL; int i = OBJ_obj2nid(p7->type); lua_newtable(L); AUXILIAR_SET(L, -1, "type", OBJ_nid2ln(i), string); switch (i) { case NID_pkcs7_signed: { PKCS7_SIGNED *sign = p7->d.sign; PKCS7* c = sign->contents; PKCS7_SIGNER_INFO* si = sk_PKCS7_SIGNER_INFO_value(sign->signer_info, 0); (void*)si; certs = sign->cert ? sign->cert : NULL; crls = sign->crl ? sign->crl : NULL; #if 0 typedef struct pkcs7_signed_st { ASN1_INTEGER *version; /* version 1 */ STACK_OF(X509_ALGOR) *md_algs; /* md used */ STACK_OF(X509) *cert; /* [ 0 ] */ STACK_OF(X509_CRL) *crl; /* [ 1 ] */ STACK_OF(PKCS7_SIGNER_INFO) *signer_info; struct pkcs7_st *contents; } PKCS7_SIGNED; #endif AUXILIAR_SETOBJECT(L, sk_X509_ALGOR_dup(sign->md_algs), "openssl.stack_of_x509_algor", -1, "md_algs"); AUXILIAR_SETOBJECT(L, sk_PKCS7_SIGNER_INFO_dup(sign->signer_info), "openssl.stack_of_pkcs7_signer_info", -1, "signer_info"); AUXILIAR_SET(L, -1, "detached", PKCS7_is_detached(p7), boolean); if (c) { AUXILIAR_SETOBJECT(L, PKCS7_dup(c), "openssl.pkcs7", -1, "contents"); } if (!PKCS7_is_detached(p7)) { AUXILIAR_SETOBJECT(L, p7->d.sign->contents, "openssl.pkcs7", -1, "content"); } } break; case NID_pkcs7_signedAndEnveloped: certs = p7->d.signed_and_enveloped->cert; crls = p7->d.signed_and_enveloped->crl; break; case NID_pkcs7_enveloped: { /* BIO * mem = BIO_new(BIO_s_mem()); BIO * v_p7bio = PKCS7_dataDecode(p7,pkey,NULL,NULL); BUF_MEM *bptr = NULL; unsigned char src[4096]; int len; while((len = BIO_read(v_p7bio,src,4096))>0){ BIO_write(mem, src, len); } BIO_free(v_p7bio); BIO_get_mem_ptr(mem, &bptr); if((int)*puiDataLen < bptr->length) { *puiDataLen = bptr->length; ret = SAR_MemoryErr; }else{ *puiDataLen = bptr->length; memcpy(pucData,bptr->data, bptr->length); } */ } break; case NID_pkcs7_digest: { PKCS7_DIGEST* d = p7->d.digest; PKCS7* c = d->contents; ASN1_OCTET_STRING *data = d->digest; (void*)c; AUXILIAR_SET(L, -1, "type", "digest", string); if (data) { int dlen = ASN1_STRING_length(data); unsigned char* dptr = ASN1_STRING_data(data); AUXILIAR_SETLSTR(L, -1, "digest", (const char*)dptr, dlen); } } break; case NID_pkcs7_data: { ASN1_OCTET_STRING *data = p7->d.data; int dlen = ASN1_STRING_length(data); unsigned char* dptr = ASN1_STRING_data(data); AUXILIAR_SET(L, -1, "type", "data", string); AUXILIAR_SETLSTR(L, -1, "data", (const char*)dptr, dlen); } break; default: break; }