summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/pkcs12/p12tmpl.c
blob: b08384f66a487df7e1df1a686aef70e8fb65ecf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "plarena.h"
#include "secitem.h"
#include "secoid.h"
#include "seccomon.h"
#include "secport.h"
#include "cert.h"
#include "secpkcs7.h"
#include "secasn1.h"
#include "p12t.h"

SEC_ASN1_MKSUB(SEC_AnyTemplate)
SEC_ASN1_MKSUB(sgn_DigestInfoTemplate)

static const SEC_ASN1Template *
sec_pkcs12_choose_safe_bag_type(void *src_or_dest, PRBool encoding)
{
    const SEC_ASN1Template *theTemplate;
    sec_PKCS12SafeBag *safeBag;
    SECOidData *oiddata;

    if (src_or_dest == NULL) {
        return NULL;
    }

    safeBag = (sec_PKCS12SafeBag *)src_or_dest;

    oiddata = SECOID_FindOID(&safeBag->safeBagType);
    if (oiddata == NULL) {
        return SEC_ASN1_GET(SEC_PointerToAnyTemplate);
    }

    switch (oiddata->offset) {
        default:
            theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate);
            break;
        case SEC_OID_PKCS12_V1_KEY_BAG_ID:
            theTemplate = SEC_ASN1_GET(SECKEY_PointerToPrivateKeyInfoTemplate);
            break;
        case SEC_OID_PKCS12_V1_CERT_BAG_ID:
            theTemplate = sec_PKCS12PointerToCertBagTemplate;
            break;
        case SEC_OID_PKCS12_V1_CRL_BAG_ID:
            theTemplate = sec_PKCS12PointerToCRLBagTemplate;
            break;
        case SEC_OID_PKCS12_V1_SECRET_BAG_ID:
            theTemplate = sec_PKCS12PointerToSecretBagTemplate;
            break;
        case SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID:
            theTemplate =
                SEC_ASN1_GET(SECKEY_PointerToEncryptedPrivateKeyInfoTemplate);
            break;
        case SEC_OID_PKCS12_V1_SAFE_CONTENTS_BAG_ID:
            if (encoding) {
                theTemplate = sec_PKCS12PointerToSafeContentsTemplate;
            } else {
                theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate);
            }
            break;
    }
    return theTemplate;
}

static const SEC_ASN1Template *
sec_pkcs12_choose_crl_bag_type(void *src_or_dest, PRBool encoding)
{
    const SEC_ASN1Template *theTemplate;
    sec_PKCS12CRLBag *crlbag;
    SECOidData *oiddata;

    if (src_or_dest == NULL) {
        return NULL;
    }

    crlbag = (sec_PKCS12CRLBag *)src_or_dest;

    oiddata = SECOID_FindOID(&crlbag->bagID);
    if (oiddata == NULL) {
        return SEC_ASN1_GET(SEC_AnyTemplate);
    }

    switch (oiddata->offset) {
        default:
            theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
            break;
        case SEC_OID_PKCS9_X509_CRL:
            theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
            break;
    }
    return theTemplate;
}

static const SEC_ASN1Template *
sec_pkcs12_choose_cert_bag_type(void *src_or_dest, PRBool encoding)
{
    const SEC_ASN1Template *theTemplate;
    sec_PKCS12CertBag *certbag;
    SECOidData *oiddata;

    if (src_or_dest == NULL) {
        return NULL;
    }

    certbag = (sec_PKCS12CertBag *)src_or_dest;

    oiddata = SECOID_FindOID(&certbag->bagID);
    if (oiddata == NULL) {
        return SEC_ASN1_GET(SEC_AnyTemplate);
    }

    switch (oiddata->offset) {
        default:
            theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
            break;
        case SEC_OID_PKCS9_X509_CERT:
            theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
            break;
        case SEC_OID_PKCS9_SDSI_CERT:
            theTemplate = SEC_ASN1_GET(SEC_IA5StringTemplate);
            break;
    }
    return theTemplate;
}

static const SEC_ASN1Template *
sec_pkcs12_choose_attr_type(void *src_or_dest, PRBool encoding)
{
    const SEC_ASN1Template *theTemplate;
    sec_PKCS12Attribute *attr;
    SECOidData *oiddata;

    if (src_or_dest == NULL) {
        return NULL;
    }

    attr = (sec_PKCS12Attribute *)src_or_dest;

    oiddata = SECOID_FindOID(&attr->attrType);
    if (oiddata == NULL) {
        return SEC_ASN1_GET(SEC_AnyTemplate);
    }

    switch (oiddata->offset) {
        default:
            theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
            break;
        case SEC_OID_PKCS9_FRIENDLY_NAME:
            theTemplate = SEC_ASN1_GET(SEC_BMPStringTemplate);
            break;
        case SEC_OID_PKCS9_LOCAL_KEY_ID:
            theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
            break;
        case SEC_OID_PKCS12_KEY_USAGE:
            theTemplate = SEC_ASN1_GET(SEC_BitStringTemplate);
            break;
    }

    return theTemplate;
}

const SEC_ASN1Template sec_PKCS12PointerToContentInfoTemplate[] = {
    { SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM, 0, sec_PKCS7ContentInfoTemplate }
};

static const SEC_ASN1TemplateChooserPtr sec_pkcs12_crl_bag_chooser =
    sec_pkcs12_choose_crl_bag_type;

static const SEC_ASN1TemplateChooserPtr sec_pkcs12_cert_bag_chooser =
    sec_pkcs12_choose_cert_bag_type;

static const SEC_ASN1TemplateChooserPtr sec_pkcs12_safe_bag_chooser =
    sec_pkcs12_choose_safe_bag_type;

static const SEC_ASN1TemplateChooserPtr sec_pkcs12_attr_chooser =
    sec_pkcs12_choose_attr_type;

const SEC_ASN1Template sec_PKCS12PointerToCertBagTemplate[] = {
    { SEC_ASN1_POINTER, 0, sec_PKCS12CertBagTemplate }
};

const SEC_ASN1Template sec_PKCS12PointerToCRLBagTemplate[] = {
    { SEC_ASN1_POINTER, 0, sec_PKCS12CRLBagTemplate }
};

const SEC_ASN1Template sec_PKCS12PointerToSecretBagTemplate[] = {
    { SEC_ASN1_POINTER, 0, sec_PKCS12SecretBagTemplate }
};

const SEC_ASN1Template sec_PKCS12PointerToSafeContentsTemplate[] = {
    { SEC_ASN1_POINTER, 0, sec_PKCS12SafeContentsTemplate }
};

const SEC_ASN1Template sec_PKCS12PFXItemTemplate[] = {
    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL,
      sizeof(sec_PKCS12PFXItem) },
    { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER,
      offsetof(sec_PKCS12PFXItem, version) },
    { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM,
      offsetof(sec_PKCS12PFXItem, encodedAuthSafe) },
    { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM,
      offsetof(sec_PKCS12PFXItem, encodedMacData) },
    { 0 }
};

const SEC_ASN1Template sec_PKCS12MacDataTemplate[] = {
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12MacData) },
    { SEC_ASN1_INLINE | SEC_ASN1_XTRN, offsetof(sec_PKCS12MacData, safeMac),
      SEC_ASN1_SUB(sgn_DigestInfoTemplate) },
    { SEC_ASN1_OCTET_STRING, offsetof(sec_PKCS12MacData, macSalt) },
    { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, offsetof(sec_PKCS12MacData, iter) },
    { 0 }
};

const SEC_ASN1Template sec_PKCS12AuthenticatedSafeTemplate[] = {
    { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN,
      offsetof(sec_PKCS12AuthenticatedSafe, encodedSafes),
      SEC_ASN1_SUB(SEC_AnyTemplate) }
};

const SEC_ASN1Template sec_PKCS12SafeBagTemplate[] = {
    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL,
      sizeof(sec_PKCS12SafeBag) },
    { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SafeBag, safeBagType) },
    { SEC_ASN1_EXPLICIT | SEC_ASN1_DYNAMIC | SEC_ASN1_CONSTRUCTED |
          SEC_ASN1_MAY_STREAM | SEC_ASN1_CONTEXT_SPECIFIC | 0,
      offsetof(sec_PKCS12SafeBag, safeBagContent),
      &sec_pkcs12_safe_bag_chooser },
    { SEC_ASN1_SET_OF | SEC_ASN1_OPTIONAL, offsetof(sec_PKCS12SafeBag, attribs),
      sec_PKCS12AttributeTemplate },
    { 0 }
};

const SEC_ASN1Template sec_PKCS12SafeContentsTemplate[] = {
    { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM,
      offsetof(sec_PKCS12SafeContents, safeBags),
      sec_PKCS12SafeBagTemplate }
};

const SEC_ASN1Template sec_PKCS12SequenceOfAnyTemplate[] = {
    { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN, 0,
      SEC_ASN1_SUB(SEC_AnyTemplate) }
};

const SEC_ASN1Template sec_PKCS12NestedSafeContentsDecodeTemplate[] = {
    { SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0,
      offsetof(sec_PKCS12SafeContents, encodedSafeBags),
      sec_PKCS12SequenceOfAnyTemplate }
};

const SEC_ASN1Template sec_PKCS12SafeContentsDecodeTemplate[] = {
    { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN,
      offsetof(sec_PKCS12SafeContents, encodedSafeBags),
      SEC_ASN1_SUB(SEC_AnyTemplate) }
};

const SEC_ASN1Template sec_PKCS12CRLBagTemplate[] = {
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CRLBag) },
    { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CRLBag, bagID) },
    { SEC_ASN1_DYNAMIC | SEC_ASN1_POINTER,
      offsetof(sec_PKCS12CRLBag, value), &sec_pkcs12_crl_bag_chooser },
    { 0 }
};

const SEC_ASN1Template sec_PKCS12CertBagTemplate[] = {
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CertBag) },
    { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CertBag, bagID) },
    { SEC_ASN1_DYNAMIC | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED |
          SEC_ASN1_CONTEXT_SPECIFIC | 0,
      offsetof(sec_PKCS12CertBag, value), &sec_pkcs12_cert_bag_chooser },
    { 0 }
};

const SEC_ASN1Template sec_PKCS12SecretBagTemplate[] = {
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12SecretBag) },
    { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SecretBag, secretType) },
    { SEC_ASN1_ANY, offsetof(sec_PKCS12SecretBag, secretContent) },
    { 0 }
};

const SEC_ASN1Template sec_PKCS12AttributeTemplate[] = {
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12Attribute) },
    { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12Attribute, attrType) },
    { SEC_ASN1_SET_OF | SEC_ASN1_DYNAMIC,
      offsetof(sec_PKCS12Attribute, attrValue),
      &sec_pkcs12_attr_chooser },
    { 0 }
};