diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs7/example.c')
-rw-r--r-- | src/lib/libcrypto/pkcs7/example.c | 340 |
1 files changed, 0 insertions, 340 deletions
diff --git a/src/lib/libcrypto/pkcs7/example.c b/src/lib/libcrypto/pkcs7/example.c deleted file mode 100644 index 2dc1b655b1..0000000000 --- a/src/lib/libcrypto/pkcs7/example.c +++ /dev/null | |||
@@ -1,340 +0,0 @@ | |||
1 | /* $OpenBSD: example.c,v 1.6 2014/06/29 17:05:36 jsing Exp $ */ | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <openssl/pkcs7.h> | ||
6 | #include <openssl/asn1_mac.h> | ||
7 | #include <openssl/x509.h> | ||
8 | |||
9 | int | ||
10 | add_signed_time(PKCS7_SIGNER_INFO *si) | ||
11 | { | ||
12 | ASN1_UTCTIME *sign_time; | ||
13 | |||
14 | /* The last parameter is the amount to add/subtract from the current | ||
15 | * time (in seconds) */ | ||
16 | sign_time = X509_gmtime_adj(NULL, 0); | ||
17 | PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime, | ||
18 | V_ASN1_UTCTIME, (char *)sign_time); | ||
19 | return (1); | ||
20 | } | ||
21 | |||
22 | ASN1_UTCTIME * | ||
23 | get_signed_time(PKCS7_SIGNER_INFO *si) | ||
24 | { | ||
25 | ASN1_TYPE *so; | ||
26 | |||
27 | so = PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime); | ||
28 | if (so->type == V_ASN1_UTCTIME) | ||
29 | return so->value.utctime; | ||
30 | return NULL; | ||
31 | } | ||
32 | |||
33 | static int signed_string_nid = -1; | ||
34 | |||
35 | void | ||
36 | add_signed_string(PKCS7_SIGNER_INFO *si, char *str) | ||
37 | { | ||
38 | ASN1_OCTET_STRING *os; | ||
39 | |||
40 | /* To a an object of OID 1.2.3.4.5, which is an octet string */ | ||
41 | if (signed_string_nid == -1) | ||
42 | signed_string_nid = | ||
43 | OBJ_create("1.2.3.4.5","OID_example","Our example OID"); | ||
44 | os = ASN1_OCTET_STRING_new(); | ||
45 | ASN1_OCTET_STRING_set(os, (unsigned char*)str, strlen(str)); | ||
46 | /* When we add, we do not free */ | ||
47 | PKCS7_add_signed_attribute(si, signed_string_nid, | ||
48 | V_ASN1_OCTET_STRING, (char *)os); | ||
49 | } | ||
50 | |||
51 | int | ||
52 | get_signed_string(PKCS7_SIGNER_INFO *si, char *buf, int len) | ||
53 | { | ||
54 | ASN1_TYPE *so; | ||
55 | ASN1_OCTET_STRING *os; | ||
56 | int i; | ||
57 | |||
58 | if (signed_string_nid == -1) | ||
59 | signed_string_nid = | ||
60 | OBJ_create("1.2.3.4.5","OID_example","Our example OID"); | ||
61 | /* To retrieve */ | ||
62 | so = PKCS7_get_signed_attribute(si, signed_string_nid); | ||
63 | if (so != NULL) { | ||
64 | if (so->type == V_ASN1_OCTET_STRING) { | ||
65 | os = so->value.octet_string; | ||
66 | i = os->length; | ||
67 | if ((i + 1) > len) | ||
68 | i = len - 1; | ||
69 | memcpy(buf, os->data, i); | ||
70 | return (i); | ||
71 | } | ||
72 | } | ||
73 | return (0); | ||
74 | } | ||
75 | |||
76 | static int signed_seq2string_nid = -1; | ||
77 | /* ########################################### */ | ||
78 | int | ||
79 | add_signed_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2) | ||
80 | { | ||
81 | /* To add an object of OID 1.9.999, which is a sequence containing | ||
82 | * 2 octet strings */ | ||
83 | unsigned char *p; | ||
84 | ASN1_OCTET_STRING *os1, *os2; | ||
85 | ASN1_STRING *seq; | ||
86 | unsigned char *data; | ||
87 | int i, total; | ||
88 | |||
89 | if (signed_seq2string_nid == -1) | ||
90 | signed_seq2string_nid = | ||
91 | OBJ_create("1.9.9999","OID_example","Our example OID"); | ||
92 | |||
93 | os1 = ASN1_OCTET_STRING_new(); | ||
94 | os2 = ASN1_OCTET_STRING_new(); | ||
95 | ASN1_OCTET_STRING_set(os1, (unsigned char*)str1, strlen(str1)); | ||
96 | ASN1_OCTET_STRING_set(os2, (unsigned char*)str1, strlen(str1)); | ||
97 | i = i2d_ASN1_OCTET_STRING(os1, NULL); | ||
98 | i += i2d_ASN1_OCTET_STRING(os2, NULL); | ||
99 | total = ASN1_object_size(1, i, V_ASN1_SEQUENCE); | ||
100 | |||
101 | data = malloc(total); | ||
102 | p = data; | ||
103 | ASN1_put_object(&p, 1,i, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); | ||
104 | i2d_ASN1_OCTET_STRING(os1, &p); | ||
105 | i2d_ASN1_OCTET_STRING(os2, &p); | ||
106 | |||
107 | seq = ASN1_STRING_new(); | ||
108 | ASN1_STRING_set(seq, data, total); | ||
109 | free(data); | ||
110 | ASN1_OCTET_STRING_free(os1); | ||
111 | ASN1_OCTET_STRING_free(os2); | ||
112 | |||
113 | PKCS7_add_signed_attribute(si, signed_seq2string_nid, | ||
114 | V_ASN1_SEQUENCE, (char *)seq); | ||
115 | return (1); | ||
116 | } | ||
117 | |||
118 | /* For this case, I will malloc the return strings */ | ||
119 | int | ||
120 | get_signed_seq2string(PKCS7_SIGNER_INFO *si, char **str1, char **str2) | ||
121 | { | ||
122 | ASN1_TYPE *so; | ||
123 | |||
124 | if (signed_seq2string_nid == -1) | ||
125 | signed_seq2string_nid = | ||
126 | OBJ_create("1.9.9999","OID_example","Our example OID"); | ||
127 | /* To retrieve */ | ||
128 | so = PKCS7_get_signed_attribute(si, signed_seq2string_nid); | ||
129 | if (so && (so->type == V_ASN1_SEQUENCE)) { | ||
130 | ASN1_const_CTX c; | ||
131 | ASN1_STRING *s; | ||
132 | long length; | ||
133 | ASN1_OCTET_STRING *os1, *os2; | ||
134 | |||
135 | s = so->value.sequence; | ||
136 | c.p = ASN1_STRING_data(s); | ||
137 | c.max = c.p + ASN1_STRING_length(s); | ||
138 | if (!asn1_GetSequence(&c, &length)) | ||
139 | goto err; | ||
140 | /* Length is the length of the seqence */ | ||
141 | |||
142 | c.q = c.p; | ||
143 | if ((os1 = d2i_ASN1_OCTET_STRING(NULL, &c.p, c.slen)) == NULL) | ||
144 | goto err; | ||
145 | c.slen -= (c.p - c.q); | ||
146 | |||
147 | c.q = c.p; | ||
148 | if ((os2 = d2i_ASN1_OCTET_STRING(NULL, &c.p, c.slen)) == NULL) | ||
149 | goto err; | ||
150 | c.slen -= (c.p - c.q); | ||
151 | |||
152 | if (!asn1_const_Finish(&c)) | ||
153 | goto err; | ||
154 | *str1 = malloc(os1->length + 1); | ||
155 | *str2 = malloc(os2->length + 1); | ||
156 | memcpy(*str1, os1->data, os1->length); | ||
157 | memcpy(*str2, os2->data, os2->length); | ||
158 | (*str1)[os1->length]='\0'; | ||
159 | (*str2)[os2->length]='\0'; | ||
160 | ASN1_OCTET_STRING_free(os1); | ||
161 | ASN1_OCTET_STRING_free(os2); | ||
162 | return (1); | ||
163 | } | ||
164 | err: | ||
165 | return (0); | ||
166 | } | ||
167 | |||
168 | |||
169 | /* ####################################### | ||
170 | * THE OTHER WAY TO DO THINGS | ||
171 | * ####################################### | ||
172 | */ | ||
173 | X509_ATTRIBUTE * | ||
174 | create_time(void) | ||
175 | { | ||
176 | ASN1_UTCTIME *sign_time; | ||
177 | X509_ATTRIBUTE *ret; | ||
178 | |||
179 | /* The last parameter is the amount to add/subtract from the current | ||
180 | * time (in seconds) */ | ||
181 | sign_time = X509_gmtime_adj(NULL, 0); | ||
182 | ret = X509_ATTRIBUTE_create(NID_pkcs9_signingTime, | ||
183 | V_ASN1_UTCTIME, (char *)sign_time); | ||
184 | return (ret); | ||
185 | } | ||
186 | |||
187 | ASN1_UTCTIME * | ||
188 | sk_get_time(STACK_OF(X509_ATTRIBUTE) *sk) | ||
189 | { | ||
190 | ASN1_TYPE *so; | ||
191 | PKCS7_SIGNER_INFO si; | ||
192 | |||
193 | si.auth_attr = sk; | ||
194 | so = PKCS7_get_signed_attribute(&si, NID_pkcs9_signingTime); | ||
195 | if (so->type == V_ASN1_UTCTIME) | ||
196 | return so->value.utctime; | ||
197 | return NULL; | ||
198 | } | ||
199 | |||
200 | X509_ATTRIBUTE * | ||
201 | create_string(char *str) | ||
202 | { | ||
203 | ASN1_OCTET_STRING *os; | ||
204 | X509_ATTRIBUTE *ret; | ||
205 | |||
206 | /* To a an object of OID 1.2.3.4.5, which is an octet string */ | ||
207 | if (signed_string_nid == -1) | ||
208 | signed_string_nid = | ||
209 | OBJ_create("1.2.3.4.5","OID_example","Our example OID"); | ||
210 | os = ASN1_OCTET_STRING_new(); | ||
211 | ASN1_OCTET_STRING_set(os, (unsigned char*)str, strlen(str)); | ||
212 | /* When we add, we do not free */ | ||
213 | ret = X509_ATTRIBUTE_create(signed_string_nid, | ||
214 | V_ASN1_OCTET_STRING, (char *)os); | ||
215 | return (ret); | ||
216 | } | ||
217 | |||
218 | int | ||
219 | sk_get_string(STACK_OF(X509_ATTRIBUTE) *sk, char *buf, int len) | ||
220 | { | ||
221 | ASN1_TYPE *so; | ||
222 | ASN1_OCTET_STRING *os; | ||
223 | int i; | ||
224 | PKCS7_SIGNER_INFO si; | ||
225 | |||
226 | si.auth_attr = sk; | ||
227 | |||
228 | if (signed_string_nid == -1) | ||
229 | signed_string_nid = | ||
230 | OBJ_create("1.2.3.4.5","OID_example","Our example OID"); | ||
231 | /* To retrieve */ | ||
232 | so = PKCS7_get_signed_attribute(&si, signed_string_nid); | ||
233 | if (so != NULL) { | ||
234 | if (so->type == V_ASN1_OCTET_STRING) { | ||
235 | os = so->value.octet_string; | ||
236 | i = os->length; | ||
237 | if ((i + 1) > len) | ||
238 | i = len - 1; | ||
239 | memcpy(buf, os->data, i); | ||
240 | return (i); | ||
241 | } | ||
242 | } | ||
243 | return (0); | ||
244 | } | ||
245 | |||
246 | X509_ATTRIBUTE * | ||
247 | add_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2) | ||
248 | { | ||
249 | /* To add an object of OID 1.9.999, which is a sequence containing | ||
250 | * 2 octet strings */ | ||
251 | unsigned char *p; | ||
252 | ASN1_OCTET_STRING *os1, *os2; | ||
253 | ASN1_STRING *seq; | ||
254 | X509_ATTRIBUTE *ret; | ||
255 | unsigned char *data; | ||
256 | int i, total; | ||
257 | |||
258 | if (signed_seq2string_nid == -1) | ||
259 | signed_seq2string_nid = | ||
260 | OBJ_create("1.9.9999","OID_example","Our example OID"); | ||
261 | |||
262 | os1 = ASN1_OCTET_STRING_new(); | ||
263 | os2 = ASN1_OCTET_STRING_new(); | ||
264 | ASN1_OCTET_STRING_set(os1, (unsigned char*)str1, strlen(str1)); | ||
265 | ASN1_OCTET_STRING_set(os2, (unsigned char*)str1, strlen(str1)); | ||
266 | i = i2d_ASN1_OCTET_STRING(os1, NULL); | ||
267 | i += i2d_ASN1_OCTET_STRING(os2, NULL); | ||
268 | total = ASN1_object_size(1, i, V_ASN1_SEQUENCE); | ||
269 | |||
270 | data = malloc(total); | ||
271 | p = data; | ||
272 | ASN1_put_object(&p, 1,i, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); | ||
273 | i2d_ASN1_OCTET_STRING(os1, &p); | ||
274 | i2d_ASN1_OCTET_STRING(os2, &p); | ||
275 | |||
276 | seq = ASN1_STRING_new(); | ||
277 | ASN1_STRING_set(seq, data, total); | ||
278 | free(data); | ||
279 | ASN1_OCTET_STRING_free(os1); | ||
280 | ASN1_OCTET_STRING_free(os2); | ||
281 | |||
282 | ret = X509_ATTRIBUTE_create(signed_seq2string_nid, | ||
283 | V_ASN1_SEQUENCE, (char *)seq); | ||
284 | return (ret); | ||
285 | } | ||
286 | |||
287 | /* For this case, I will malloc the return strings */ | ||
288 | int | ||
289 | sk_get_seq2string(STACK_OF(X509_ATTRIBUTE) *sk, char **str1, char **str2) | ||
290 | { | ||
291 | ASN1_TYPE *so; | ||
292 | PKCS7_SIGNER_INFO si; | ||
293 | |||
294 | if (signed_seq2string_nid == -1) | ||
295 | signed_seq2string_nid = | ||
296 | OBJ_create("1.9.9999","OID_example","Our example OID"); | ||
297 | |||
298 | si.auth_attr = sk; | ||
299 | /* To retrieve */ | ||
300 | so = PKCS7_get_signed_attribute(&si, signed_seq2string_nid); | ||
301 | if (so->type == V_ASN1_SEQUENCE) { | ||
302 | ASN1_const_CTX c; | ||
303 | ASN1_STRING *s; | ||
304 | long length; | ||
305 | ASN1_OCTET_STRING *os1, *os2; | ||
306 | |||
307 | s = so->value.sequence; | ||
308 | c.p = ASN1_STRING_data(s); | ||
309 | c.max = c.p + ASN1_STRING_length(s); | ||
310 | if (!asn1_GetSequence(&c, &length)) | ||
311 | goto err; | ||
312 | /* Length is the length of the seqence */ | ||
313 | |||
314 | c.q = c.p; | ||
315 | if ((os1 = d2i_ASN1_OCTET_STRING(NULL, &c.p, c.slen)) == NULL) | ||
316 | goto err; | ||
317 | c.slen -= (c.p - c.q); | ||
318 | |||
319 | c.q = c.p; | ||
320 | if ((os2 = d2i_ASN1_OCTET_STRING(NULL, &c.p, c.slen)) == NULL) | ||
321 | goto err; | ||
322 | c.slen -= (c.p - c.q); | ||
323 | |||
324 | if (!asn1_const_Finish(&c)) | ||
325 | goto err; | ||
326 | *str1 = malloc(os1->length + 1); | ||
327 | *str2 = malloc(os2->length + 1); | ||
328 | memcpy(*str1, os1->data, os1->length); | ||
329 | memcpy(*str2, os2->data, os2->length); | ||
330 | (*str1)[os1->length]='\0'; | ||
331 | (*str2)[os2->length]='\0'; | ||
332 | ASN1_OCTET_STRING_free(os1); | ||
333 | ASN1_OCTET_STRING_free(os2); | ||
334 | return (1); | ||
335 | } | ||
336 | err: | ||
337 | return (0); | ||
338 | } | ||
339 | |||
340 | |||