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: Misc. SSL utility functions
24 Written by: Doug Mitchell
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
30 #include "sslContext.h"
32 #include "sslMemory.h"
34 #include <Security/devrandom.h>
36 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
40 SSLDecodeInt(const unsigned char *p
, int length
)
43 val
= (val
<< 8) | *p
++;
48 SSLEncodeInt(unsigned char *p
, UInt32 value
, int length
)
49 { unsigned char *retVal
= p
+ length
; /* Return pointer to char after int */
50 assert(length
> 0 && length
<= 4);
51 while (length
--) /* Assemble backwards */
52 { p
[length
] = (UInt8
)value
; /* Implicit masking to low byte */
59 SSLEncodeUInt64(UInt8
*p
, sslUint64 value
)
60 { p
= SSLEncodeInt(p
, value
.high
, 4);
61 return SSLEncodeInt(p
, value
.low
, 4);
66 IncrementUInt64(sslUint64
*v
)
67 { if (++v
->low
== 0) /* Must have just rolled over */
72 SSLGetCertificateChainLength(const SSLCertificate
*c
)
84 Boolean
sslIsSessionActive(const SSLContext
*ctx
)
88 case SSL_HdskStateUninit
:
89 case SSL_HdskStateServerUninit
:
90 case SSL_HdskStateClientUninit
:
91 case SSL_HdskStateGracefulClose
:
92 case SSL_HdskStateErrorClose
:
99 OSStatus
sslDeleteCertificateChain(
100 SSLCertificate
*certs
,
103 SSLCertificate
*cert
;
104 SSLCertificate
*nextCert
;
108 while(cert
!= NULL
) {
109 nextCert
= cert
->next
;
110 SSLFreeBuffer(cert
->derCert
, ctx
);
119 const char *protocolVersStr(SSLProtocolVersion prot
)
122 case SSL_Version_Undetermined
: return "SSL_Version_Undetermined";
123 case SSL_Version_3_0_With_2_0_Hello
: return "SSL_Version_3_0_With_2_0_Hello";
124 case SSL_Version_3_0_Only
: return "SSL_Version_3_0_Only";
125 case SSL_Version_2_0
: return "SSL_Version_2_0";
126 case SSL_Version_3_0
: return "SSL_Version_3_0";
127 case TLS_Version_1_0
: return "TLS_Version_1_0";
128 case TLS_Version_1_0_Only
: return "TLS_Version_1_0_Only";
129 default: sslErrorLog("protocolVersStr: bad prot\n"); return "BAD PROTOCOL";
131 return NULL
; /* NOT REACHED */
134 #endif /* SSL_DEBUG */
137 * Redirect SSLBuffer-based I/O call to user-supplied I/O.
141 size_t *actualLength
,
144 UInt32 dataLength
= buf
.length
;
148 ortn
= (ctx
->ioCtx
.read
)(ctx
->ioCtx
.ioRef
,
151 *actualLength
= dataLength
;
157 size_t *actualLength
,
160 UInt32 dataLength
= buf
.length
;
164 ortn
= (ctx
->ioCtx
.write
)(ctx
->ioCtx
.ioRef
,
167 *actualLength
= dataLength
;
171 OSStatus
sslTime(UInt32
*tim
)
180 * Common RNG function.
182 OSStatus
sslRand(SSLContext
*ctx
, SSLBuffer
*buf
)
184 OSStatus serr
= noErr
;
188 assert(buf
->data
!= NULL
);
190 if(buf
->length
== 0) {
191 sslErrorLog("sslRand: zero buf->length\n");
195 Security::DevRandomGenerator
devRand(false);
196 devRand
.random(buf
->data
, buf
->length
);