4 #include <openssl/pkcs7.h>
5 #include <openssl/asn1_mac.h>
6 #include <openssl/x509.h>
8 int add_signed_time(PKCS7_SIGNER_INFO *si)
10 ASN1_UTCTIME *sign_time;
12 /* The last parameter is the amount to add/subtract from the current
13 * time (in seconds) */
14 sign_time=X509_gmtime_adj(NULL,0);
15 PKCS7_add_signed_attribute(si,NID_pkcs9_signingTime,
16 V_ASN1_UTCTIME,(char *)sign_time);
20 ASN1_UTCTIME *get_signed_time(PKCS7_SIGNER_INFO *si)
24 so=PKCS7_get_signed_attribute(si,NID_pkcs9_signingTime);
25 if (so->type == V_ASN1_UTCTIME)
26 return so->value.utctime;
30 static int signed_string_nid= -1;
32 void add_signed_string(PKCS7_SIGNER_INFO *si, char *str)
34 ASN1_OCTET_STRING *os;
36 /* To a an object of OID 1.2.3.4.5, which is an octet string */
37 if (signed_string_nid == -1)
39 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
40 os=ASN1_OCTET_STRING_new();
41 ASN1_OCTET_STRING_set(os,(unsigned char*)str,strlen(str));
42 /* When we add, we do not free */
43 PKCS7_add_signed_attribute(si,signed_string_nid,
44 V_ASN1_OCTET_STRING,(char *)os);
47 int get_signed_string(PKCS7_SIGNER_INFO *si, char *buf, int len)
50 ASN1_OCTET_STRING *os;
53 if (signed_string_nid == -1)
55 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
57 so=PKCS7_get_signed_attribute(si,signed_string_nid);
60 if (so->type == V_ASN1_OCTET_STRING)
62 os=so->value.octet_string;
66 memcpy(buf,os->data,i);
73 static int signed_seq2string_nid= -1;
74 /* ########################################### */
75 int add_signed_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2)
77 /* To add an object of OID 1.9.999, which is a sequence containing
80 ASN1_OCTET_STRING *os1,*os2;
85 if (signed_seq2string_nid == -1)
86 signed_seq2string_nid=
87 OBJ_create("1.9.9999","OID_example","Our example OID");
89 os1=ASN1_OCTET_STRING_new();
90 os2=ASN1_OCTET_STRING_new();
91 ASN1_OCTET_STRING_set(os1,(unsigned char*)str1,strlen(str1));
92 ASN1_OCTET_STRING_set(os2,(unsigned char*)str1,strlen(str1));
93 i =i2d_ASN1_OCTET_STRING(os1,NULL);
94 i+=i2d_ASN1_OCTET_STRING(os2,NULL);
95 total=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
99 ASN1_put_object(&p,1,i,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
100 i2d_ASN1_OCTET_STRING(os1,&p);
101 i2d_ASN1_OCTET_STRING(os2,&p);
103 seq=ASN1_STRING_new();
104 ASN1_STRING_set(seq,data,total);
106 ASN1_OCTET_STRING_free(os1);
107 ASN1_OCTET_STRING_free(os2);
109 PKCS7_add_signed_attribute(si,signed_seq2string_nid,
110 V_ASN1_SEQUENCE,(char *)seq);
114 /* For this case, I will malloc the return strings */
115 int get_signed_seq2string(PKCS7_SIGNER_INFO *si, char **str1, char **str2)
119 if (signed_seq2string_nid == -1)
120 signed_seq2string_nid=
121 OBJ_create("1.9.9999","OID_example","Our example OID");
123 so=PKCS7_get_signed_attribute(si,signed_seq2string_nid);
124 if (so && (so->type == V_ASN1_SEQUENCE))
129 ASN1_OCTET_STRING *os1,*os2;
131 s=so->value.sequence;
132 c.p=ASN1_STRING_data(s);
133 c.max=c.p+ASN1_STRING_length(s);
134 if (!asn1_GetSequence(&c,&length)) goto err;
135 /* Length is the length of the seqence */
138 if ((os1=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
143 if ((os2=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
147 if (!asn1_const_Finish(&c)) goto err;
148 *str1=malloc(os1->length+1);
149 *str2=malloc(os2->length+1);
150 memcpy(*str1,os1->data,os1->length);
151 memcpy(*str2,os2->data,os2->length);
152 (*str1)[os1->length]='\0';
153 (*str2)[os2->length]='\0';
154 ASN1_OCTET_STRING_free(os1);
155 ASN1_OCTET_STRING_free(os2);
163 /* #######################################
164 * THE OTHER WAY TO DO THINGS
165 * #######################################
167 X509_ATTRIBUTE *create_time(void)
169 ASN1_UTCTIME *sign_time;
172 /* The last parameter is the amount to add/subtract from the current
173 * time (in seconds) */
174 sign_time=X509_gmtime_adj(NULL,0);
175 ret=X509_ATTRIBUTE_create(NID_pkcs9_signingTime,
176 V_ASN1_UTCTIME,(char *)sign_time);
180 ASN1_UTCTIME *sk_get_time(STACK_OF(X509_ATTRIBUTE) *sk)
183 PKCS7_SIGNER_INFO si;
186 so=PKCS7_get_signed_attribute(&si,NID_pkcs9_signingTime);
187 if (so->type == V_ASN1_UTCTIME)
188 return so->value.utctime;
192 X509_ATTRIBUTE *create_string(char *str)
194 ASN1_OCTET_STRING *os;
197 /* To a an object of OID 1.2.3.4.5, which is an octet string */
198 if (signed_string_nid == -1)
200 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
201 os=ASN1_OCTET_STRING_new();
202 ASN1_OCTET_STRING_set(os,(unsigned char*)str,strlen(str));
203 /* When we add, we do not free */
204 ret=X509_ATTRIBUTE_create(signed_string_nid,
205 V_ASN1_OCTET_STRING,(char *)os);
209 int sk_get_string(STACK_OF(X509_ATTRIBUTE) *sk, char *buf, int len)
212 ASN1_OCTET_STRING *os;
214 PKCS7_SIGNER_INFO si;
218 if (signed_string_nid == -1)
220 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
222 so=PKCS7_get_signed_attribute(&si,signed_string_nid);
225 if (so->type == V_ASN1_OCTET_STRING)
227 os=so->value.octet_string;
231 memcpy(buf,os->data,i);
238 X509_ATTRIBUTE *add_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2)
240 /* To add an object of OID 1.9.999, which is a sequence containing
243 ASN1_OCTET_STRING *os1,*os2;
249 if (signed_seq2string_nid == -1)
250 signed_seq2string_nid=
251 OBJ_create("1.9.9999","OID_example","Our example OID");
253 os1=ASN1_OCTET_STRING_new();
254 os2=ASN1_OCTET_STRING_new();
255 ASN1_OCTET_STRING_set(os1,(unsigned char*)str1,strlen(str1));
256 ASN1_OCTET_STRING_set(os2,(unsigned char*)str1,strlen(str1));
257 i =i2d_ASN1_OCTET_STRING(os1,NULL);
258 i+=i2d_ASN1_OCTET_STRING(os2,NULL);
259 total=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
263 ASN1_put_object(&p,1,i,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
264 i2d_ASN1_OCTET_STRING(os1,&p);
265 i2d_ASN1_OCTET_STRING(os2,&p);
267 seq=ASN1_STRING_new();
268 ASN1_STRING_set(seq,data,total);
270 ASN1_OCTET_STRING_free(os1);
271 ASN1_OCTET_STRING_free(os2);
273 ret=X509_ATTRIBUTE_create(signed_seq2string_nid,
274 V_ASN1_SEQUENCE,(char *)seq);
278 /* For this case, I will malloc the return strings */
279 int sk_get_seq2string(STACK_OF(X509_ATTRIBUTE) *sk, char **str1, char **str2)
282 PKCS7_SIGNER_INFO si;
284 if (signed_seq2string_nid == -1)
285 signed_seq2string_nid=
286 OBJ_create("1.9.9999","OID_example","Our example OID");
290 so=PKCS7_get_signed_attribute(&si,signed_seq2string_nid);
291 if (so->type == V_ASN1_SEQUENCE)
296 ASN1_OCTET_STRING *os1,*os2;
298 s=so->value.sequence;
299 c.p=ASN1_STRING_data(s);
300 c.max=c.p+ASN1_STRING_length(s);
301 if (!asn1_GetSequence(&c,&length)) goto err;
302 /* Length is the length of the seqence */
305 if ((os1=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
310 if ((os2=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
314 if (!asn1_const_Finish(&c)) goto err;
315 *str1=malloc(os1->length+1);
316 *str2=malloc(os2->length+1);
317 memcpy(*str1,os1->data,os1->length);
318 memcpy(*str2,os2->data,os2->length);
319 (*str1)[os1->length]='\0';
320 (*str2)[os2->length]='\0';
321 ASN1_OCTET_STRING_free(os1);
322 ASN1_OCTET_STRING_free(os2);