4 #include <openssl/pkcs7.h>
5 #include <openssl/asn1_mac.h>
7 int add_signed_time(PKCS7_SIGNER_INFO *si)
9 ASN1_UTCTIME *sign_time;
11 /* The last parameter is the amount to add/subtract from the current
12 * time (in seconds) */
13 sign_time=X509_gmtime_adj(NULL,0);
14 PKCS7_add_signed_attribute(si,NID_pkcs9_signingTime,
15 V_ASN1_UTCTIME,(char *)sign_time);
19 ASN1_UTCTIME *get_signed_time(PKCS7_SIGNER_INFO *si)
23 so=PKCS7_get_signed_attribute(si,NID_pkcs9_signingTime);
24 if (so->type == V_ASN1_UTCTIME)
25 return so->value.utctime;
29 static int signed_string_nid= -1;
31 void add_signed_string(PKCS7_SIGNER_INFO *si, char *str)
33 ASN1_OCTET_STRING *os;
35 /* To a an object of OID 1.2.3.4.5, which is an octet string */
36 if (signed_string_nid == -1)
38 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
39 os=ASN1_OCTET_STRING_new();
40 ASN1_OCTET_STRING_set(os,(unsigned char*)str,strlen(str));
41 /* When we add, we do not free */
42 PKCS7_add_signed_attribute(si,signed_string_nid,
43 V_ASN1_OCTET_STRING,(char *)os);
46 int get_signed_string(PKCS7_SIGNER_INFO *si, char *buf, int len)
49 ASN1_OCTET_STRING *os;
52 if (signed_string_nid == -1)
54 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
56 so=PKCS7_get_signed_attribute(si,signed_string_nid);
59 if (so->type == V_ASN1_OCTET_STRING)
61 os=so->value.octet_string;
65 memcpy(buf,os->data,i);
72 static int signed_seq2string_nid= -1;
73 /* ########################################### */
74 int add_signed_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2)
76 /* To add an object of OID 1.9.999, which is a sequence containing
79 ASN1_OCTET_STRING *os1,*os2;
84 if (signed_seq2string_nid == -1)
85 signed_seq2string_nid=
86 OBJ_create("1.9.9999","OID_example","Our example OID");
88 os1=ASN1_OCTET_STRING_new();
89 os2=ASN1_OCTET_STRING_new();
90 ASN1_OCTET_STRING_set(os1,(unsigned char*)str1,strlen(str1));
91 ASN1_OCTET_STRING_set(os2,(unsigned char*)str1,strlen(str1));
92 i =i2d_ASN1_OCTET_STRING(os1,NULL);
93 i+=i2d_ASN1_OCTET_STRING(os2,NULL);
94 total=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
98 ASN1_put_object(&p,1,i,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
99 i2d_ASN1_OCTET_STRING(os1,&p);
100 i2d_ASN1_OCTET_STRING(os2,&p);
102 seq=ASN1_STRING_new();
103 ASN1_STRING_set(seq,data,total);
105 ASN1_OCTET_STRING_free(os1);
106 ASN1_OCTET_STRING_free(os2);
108 PKCS7_add_signed_attribute(si,signed_seq2string_nid,
109 V_ASN1_SEQUENCE,(char *)seq);
113 /* For this case, I will malloc the return strings */
114 int get_signed_seq2string(PKCS7_SIGNER_INFO *si, char **str1, char **str2)
118 if (signed_seq2string_nid == -1)
119 signed_seq2string_nid=
120 OBJ_create("1.9.9999","OID_example","Our example OID");
122 so=PKCS7_get_signed_attribute(si,signed_seq2string_nid);
123 if (so && (so->type == V_ASN1_SEQUENCE))
128 ASN1_OCTET_STRING *os1,*os2;
130 s=so->value.sequence;
131 c.p=ASN1_STRING_data(s);
132 c.max=c.p+ASN1_STRING_length(s);
133 if (!asn1_GetSequence(&c,&length)) goto err;
134 /* Length is the length of the seqence */
137 if ((os1=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
142 if ((os2=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
146 if (!asn1_Finish(&c)) goto err;
147 *str1=malloc(os1->length+1);
148 *str2=malloc(os2->length+1);
149 memcpy(*str1,os1->data,os1->length);
150 memcpy(*str2,os2->data,os2->length);
151 (*str1)[os1->length]='\0';
152 (*str2)[os2->length]='\0';
153 ASN1_OCTET_STRING_free(os1);
154 ASN1_OCTET_STRING_free(os2);
162 /* #######################################
163 * THE OTHER WAY TO DO THINGS
164 * #######################################
166 X509_ATTRIBUTE *create_time(void)
168 ASN1_UTCTIME *sign_time;
171 /* The last parameter is the amount to add/subtract from the current
172 * time (in seconds) */
173 sign_time=X509_gmtime_adj(NULL,0);
174 ret=X509_ATTRIBUTE_create(NID_pkcs9_signingTime,
175 V_ASN1_UTCTIME,(char *)sign_time);
179 ASN1_UTCTIME *sk_get_time(STACK_OF(X509_ATTRIBUTE) *sk)
182 PKCS7_SIGNER_INFO si;
185 so=PKCS7_get_signed_attribute(&si,NID_pkcs9_signingTime);
186 if (so->type == V_ASN1_UTCTIME)
187 return so->value.utctime;
191 X509_ATTRIBUTE *create_string(char *str)
193 ASN1_OCTET_STRING *os;
196 /* To a an object of OID 1.2.3.4.5, which is an octet string */
197 if (signed_string_nid == -1)
199 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
200 os=ASN1_OCTET_STRING_new();
201 ASN1_OCTET_STRING_set(os,(unsigned char*)str,strlen(str));
202 /* When we add, we do not free */
203 ret=X509_ATTRIBUTE_create(signed_string_nid,
204 V_ASN1_OCTET_STRING,(char *)os);
208 int sk_get_string(STACK_OF(X509_ATTRIBUTE) *sk, char *buf, int len)
211 ASN1_OCTET_STRING *os;
213 PKCS7_SIGNER_INFO si;
217 if (signed_string_nid == -1)
219 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
221 so=PKCS7_get_signed_attribute(&si,signed_string_nid);
224 if (so->type == V_ASN1_OCTET_STRING)
226 os=so->value.octet_string;
230 memcpy(buf,os->data,i);
237 X509_ATTRIBUTE *add_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2)
239 /* To add an object of OID 1.9.999, which is a sequence containing
242 ASN1_OCTET_STRING *os1,*os2;
248 if (signed_seq2string_nid == -1)
249 signed_seq2string_nid=
250 OBJ_create("1.9.9999","OID_example","Our example OID");
252 os1=ASN1_OCTET_STRING_new();
253 os2=ASN1_OCTET_STRING_new();
254 ASN1_OCTET_STRING_set(os1,(unsigned char*)str1,strlen(str1));
255 ASN1_OCTET_STRING_set(os2,(unsigned char*)str1,strlen(str1));
256 i =i2d_ASN1_OCTET_STRING(os1,NULL);
257 i+=i2d_ASN1_OCTET_STRING(os2,NULL);
258 total=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
262 ASN1_put_object(&p,1,i,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
263 i2d_ASN1_OCTET_STRING(os1,&p);
264 i2d_ASN1_OCTET_STRING(os2,&p);
266 seq=ASN1_STRING_new();
267 ASN1_STRING_set(seq,data,total);
269 ASN1_OCTET_STRING_free(os1);
270 ASN1_OCTET_STRING_free(os2);
272 ret=X509_ATTRIBUTE_create(signed_seq2string_nid,
273 V_ASN1_SEQUENCE,(char *)seq);
277 /* For this case, I will malloc the return strings */
278 int sk_get_seq2string(STACK_OF(X509_ATTRIBUTE) *sk, char **str1, char **str2)
281 PKCS7_SIGNER_INFO si;
283 if (signed_seq2string_nid == -1)
284 signed_seq2string_nid=
285 OBJ_create("1.9.9999","OID_example","Our example OID");
289 so=PKCS7_get_signed_attribute(&si,signed_seq2string_nid);
290 if (so->type == V_ASN1_SEQUENCE)
295 ASN1_OCTET_STRING *os1,*os2;
297 s=so->value.sequence;
298 c.p=ASN1_STRING_data(s);
299 c.max=c.p+ASN1_STRING_length(s);
300 if (!asn1_GetSequence(&c,&length)) goto err;
301 /* Length is the length of the seqence */
304 if ((os1=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
309 if ((os2=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
313 if (!asn1_Finish(&c)) goto err;
314 *str1=malloc(os1->length+1);
315 *str2=malloc(os2->length+1);
316 memcpy(*str1,os1->data,os1->length);
317 memcpy(*str2,os2->data,os2->length);
318 (*str1)[os1->length]='\0';
319 (*str2)[os2->length]='\0';
320 ASN1_OCTET_STRING_free(os1);
321 ASN1_OCTET_STRING_free(os2);