]> git.saurik.com Git - apple/security.git/blob - SecureTransport/sslutil.c
Security-30.1.tar.gz
[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 void
91 IncrementUInt64(sslUint64 *v)
92 { if (++v->low == 0) /* Must have just rolled over */
93 ++v->high;
94 }
95
96 UInt32
97 SSLGetCertificateChainLength(const SSLCertificate *c)
98 {
99 UInt32 rtn = 0;
100
101 while (c)
102 {
103 rtn++;
104 c = c->next;
105 }
106 return rtn;
107 }
108
109 Boolean sslIsSessionActive(const SSLContext *ctx)
110 {
111 CASSERT(ctx != NULL);
112 switch(ctx->state) {
113 case SSLUninitialized:
114 case HandshakeServerUninit:
115 case HandshakeClientUninit:
116 case SSLGracefulClose:
117 case SSLErrorClose:
118 return false;
119 default:
120 return true;
121 }
122 }
123
124 OSStatus sslDeleteCertificateChain(
125 SSLCertificate *certs,
126 SSLContext *ctx)
127 {
128 SSLCertificate *cert;
129 SSLCertificate *nextCert;
130
131 CASSERT(ctx != NULL);
132 cert=certs;
133 while(cert != NULL) {
134 nextCert = cert->next;
135 SSLFreeBuffer(&cert->derCert, &ctx->sysCtx);
136 sslFree(cert);
137 cert = nextCert;
138 }
139 return noErr;
140 }
141
142 #if SSL_DEBUG
143
144 const char *protocolVersStr(SSLProtocolVersion prot)
145 {
146 switch(prot) {
147 case SSL_Version_Undetermined: return "SSL_Version_Undetermined";
148 case SSL_Version_3_0_With_2_0_Hello: return "SSL_Version_3_0_With_2_0_Hello";
149 case SSL_Version_3_0_Only: return "SSL_Version_3_0_Only";
150 case SSL_Version_2_0: return "SSL_Version_2_0";
151 case SSL_Version_3_0: return "SSL_Version_3_0";
152 default: sslPanic("protocolVersStr: bad prot");
153 }
154 return NULL; /* NOT REACHED */
155 }
156
157 #endif /* SSL_DEBUG */