I got sick and tired of having to keep track of NIDs when such a thing
[openssl.git] / crypto / pkcs7 / pk7_attr.c
1 /* pk7_attr.c */
2 /* S/MIME code.
3  * Copyright (C) 1997-8 Dr S N Henson (shenson@bigfoot.com) 
4  * All Rights Reserved. 
5  * Redistribution of this code without the authors permission is expressly
6  * prohibited.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <openssl/bio.h>
12 #include <openssl/asn1.h>
13 #include <openssl/pem.h>
14 #include <openssl/pkcs7.h>
15 #include <openssl/x509.h>
16 #include <openssl/err.h>
17
18 int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, STACK_OF(X509_ALGOR) *cap)
19 {
20         ASN1_STRING *seq;
21         unsigned char *p, *pp;
22         int len;
23         len=i2d_ASN1_SET_OF_X509_ALGOR(cap,NULL,i2d_X509_ALGOR,
24                                        V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL,
25                                        IS_SEQUENCE);
26         if(!(pp=(unsigned char *)OPENSSL_malloc(len))) {
27                 PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE);
28                 return 0;
29         }
30         p=pp;
31         i2d_ASN1_SET_OF_X509_ALGOR(cap,&p,i2d_X509_ALGOR, V_ASN1_SEQUENCE,
32                                    V_ASN1_UNIVERSAL, IS_SEQUENCE);
33         if(!(seq = ASN1_STRING_new())) {
34                 PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE);
35                 return 0;
36         }
37         if(!ASN1_STRING_set (seq, pp, len)) {
38                 PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE);
39                 return 0;
40         }
41         OPENSSL_free (pp);
42         return PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
43                                                         V_ASN1_SEQUENCE, seq);
44 }
45
46 STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
47 {
48         ASN1_TYPE *cap;
49         unsigned char *p;
50         cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
51         if (!cap) return NULL;
52         p = cap->value.sequence->data;
53         return d2i_ASN1_SET_OF_X509_ALGOR(NULL, &p,
54                                           cap->value.sequence->length,
55                                           d2i_X509_ALGOR, X509_ALGOR_free,
56                                           V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
57 }
58
59 /* Basic smime-capabilities OID and optional integer arg */
60 int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
61 {
62         X509_ALGOR *alg;
63
64         if(!(alg = X509_ALGOR_new())) {
65                 PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
66                 return 0;
67         }
68         ASN1_OBJECT_free(alg->algorithm);
69         alg->algorithm = OBJ_nid2obj (nid);
70         if (arg > 0) {
71                 ASN1_INTEGER *nbit;
72                 if(!(alg->parameter = ASN1_TYPE_new())) {
73                         PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
74                         return 0;
75                 }
76                 if(!(nbit = ASN1_INTEGER_new())) {
77                         PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
78                         return 0;
79                 }
80                 if(!ASN1_INTEGER_set (nbit, arg)) {
81                         PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
82                         return 0;
83                 }
84                 alg->parameter->value.integer = nbit;
85                 alg->parameter->type = V_ASN1_INTEGER;
86         }
87         sk_X509_ALGOR_push (sk, alg);
88         return 1;
89 }