]> git.saurik.com Git - apple/security.git/blob - SecureTransport/sslutil.c
9118f89c16fd565428d1216676320c2db928ab4e
[apple/security.git] / SecureTransport / sslutil.c
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 File: sslutil.c
21
22 Contains: Misc. SSL utility functions
23
24 Written by: Doug Mitchell, based on Netscape SSLRef 3.0
25
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
27
28 */
29 /* *********************************************************************
30 File: sslutil.c
31
32 SSLRef 3.0 Final -- 11/19/96
33
34 Copyright (c)1996 by Netscape Communications Corp.
35
36 By retrieving this software you are bound by the licensing terms
37 disclosed in the file "LICENSE.txt". Please read it, and if you don't
38 accept the terms, delete this software.
39
40 SSLRef 3.0 was developed by Netscape Communications Corp. of Mountain
41 View, California <http://home.netscape.com/> and Consensus Development
42 Corporation of Berkeley, California <http://www.consensus.com/>.
43
44 *********************************************************************
45
46 File: sslutil.c Utility functions for encoding structures
47
48 Handles encoding endian-independant wire representation of 2, 3, or 4
49 byte integers.
50
51 ****************************************************************** */
52
53 #ifndef _SSLCTX_H_
54 #include "sslctx.h"
55 #endif
56
57 #ifndef _SSLUTIL_H_
58 #include "sslutil.h"
59 #endif
60
61 #ifndef _SSLALLOC_H_
62 #include "sslalloc.h"
63 #endif
64
65 #ifndef _SSL_DEBUG_H_
66 #include "sslDebug.h"
67 #endif
68
69 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
70
71 UInt32
72 SSLDecodeInt(const unsigned char *p, int length)
73 { UInt32 val = 0;
74 while (length--)
75 val = (val << 8) | *p++;
76 return val;
77 }
78
79 unsigned char *
80 SSLEncodeInt(unsigned char *p, UInt32 value, int length)
81 { unsigned char *retVal = p + length; /* Return pointer to char after int */
82 CASSERT(length > 0 && length <= 4);
83 while (length--) /* Assemble backwards */
84 { p[length] = (UInt8)value; /* Implicit masking to low byte */
85 value >>= 8;
86 }
87 return retVal;
88 }
89
90 UInt8*
91 SSLEncodeUInt64(UInt8 *p, sslUint64 value)
92 { p = SSLEncodeInt(p, value.high, 4);
93 return SSLEncodeInt(p, value.low, 4);
94 }
95
96
97 void
98 IncrementUInt64(sslUint64 *v)
99 { if (++v->low == 0) /* Must have just rolled over */
100 ++v->high;
101 }
102
103 UInt32
104 SSLGetCertificateChainLength(const SSLCertificate *c)
105 {
106 UInt32 rtn = 0;
107
108 while (c)
109 {
110 rtn++;
111 c = c->next;
112 }
113 return rtn;
114 }
115
116 Boolean sslIsSessionActive(const SSLContext *ctx)
117 {
118 CASSERT(ctx != NULL);
119 switch(ctx->state) {
120 case SSLUninitialized:
121 case HandshakeServerUninit:
122 case HandshakeClientUninit:
123 case SSLGracefulClose:
124 case SSLErrorClose:
125 return false;
126 default:
127 return true;
128 }
129 }
130
131 OSStatus sslDeleteCertificateChain(
132 SSLCertificate *certs,
133 SSLContext *ctx)
134 {
135 SSLCertificate *cert;
136 SSLCertificate *nextCert;
137
138 CASSERT(ctx != NULL);
139 cert=certs;
140 while(cert != NULL) {
141 nextCert = cert->next;
142 SSLFreeBuffer(&cert->derCert, &ctx->sysCtx);
143 sslFree(cert);
144 cert = nextCert;
145 }
146 return noErr;
147 }
148
149 #if SSL_DEBUG
150
151 const char *protocolVersStr(SSLProtocolVersion prot)
152 {
153 switch(prot) {
154 case SSL_Version_Undetermined: return "SSL_Version_Undetermined";
155 case SSL_Version_3_0_With_2_0_Hello: return "SSL_Version_3_0_With_2_0_Hello";
156 case SSL_Version_3_0_Only: return "SSL_Version_3_0_Only";
157 case SSL_Version_2_0: return "SSL_Version_2_0";
158 case SSL_Version_3_0: return "SSL_Version_3_0";
159 case TLS_Version_1_0: return "TLS_Version_1_0";
160 case TLS_Version_1_0_Only: return "TLS_Version_1_0_Only";
161 default: sslPanic("protocolVersStr: bad prot");
162 }
163 return NULL; /* NOT REACHED */
164 }
165
166 #endif /* SSL_DEBUG */