]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs7/example.c
3 #include <openssl/pkcs7.h>
4 #include <openssl/asn1_mac.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
)
22 so
=PKCS7_get_signed_attribute(si
,NID_pkcs9_signingTime
);
23 if (so
->type
== V_ASN1_UTCTIME
)
24 return so
->value
.utctime
;
28 static int signed_string_nid
= -1;
30 void add_signed_string(PKCS7_SIGNER_INFO
*si
, char *str
)
32 ASN1_OCTET_STRING
*os
;
34 /* To a an object of OID 1.2.3.4.5, which is an octet string */
35 if (signed_string_nid
== -1)
37 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
38 os
=ASN1_OCTET_STRING_new();
39 ASN1_OCTET_STRING_set(os
,(unsigned char*)str
,strlen(str
));
40 /* When we add, we do not free */
41 PKCS7_add_signed_attribute(si
,signed_string_nid
,
42 V_ASN1_OCTET_STRING
,(char *)os
);
45 int get_signed_string(PKCS7_SIGNER_INFO
*si
, char *buf
, int len
)
48 ASN1_OCTET_STRING
*os
;
51 if (signed_string_nid
== -1)
53 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
55 so
=PKCS7_get_signed_attribute(si
,signed_string_nid
);
58 if (so
->type
== V_ASN1_OCTET_STRING
)
60 os
=so
->value
.octet_string
;
64 memcpy(buf
,os
->data
,i
);
71 static signed_seq2string_nid
= -1;
72 /* ########################################### */
73 int add_signed_seq2string(PKCS7_SIGNER_INFO
*si
, char *str1
, char *str2
)
75 /* To add an object of OID 1.9.999, which is a sequence containing
78 ASN1_OCTET_STRING
*os1
,*os2
;
83 if (signed_seq2string_nid
== -1)
84 signed_seq2string_nid
=
85 OBJ_create("1.9.9999","OID_example","Our example OID");
87 os1
=ASN1_OCTET_STRING_new();
88 os2
=ASN1_OCTET_STRING_new();
89 ASN1_OCTET_STRING_set(os1
,(unsigned char*)str1
,strlen(str1
));
90 ASN1_OCTET_STRING_set(os2
,(unsigned char*)str1
,strlen(str1
));
91 i
=i2d_ASN1_OCTET_STRING(os1
,NULL
);
92 i
+=i2d_ASN1_OCTET_STRING(os2
,NULL
);
93 total
=ASN1_object_size(1,i
,V_ASN1_SEQUENCE
);
97 ASN1_put_object(&p
,1,i
,V_ASN1_SEQUENCE
,V_ASN1_UNIVERSAL
);
98 i2d_ASN1_OCTET_STRING(os1
,&p
);
99 i2d_ASN1_OCTET_STRING(os2
,&p
);
101 seq
=ASN1_STRING_new();
102 ASN1_STRING_set(seq
,data
,total
);
104 ASN1_OCTET_STRING_free(os1
);
105 ASN1_OCTET_STRING_free(os2
);
107 PKCS7_add_signed_attribute(si
,signed_seq2string_nid
,
108 V_ASN1_SEQUENCE
,(char *)seq
);
112 /* For this case, I will malloc the return strings */
113 int get_signed_seq2string(PKCS7_SIGNER_INFO
*si
, char **str1
, char **str2
)
117 if (signed_seq2string_nid
== -1)
118 signed_seq2string_nid
=
119 OBJ_create("1.9.9999","OID_example","Our example OID");
121 so
=PKCS7_get_signed_attribute(si
,signed_seq2string_nid
);
122 if (so
&& (so
->type
== V_ASN1_SEQUENCE
))
127 ASN1_OCTET_STRING
*os1
,*os2
;
129 s
=so
->value
.sequence
;
130 c
.p
=ASN1_STRING_data(s
);
131 c
.max
=c
.p
+ASN1_STRING_length(s
);
132 if (!asn1_GetSequence(&c
,&length
)) goto err
;
133 /* Length is the length of the seqence */
136 if ((os1
=d2i_ASN1_OCTET_STRING(NULL
,&c
.p
,c
.slen
)) == NULL
)
141 if ((os2
=d2i_ASN1_OCTET_STRING(NULL
,&c
.p
,c
.slen
)) == NULL
)
145 if (!asn1_Finish(&c
)) goto err
;
146 *str1
=malloc(os1
->length
+1);
147 *str2
=malloc(os2
->length
+1);
148 memcpy(*str1
,os1
->data
,os1
->length
);
149 memcpy(*str2
,os2
->data
,os2
->length
);
150 (*str1
)[os1
->length
]='\0';
151 (*str2
)[os2
->length
]='\0';
152 ASN1_OCTET_STRING_free(os1
);
153 ASN1_OCTET_STRING_free(os2
);
161 /* #######################################
162 * THE OTHER WAY TO DO THINGS
163 * #######################################
165 X509_ATTRIBUTE
*create_time(void)
167 ASN1_UTCTIME
*sign_time
;
170 /* The last parameter is the amount to add/subtract from the current
171 * time (in seconds) */
172 sign_time
=X509_gmtime_adj(NULL
,0);
173 ret
=X509_ATTRIBUTE_create(NID_pkcs9_signingTime
,
174 V_ASN1_UTCTIME
,(char *)sign_time
);
178 ASN1_UTCTIME
*sk_get_time(STACK_OF(X509_ATTRIBUTE
) *sk
)
181 PKCS7_SIGNER_INFO si
;
184 so
=PKCS7_get_signed_attribute(&si
,NID_pkcs9_signingTime
);
185 if (so
->type
== V_ASN1_UTCTIME
)
186 return so
->value
.utctime
;
190 X509_ATTRIBUTE
*create_string(char *str
)
192 ASN1_OCTET_STRING
*os
;
195 /* To a an object of OID 1.2.3.4.5, which is an octet string */
196 if (signed_string_nid
== -1)
198 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
199 os
=ASN1_OCTET_STRING_new();
200 ASN1_OCTET_STRING_set(os
,(unsigned char*)str
,strlen(str
));
201 /* When we add, we do not free */
202 ret
=X509_ATTRIBUTE_create(signed_string_nid
,
203 V_ASN1_OCTET_STRING
,(char *)os
);
207 int sk_get_string(STACK_OF(X509_ATTRIBUTE
) *sk
, char *buf
, int len
)
210 ASN1_OCTET_STRING
*os
;
212 PKCS7_SIGNER_INFO si
;
216 if (signed_string_nid
== -1)
218 OBJ_create("1.2.3.4.5","OID_example","Our example OID");
220 so
=PKCS7_get_signed_attribute(&si
,signed_string_nid
);
223 if (so
->type
== V_ASN1_OCTET_STRING
)
225 os
=so
->value
.octet_string
;
229 memcpy(buf
,os
->data
,i
);
236 X509_ATTRIBUTE
*add_seq2string(PKCS7_SIGNER_INFO
*si
, char *str1
, char *str2
)
238 /* To add an object of OID 1.9.999, which is a sequence containing
241 ASN1_OCTET_STRING
*os1
,*os2
;
247 if (signed_seq2string_nid
== -1)
248 signed_seq2string_nid
=
249 OBJ_create("1.9.9999","OID_example","Our example OID");
251 os1
=ASN1_OCTET_STRING_new();
252 os2
=ASN1_OCTET_STRING_new();
253 ASN1_OCTET_STRING_set(os1
,(unsigned char*)str1
,strlen(str1
));
254 ASN1_OCTET_STRING_set(os2
,(unsigned char*)str1
,strlen(str1
));
255 i
=i2d_ASN1_OCTET_STRING(os1
,NULL
);
256 i
+=i2d_ASN1_OCTET_STRING(os2
,NULL
);
257 total
=ASN1_object_size(1,i
,V_ASN1_SEQUENCE
);
261 ASN1_put_object(&p
,1,i
,V_ASN1_SEQUENCE
,V_ASN1_UNIVERSAL
);
262 i2d_ASN1_OCTET_STRING(os1
,&p
);
263 i2d_ASN1_OCTET_STRING(os2
,&p
);
265 seq
=ASN1_STRING_new();
266 ASN1_STRING_set(seq
,data
,total
);
268 ASN1_OCTET_STRING_free(os1
);
269 ASN1_OCTET_STRING_free(os2
);
271 ret
=X509_ATTRIBUTE_create(signed_seq2string_nid
,
272 V_ASN1_SEQUENCE
,(char *)seq
);
276 /* For this case, I will malloc the return strings */
277 int sk_get_seq2string(STACK_OF(X509_ATTRIBUTE
) *sk
, char **str1
, char **str2
)
280 PKCS7_SIGNER_INFO si
;
282 if (signed_seq2string_nid
== -1)
283 signed_seq2string_nid
=
284 OBJ_create("1.9.9999","OID_example","Our example OID");
288 so
=PKCS7_get_signed_attribute(&si
,signed_seq2string_nid
);
289 if (so
->type
== V_ASN1_SEQUENCE
)
294 ASN1_OCTET_STRING
*os1
,*os2
;
296 s
=so
->value
.sequence
;
297 c
.p
=ASN1_STRING_data(s
);
298 c
.max
=c
.p
+ASN1_STRING_length(s
);
299 if (!asn1_GetSequence(&c
,&length
)) goto err
;
300 /* Length is the length of the seqence */
303 if ((os1
=d2i_ASN1_OCTET_STRING(NULL
,&c
.p
,c
.slen
)) == NULL
)
308 if ((os2
=d2i_ASN1_OCTET_STRING(NULL
,&c
.p
,c
.slen
)) == NULL
)
312 if (!asn1_Finish(&c
)) goto err
;
313 *str1
=malloc(os1
->length
+1);
314 *str2
=malloc(os2
->length
+1);
315 memcpy(*str1
,os1
->data
,os1
->length
);
316 memcpy(*str2
,os2
->data
,os2
->length
);
317 (*str1
)[os1
->length
]='\0';
318 (*str2
)[os2
->length
]='\0';
319 ASN1_OCTET_STRING_free(os1
);
320 ASN1_OCTET_STRING_free(os2
);