4 #include <openssl/pkcs7.h>
6 int add_signed_time(PKCS7_SIGNER_INFO *si)
8 ASN1_UTCTIME *sign_time;
10 /* The last parameter is the amount to add/subtract from the current
11 * time (in seconds) */
12 sign_time=X509_gmtime_adj(NULL,0);
13 PKCS7_add_signed_attribute(si,NID_pkcs9_signingTime,
14 V_ASN1_UTCTIME,(char *)sign_time);
18 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)
31 static int signed_string_nid= -1;
33 int add_signed_string(PKCS7_SIGNER_INFO *si, char *str)
35 ASN1_OCTET_STRING *os;
37 /* To a an object of OID 1.2.3.4.5, which is an octet string */
38 if (signed_string_nid == -1)
40 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
41 os=ASN1_OCTET_STRING_new();
42 ASN1_OCTET_STRING_set(os,str,strlen(str));
43 /* When we add, we do not free */
44 PKCS7_add_signed_attribute(si,signed_string_nid,
45 V_ASN1_OCTET_STRING,(char *)os);
48 int get_signed_string(PKCS7_SIGNER_INFO *si, char *buf, int len)
51 ASN1_OCTET_STRING *os;
54 if (signed_string_nid == -1)
56 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
58 so=PKCS7_get_signed_attribute(si,signed_string_nid);
61 if (so->type == V_ASN1_OCTET_STRING)
63 os=so->value.octet_string;
67 memcpy(buf,os->data,i);
74 static signed_seq2string_nid= -1;
75 /* ########################################### */
76 int add_signed_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2)
78 /* To add an object of OID 1.9.999, which is a sequence containing
81 ASN1_OCTET_STRING *os1,*os2;
86 if (signed_seq2string_nid == -1)
87 signed_seq2string_nid=
88 OBJ_create("1.9.9999","OID_example","Our example OID");
90 os1=ASN1_OCTET_STRING_new();
91 os2=ASN1_OCTET_STRING_new();
92 ASN1_OCTET_STRING_set(os1,str1,strlen(str1));
93 ASN1_OCTET_STRING_set(os2,str1,strlen(str1));
94 i =i2d_ASN1_OCTET_STRING(os1,NULL);
95 i+=i2d_ASN1_OCTET_STRING(os2,NULL);
96 total=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
100 ASN1_put_object(&p,1,i,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
101 i2d_ASN1_OCTET_STRING(os1,&p);
102 i2d_ASN1_OCTET_STRING(os2,&p);
104 seq=ASN1_STRING_new();
105 ASN1_STRING_set(seq,data,total);
107 ASN1_OCTET_STRING_free(os1);
108 ASN1_OCTET_STRING_free(os2);
110 PKCS7_add_signed_attribute(si,signed_seq2string_nid,
111 V_ASN1_SEQUENCE,(char *)seq);
115 /* For this case, I will malloc the return strings */
116 int get_signed_seq2string(PKCS7_SIGNER_INFO *si, char **str1, char **str2)
120 if (signed_seq2string_nid == -1)
121 signed_seq2string_nid=
122 OBJ_create("1.9.9999","OID_example","Our example OID");
124 so=PKCS7_get_signed_attribute(si,signed_seq2string_nid);
125 if (so && (so->type == V_ASN1_SEQUENCE))
130 ASN1_OCTET_STRING *os1,*os2;
132 s=so->value.sequence;
133 c.p=ASN1_STRING_data(s);
134 c.max=c.p+ASN1_STRING_length(s);
135 if (!asn1_GetSequence(&c,&length)) goto err;
136 /* Length is the length of the seqence */
139 if ((os1=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
144 if ((os2=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
148 if (!asn1_Finish(&c)) goto err;
149 *str1=malloc(os1->length+1);
150 *str2=malloc(os2->length+1);
151 memcpy(*str1,os1->data,os1->length);
152 memcpy(*str2,os2->data,os2->length);
153 (*str1)[os1->length]='\0';
154 (*str2)[os2->length]='\0';
155 ASN1_OCTET_STRING_free(os1);
156 ASN1_OCTET_STRING_free(os2);
164 /* #######################################
165 * THE OTHER WAY TO DO THINGS
166 * #######################################
168 X509_ATTRIBUTE *create_time(void)
170 ASN1_UTCTIME *sign_time;
173 /* The last parameter is the amount to add/subtract from the current
174 * time (in seconds) */
175 sign_time=X509_gmtime_adj(NULL,0);
176 ret=X509_ATTRIBUTE_create(NID_pkcs9_signingTime,
177 V_ASN1_UTCTIME,(char *)sign_time);
181 ASN1_UTCTIME *sk_get_time(STACK *sk)
185 PKCS7_SIGNER_INFO si;
188 so=PKCS7_get_signed_attribute(&si,NID_pkcs9_signingTime);
189 if (so->type == V_ASN1_UTCTIME)
191 ut=so->value.utctime;
196 X509_ATTRIBUTE *create_string(char *str)
198 ASN1_OCTET_STRING *os;
201 /* To a an object of OID 1.2.3.4.5, which is an octet string */
202 if (signed_string_nid == -1)
204 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
205 os=ASN1_OCTET_STRING_new();
206 ASN1_OCTET_STRING_set(os,str,strlen(str));
207 /* When we add, we do not free */
208 ret=X509_ATTRIBUTE_create(signed_string_nid,
209 V_ASN1_OCTET_STRING,(char *)os);
213 int sk_get_string(STACK *sk, char *buf, int len)
216 ASN1_OCTET_STRING *os;
218 PKCS7_SIGNER_INFO si;
222 if (signed_string_nid == -1)
224 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
226 so=PKCS7_get_signed_attribute(&si,signed_string_nid);
229 if (so->type == V_ASN1_OCTET_STRING)
231 os=so->value.octet_string;
235 memcpy(buf,os->data,i);
242 X509_ATTRIBUTE *add_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2)
244 /* To add an object of OID 1.9.999, which is a sequence containing
247 ASN1_OCTET_STRING *os1,*os2;
253 if (signed_seq2string_nid == -1)
254 signed_seq2string_nid=
255 OBJ_create("1.9.9999","OID_example","Our example OID");
257 os1=ASN1_OCTET_STRING_new();
258 os2=ASN1_OCTET_STRING_new();
259 ASN1_OCTET_STRING_set(os1,str1,strlen(str1));
260 ASN1_OCTET_STRING_set(os2,str1,strlen(str1));
261 i =i2d_ASN1_OCTET_STRING(os1,NULL);
262 i+=i2d_ASN1_OCTET_STRING(os2,NULL);
263 total=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
267 ASN1_put_object(&p,1,i,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
268 i2d_ASN1_OCTET_STRING(os1,&p);
269 i2d_ASN1_OCTET_STRING(os2,&p);
271 seq=ASN1_STRING_new();
272 ASN1_STRING_set(seq,data,total);
274 ASN1_OCTET_STRING_free(os1);
275 ASN1_OCTET_STRING_free(os2);
277 ret=X509_ATTRIBUTE_create(signed_seq2string_nid,
278 V_ASN1_SEQUENCE,(char *)seq);
282 /* For this case, I will malloc the return strings */
283 int sk_get_seq2string(STACK *sk, char **str1, char **str2)
286 PKCS7_SIGNER_INFO si;
288 if (signed_seq2string_nid == -1)
289 signed_seq2string_nid=
290 OBJ_create("1.9.9999","OID_example","Our example OID");
294 so=PKCS7_get_signed_attribute(&si,signed_seq2string_nid);
295 if (so->type == V_ASN1_SEQUENCE)
300 ASN1_OCTET_STRING *os1,*os2;
302 s=so->value.sequence;
303 c.p=ASN1_STRING_data(s);
304 c.max=c.p+ASN1_STRING_length(s);
305 if (!asn1_GetSequence(&c,&length)) goto err;
306 /* Length is the length of the seqence */
309 if ((os1=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
314 if ((os2=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL)
318 if (!asn1_Finish(&c)) goto err;
319 *str1=malloc(os1->length+1);
320 *str2=malloc(os2->length+1);
321 memcpy(*str1,os1->data,os1->length);
322 memcpy(*str2,os2->data,os2->length);
323 (*str1)[os1->length]='\0';
324 (*str2)[os2->length]='\0';
325 ASN1_OCTET_STRING_free(os1);
326 ASN1_OCTET_STRING_free(os2);