2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 * CertBuilder.cpp - sublasses of various snacc-generated cert-related
23 * Created 9/1/2000 by Doug Mitchell.
24 * Copyright (c) 2000 by Apple Computer.
27 #include "CertBuilder.h"
28 #include <Security/cssmerr.h>
29 #include <Security/utilities.h>
31 #define BUF_ENC_EXTRA 64
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.
41 * Typically the object manipulated here is inserted into a
42 * CertificateToSign object, as issuer or subject.
44 void NameBuilder::addATDV(
45 const AsnOid
&type
, // id_at_commonName, etc. from sm_x501if
46 const char *value
, // the bytes
48 DirectoryString::ChoiceIdEnum stringType
,
49 // printableStringCid, etc.
51 bool primaryDistinguished
)
53 /* cook up the RDN sequence first time thru */
54 if(rDNSequence
== NULL
) {
55 rDNSequence
= new RDNSequence
;
56 choiceId
= rDNSequenceCid
; // no others available
59 /* one RelativeDistinguishedName and one ATDV */
60 RelativeDistinguishedName
*rdn
= rDNSequence
->Append();
61 AttributeTypeAndDistinguishedValue
*atdv
= rdn
->Append();
65 * FIXME - AttributeTypeAndDistinguishedValueSetOf??? What's that?
68 if(!primaryDistinguished
) {
69 /* default is true, only encode if not default */
70 atdv
->primaryDistinguished
= new AsnBool(primaryDistinguished
);
73 /* DirectoryString from sm_x520sa */
74 DirectoryString dirStr
;
75 dirStr
.choiceId
= stringType
;
77 case DirectoryString::teletexStringCid
:
78 dirStr
.teletexString
= new TeletexString(value
, valueLen
);
80 case DirectoryString::printableStringCid
:
81 dirStr
.printableString
= new PrintableString(value
, valueLen
);
83 case DirectoryString::universalStringCid
:
84 dirStr
.universalString
= new UniversalString(value
, valueLen
);
86 case DirectoryString::bmpStringCid
:
87 dirStr
.bmpString
= new BMPString(value
, valueLen
);
89 case DirectoryString::utf8StringCid
:
90 dirStr
.utf8String
= new UTF8String(value
, valueLen
);
95 * As far as I can tell, atdv->value.value is a CSM_Buffer containing
96 * the encoded dirStr. First malloc a dest buffer...
98 size_t bufLen
= valueLen
+ BUF_ENC_EXTRA
;
99 char *buf
= (char *)calloc(1, bufLen
);
101 CssmError::throwMe(CSSMERR_CL_MEMORY_ERROR
);
104 /* encode dirStr --> abuf */
106 abuf
.Init(buf
, bufLen
);
107 abuf
.ResetInWriteRvsMode();
109 dirStr
.BEncPdu(abuf
, bytesEnc
);
110 if(bytesEnc
> bufLen
) {
112 printf("Whoops! Buffer overflow\n");
117 /* install the result into CSM_Buffer, which mallocs & copies */
118 atdv
->value
.value
= new CSM_Buffer(abuf
.DataPtr(), abuf
.DataLen());
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).
127 OidBuilder::OidBuilder(const CSSM_OID
&coid
)
129 oid
= Asn1Alloc (coid
.Length
);
130 memcpy(oid
, coid
.Data
, coid
.Length
);
131 octetLen
= coid
.Length
;