]> git.saurik.com Git - apple/security.git/blob - AppleX509CL/CertBuilder.cpp
Security-30.1.tar.gz
[apple/security.git] / AppleX509CL / CertBuilder.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * CertBuilder.cpp - sublasses of various snacc-generated cert-related
21 * classes.
22 *
23 * Created 9/1/2000 by Doug Mitchell.
24 * Copyright (c) 2000 by Apple Computer.
25 */
26
27 #include "CertBuilder.h"
28 #include <Security/cssmerr.h>
29 #include <Security/utilities.h>
30
31 #define BUF_ENC_EXTRA 64
32
33 /*
34 * Name is a complex structure which boils down to an arbitrarily
35 * large array of (usually) printable names. We facilitate the
36 * construction of the array, one AttributeTypeAndDistinguishedValue
37 * per RelativeDistinguishedName. This is the format commonly used
38 * in the real world, though it's legal to have multiple ATDVs
39 * per RDN - we just don't do it here.
40 *
41 * Typically the object manipulated here is inserted into a
42 * CertificateToSign object, as issuer or subject.
43 */
44 void NameBuilder::addATDV(
45 const AsnOid &type, // id_at_commonName, etc. from sm_x501if
46 const char *value, // the bytes
47 size_t valueLen,
48 DirectoryString::ChoiceIdEnum stringType,
49 // printableStringCid, etc.
50 // from sm_x520sa
51 bool primaryDistinguished)
52 {
53 /* cook up the RDN sequence first time thru */
54 if(rDNSequence == NULL) {
55 rDNSequence = new RDNSequence;
56 choiceId = rDNSequenceCid; // no others available
57 }
58
59 /* one RelativeDistinguishedName and one ATDV */
60 RelativeDistinguishedName *rdn = rDNSequence->Append();
61 AttributeTypeAndDistinguishedValue *atdv = rdn->Append();
62
63 /*
64 * fill in the ATDV
65 * FIXME - AttributeTypeAndDistinguishedValueSetOf??? What's that?
66 */
67 atdv->type = type;
68 if(!primaryDistinguished) {
69 /* default is true, only encode if not default */
70 atdv->primaryDistinguished = new AsnBool(primaryDistinguished);
71 }
72
73 /* DirectoryString from sm_x520sa */
74 DirectoryString dirStr;
75 dirStr.choiceId = stringType;
76 switch(stringType) {
77 case DirectoryString::teletexStringCid:
78 dirStr.teletexString = new TeletexString(value, valueLen);
79 break;
80 case DirectoryString::printableStringCid:
81 dirStr.printableString = new PrintableString(value, valueLen);
82 break;
83 case DirectoryString::universalStringCid:
84 dirStr.universalString = new UniversalString(value, valueLen);
85 break;
86 case DirectoryString::bmpStringCid:
87 dirStr.bmpString = new BMPString(value, valueLen);
88 break;
89 case DirectoryString::utf8StringCid:
90 dirStr.utf8String = new UTF8String(value, valueLen);
91 break;
92 }
93
94 /*
95 * As far as I can tell, atdv->value.value is a CSM_Buffer containing
96 * the encoded dirStr. First malloc a dest buffer...
97 */
98 size_t bufLen = valueLen + BUF_ENC_EXTRA;
99 char *buf = (char *)calloc(1, bufLen);
100 if(buf == NULL) {
101 CssmError::throwMe(CSSMERR_CL_MEMORY_ERROR);
102 }
103
104 /* encode dirStr --> abuf */
105 AsnBuf abuf;
106 abuf.Init(buf, bufLen);
107 abuf.ResetInWriteRvsMode();
108 AsnLen bytesEnc;
109 dirStr.BEncPdu(abuf, bytesEnc);
110 if(bytesEnc > bufLen) {
111 #ifndef NDEBUG
112 printf("Whoops! Buffer overflow\n");
113 #endif
114 /* throw */
115 }
116
117 /* install the result into CSM_Buffer, which mallocs & copies */
118 atdv->value.value = new CSM_Buffer(abuf.DataPtr(), abuf.DataLen());
119 free(buf);
120 }
121
122 /*
123 * Custom AsnOid, used for converting CssmOid to AsnOid. The Snacc class
124 * declaration doesn't provide a means to construct from, or set by,
125 * pre-encoded OID bytes (which are available in a CssmOid).
126 */
127 OidBuilder::OidBuilder(const CSSM_OID &coid)
128 {
129 oid = Asn1Alloc (coid.Length);
130 memcpy(oid, coid.Data, coid.Length);
131 octetLen = coid.Length;
132 }
133