]>
git.saurik.com Git - apple/security.git/blob - SecureTransport/sslBER.cpp
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.
22 Contains: BER routines
24 Written by: Doug Mitchell
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
35 #include <Security/asn-incl.h>
36 #include <Security/sm_vdatypes.h>
37 #include <Security/asn-type.h>
38 #include <Security/pkcs1oids.h> /* for RSAPublicKey */
39 #include <Security/cdsaUtils.h>
41 #include <Security/cssmdata.h>
43 /* convert between SSLBuffer and snacc-style BigIntegerStr */
45 static void snaccIntToData(
46 const BigIntegerStr
&snaccInt
,
47 SSLBuffer
*outData
) // already mallocd
49 const char *scp
= snaccInt
;
50 uint8
*cp
= (uint8
*)scp
;
51 uint32 len
= snaccInt
.Len();
54 /* skip over this place-holding m.s. byte */
59 memmove(outData
->data
, cp
, len
);
60 outData
->length
= len
;
63 static void dataToSnaccInt(
64 const SSLBuffer
*inData
,
65 BigIntegerStr
&snaccInt
)
70 if (inData
->data
[0] & 0x80) {
71 /* m.s. bit of BER data must be zero! */
72 cp
= (uint8
*)malloc(inData
->length
+ 1);
74 memmove(cp
+1, inData
->data
, inData
->length
);
80 snaccInt
.Set(reinterpret_cast<const char *>(cp
),
81 inData
->length
+ msbIsSet
);
88 * Given a PKCS-1 encoded RSA public key, extract the
89 * modulus and public exponent.
91 * RSAPublicKey ::= SEQUENCE {
92 * modulus INTEGER, -- n
93 * publicExponent INTEGER -- e }
96 SSLErr
sslDecodeRsaBlob(
97 const SSLBuffer
*blob
, /* PKCS-1 encoded */
98 SSLBuffer
*modulus
, /* data mallocd and RETURNED */
99 SSLBuffer
*exponent
) /* data mallocd and RETURNED */
103 CASSERT(blob
!= NULL
);
104 CASSERT(modulus
!= NULL
);
105 CASSERT(exponent
!= NULL
);
107 /* DER-decode the blob */
108 RSAPublicKey snaccPubKey
;
109 CssmData
cssmBlob(blob
->data
, blob
->length
);
111 SC_decodeAsnObj(cssmBlob
, snaccPubKey
);
117 /* malloc & convert components */
118 srtn
= SSLAllocBuffer(modulus
, snaccPubKey
.modulus
.Len(), NULL
);
122 snaccIntToData(snaccPubKey
.modulus
, modulus
);
123 srtn
= SSLAllocBuffer(exponent
, snaccPubKey
.publicExponent
.Len(),
128 snaccIntToData(snaccPubKey
.publicExponent
, exponent
);
133 * Given a raw modulus and exponent, cook up a
134 * BER-encoded RSA public key blob.
136 SSLErr
sslEncodeRsaBlob(
137 const SSLBuffer
*modulus
,
138 const SSLBuffer
*exponent
,
139 SSLBuffer
*blob
) /* data mallocd and RETURNED */
141 CASSERT((modulus
!= NULL
) && (exponent
!= NULL
));
145 /* Cook up a snacc-style RSAPublic key */
146 RSAPublicKey snaccPubKey
;
147 dataToSnaccInt(modulus
, snaccPubKey
.modulus
);
148 dataToSnaccInt(exponent
, snaccPubKey
.publicExponent
);
150 /* estimate max size, BER-encode */
151 size_t maxSize
= 2 * (modulus
->length
+ exponent
->length
);
152 CssmAllocator
&alloc
= CssmAllocator::standard();
153 CssmAutoData
cblob(alloc
);
155 SC_encodeAsnObj(snaccPubKey
, cblob
, maxSize
);
162 /* copy to caller's SSLBuffer */
163 SSLErr srtn
= SSLAllocBuffer(blob
, cblob
.length(), NULL
);
167 memmove(blob
->data
, cblob
.data(), cblob
.length());