1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/* Copyright (c) 2000 Ng Pheng Siong. All rights reserved. */
/* $Id$ */
%{
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/pkcs7.h>
%}
%apply Pointer NONNULL { BIO * };
%apply Pointer NONNULL { EVP_CIPHER * };
%apply Pointer NONNULL { EVP_PKEY * };
%apply Pointer NONNULL { PKCS7 * };
%apply Pointer NONNULL { STACK * };
%apply Pointer NONNULL { X509 * };
%rename(pkcs7_new) PKCS7_new;
extern PKCS7 *PKCS7_new(void);
%rename(pkcs7_free) PKCS7_free;
extern void PKCS7_free(PKCS7 *);
/* S/MIME operation */
%constant int PKCS7_TEXT = 0x1;
%constant int PKCS7_NOCERTS = 0x2;
%constant int PKCS7_NOSIGS = 0x4;
%constant int PKCS7_NOCHAIN = 0x8;
%constant int PKCS7_NOINTERN = 0x10;
%constant int PKCS7_NOVERIFY = 0x20;
%constant int PKCS7_DETACHED = 0x40;
%constant int PKCS7_BINARY = 0x80;
%constant int PKCS7_NOATTR = 0x100;
%constant int PKCS7_SIGNED = NID_pkcs7_signed;
%constant int PKCS7_ENVELOPED = NID_pkcs7_enveloped;
%constant int PKCS7_SIGNED_ENVELOPED = NID_pkcs7_signedAndEnveloped;
%constant int PKCS7_DATA = NID_pkcs7_data;
%inline %{
static PyObject *_pkcs7_err, *_smime_err;
void pkcs7_init(PyObject *pkcs7_err) {
Py_INCREF(pkcs7_err);
_pkcs7_err = pkcs7_err;
}
void smime_init(PyObject *smime_err) {
Py_INCREF(smime_err);
_smime_err = smime_err;
}
PKCS7 *pkcs7_encrypt(STACK *stack, BIO *bio, EVP_CIPHER *cipher, int flags) {
return PKCS7_encrypt((STACK_OF(X509) *)stack, bio, cipher, flags);
}
PyObject *pkcs7_decrypt(PKCS7 *pkcs7, EVP_PKEY *pkey, X509 *cert, int flags) {
int outlen;
char *outbuf;
BIO *bio;
PyObject *ret;
if (!(bio=BIO_new(BIO_s_mem()))) {
PyErr_SetString(PyExc_MemoryError, "pkcs7_decrypt");
return NULL;
}
if (!PKCS7_decrypt(pkcs7, pkey, cert, bio, flags)) {
PyErr_SetString(_pkcs7_err, ERR_reason_error_string(ERR_get_error()));
BIO_free(bio);
return NULL;
}
outlen = BIO_ctrl_pending(bio);
if (!(outbuf=(char *)PyMem_Malloc(outlen))) {
PyErr_SetString(PyExc_MemoryError, "pkcs7_decrypt");
BIO_free(bio);
return NULL;
}
BIO_read(bio, outbuf, outlen);
ret = PyString_FromStringAndSize(outbuf, outlen);
BIO_free(bio);
PyMem_Free(outbuf);
return ret;
}
PKCS7 *pkcs7_sign0(X509 *x509, EVP_PKEY *pkey, BIO *bio, int flags) {
return PKCS7_sign(x509, pkey, NULL, bio, flags);
}
PKCS7 *pkcs7_sign1(X509 *x509, EVP_PKEY *pkey, STACK *stack, BIO *bio, int flags) {
return PKCS7_sign(x509, pkey, (STACK_OF(X509) *)stack, bio, flags);
}
PyObject *pkcs7_verify1(PKCS7 *pkcs7, STACK *stack, X509_STORE *store, BIO *data, int flags) {
int outlen;
char *outbuf;
BIO *bio;
PyObject *ret;
if (!(bio=BIO_new(BIO_s_mem()))) {
PyErr_SetString(PyExc_MemoryError, "pkcs7_verify1");
return NULL;
}
if (!PKCS7_verify(pkcs7, (STACK_OF(X509) *)stack, store, data, bio, flags)) {
PyErr_SetString(_pkcs7_err, ERR_reason_error_string(ERR_get_error()));
BIO_free(bio);
Py_INCREF(Py_None);
return Py_None;
}
outlen = BIO_ctrl_pending(bio);
if (!(outbuf=(char *)PyMem_Malloc(outlen))) {
PyErr_SetString(PyExc_MemoryError, "pkcs7_verify1");
BIO_free(bio);
return NULL;
}
BIO_read(bio, outbuf, outlen);
ret = PyString_FromStringAndSize(outbuf, outlen);
BIO_free(bio);
PyMem_Free(outbuf);
return ret;
}
PyObject *pkcs7_verify0(PKCS7 *pkcs7, STACK *stack, X509_STORE *store, int flags) {
return pkcs7_verify1(pkcs7, stack, store, NULL, flags);
}
int smime_write_pkcs7_multi(BIO *bio, PKCS7 *pkcs7, BIO *data, int flags) {
return SMIME_write_PKCS7(bio, pkcs7, data, flags | PKCS7_DETACHED);
}
int smime_write_pkcs7(BIO *bio, PKCS7 *pkcs7, int flags) {
return SMIME_write_PKCS7(bio, pkcs7, NULL, flags);
}
PyObject *smime_read_pkcs7(BIO *bio) {
BIO *bcont = NULL;
PKCS7 *p7;
PyObject *tuple, *_p7, *_BIO;
if (!(p7=SMIME_read_PKCS7(bio, &bcont))) {
PyErr_SetString(_smime_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
if (!(tuple=PyTuple_New(2))) {
PyErr_SetString(PyExc_RuntimeError, "PyTuple_New() fails");
return NULL;
}
_p7 = SWIG_NewPointerObj((void *)p7, SWIGTYPE_p_PKCS7, 0);
PyTuple_SET_ITEM(tuple, 0, _p7);
if (!bcont) {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 1, Py_None);
} else {
_BIO = SWIG_NewPointerObj((void *)bcont, SWIGTYPE_p_BIO, 0);
PyTuple_SET_ITEM(tuple, 1, _BIO);
}
return tuple;
}
PKCS7 *pkcs7_read_bio(BIO *bio) {
return PEM_read_bio_PKCS7(bio, NULL, NULL, NULL);
}
PKCS7 *pkcs7_read_bio_der(BIO *bio) {
return d2i_PKCS7_bio(bio, NULL);
}
int pkcs7_write_bio(PKCS7 *pkcs7, BIO* bio) {
return PEM_write_bio_PKCS7(bio, pkcs7);
}
int pkcs7_write_bio_der(PKCS7 *pkcs7, BIO *bio) {
return i2d_PKCS7_bio(bio, pkcs7);
}
int pkcs7_type_nid(PKCS7 *pkcs7) {
return OBJ_obj2nid(pkcs7->type);
}
const char *pkcs7_type_sn(PKCS7 *pkcs7) {
return OBJ_nid2sn(OBJ_obj2nid(pkcs7->type));
}
int smime_crlf_copy(BIO *in, BIO *out) {
return SMIME_crlf_copy(in, out, PKCS7_TEXT);
}
%}
|