| OLD | NEW |
| 1 /* | 1 /* |
| 2 * "Default" SSLSocket methods, used by sockets that do neither SSL nor socks. | 2 * "Default" SSLSocket methods, used by sockets that do neither SSL nor socks. |
| 3 * | 3 * |
| 4 * This Source Code Form is subject to the terms of the Mozilla Public | 4 * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 * License, v. 2.0. If a copy of the MPL was not distributed with this | 5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 7 | 7 |
| 8 #include "cert.h" | 8 #include "cert.h" |
| 9 #include "ssl.h" | 9 #include "ssl.h" |
| 10 #include "sslimpl.h" | 10 #include "sslimpl.h" |
| 11 | 11 |
| 12 #if defined(WIN32) | 12 #if defined(WIN32) |
| 13 #define MAP_ERROR(from,to) if (err == from) { PORT_SetError(to); } | 13 #define MAP_ERROR(from, to) \ |
| 14 #define DEFINE_ERROR PRErrorCode err = PR_GetError(); | 14 if (err == from) { \ |
| 15 PORT_SetError(to); \ |
| 16 } |
| 17 #define DEFINE_ERROR PRErrorCode err = PR_GetError(); |
| 15 #else | 18 #else |
| 16 #define MAP_ERROR(from,to) | 19 #define MAP_ERROR(from, to) |
| 17 #define DEFINE_ERROR | 20 #define DEFINE_ERROR |
| 18 #endif | 21 #endif |
| 19 | 22 |
| 20 int ssl_DefConnect(sslSocket *ss, const PRNetAddr *sa) | 23 int |
| 24 ssl_DefConnect(sslSocket *ss, const PRNetAddr *sa) |
| 21 { | 25 { |
| 22 PRFileDesc *lower = ss->fd->lower; | 26 PRFileDesc *lower = ss->fd->lower; |
| 23 int rv; | 27 int rv; |
| 24 | 28 |
| 25 rv = lower->methods->connect(lower, sa, ss->cTimeout); | 29 rv = lower->methods->connect(lower, sa, ss->cTimeout); |
| 26 return rv; | 30 return rv; |
| 27 } | 31 } |
| 28 | 32 |
| 29 int ssl_DefBind(sslSocket *ss, const PRNetAddr *addr) | 33 int |
| 34 ssl_DefBind(sslSocket *ss, const PRNetAddr *addr) |
| 30 { | 35 { |
| 31 PRFileDesc *lower = ss->fd->lower; | 36 PRFileDesc *lower = ss->fd->lower; |
| 32 int rv; | 37 int rv; |
| 33 | 38 |
| 34 rv = lower->methods->bind(lower, addr); | 39 rv = lower->methods->bind(lower, addr); |
| 35 return rv; | 40 return rv; |
| 36 } | 41 } |
| 37 | 42 |
| 38 int ssl_DefListen(sslSocket *ss, int backlog) | 43 int |
| 44 ssl_DefListen(sslSocket *ss, int backlog) |
| 39 { | 45 { |
| 40 PRFileDesc *lower = ss->fd->lower; | 46 PRFileDesc *lower = ss->fd->lower; |
| 41 int rv; | 47 int rv; |
| 42 | 48 |
| 43 rv = lower->methods->listen(lower, backlog); | 49 rv = lower->methods->listen(lower, backlog); |
| 44 return rv; | 50 return rv; |
| 45 } | 51 } |
| 46 | 52 |
| 47 int ssl_DefShutdown(sslSocket *ss, int how) | 53 int |
| 54 ssl_DefShutdown(sslSocket *ss, int how) |
| 48 { | 55 { |
| 49 PRFileDesc *lower = ss->fd->lower; | 56 PRFileDesc *lower = ss->fd->lower; |
| 50 int rv; | 57 int rv; |
| 51 | 58 |
| 52 rv = lower->methods->shutdown(lower, how); | 59 rv = lower->methods->shutdown(lower, how); |
| 53 return rv; | 60 return rv; |
| 54 } | 61 } |
| 55 | 62 |
| 56 int ssl_DefRecv(sslSocket *ss, unsigned char *buf, int len, int flags) | 63 int |
| 64 ssl_DefRecv(sslSocket *ss, unsigned char *buf, int len, int flags) |
| 57 { | 65 { |
| 58 PRFileDesc *lower = ss->fd->lower; | 66 PRFileDesc *lower = ss->fd->lower; |
| 59 int rv; | 67 int rv; |
| 60 | 68 |
| 61 rv = lower->methods->recv(lower, (void *)buf, len, flags, ss->rTimeout); | 69 rv = lower->methods->recv(lower, (void *)buf, len, flags, ss->rTimeout); |
| 62 if (rv < 0) { | 70 if (rv < 0) { |
| 63 » DEFINE_ERROR | 71 DEFINE_ERROR |
| 64 » MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR) | 72 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR) |
| 65 } else if (rv > len) { | 73 } else if (rv > len) { |
| 66 » PORT_Assert(rv <= len); | 74 PORT_Assert(rv <= len); |
| 67 » PORT_SetError(PR_BUFFER_OVERFLOW_ERROR); | 75 PORT_SetError(PR_BUFFER_OVERFLOW_ERROR); |
| 68 » rv = SECFailure; | 76 rv = SECFailure; |
| 69 } | 77 } |
| 70 return rv; | 78 return rv; |
| 71 } | 79 } |
| 72 | 80 |
| 73 /* Default (unencrypted) send. | 81 /* Default (unencrypted) send. |
| 74 * For blocking sockets, always returns len or SECFailure, no short writes. | 82 * For blocking sockets, always returns len or SECFailure, no short writes. |
| 75 * For non-blocking sockets: | 83 * For non-blocking sockets: |
| 76 * Returns positive count if any data was written, else returns SECFailure. | 84 * Returns positive count if any data was written, else returns SECFailure. |
| 77 * Short writes may occur. Does not return SECWouldBlock. | 85 * Short writes may occur. Does not return SECWouldBlock. |
| 78 */ | 86 */ |
| 79 int ssl_DefSend(sslSocket *ss, const unsigned char *buf, int len, int flags) | 87 int |
| 88 ssl_DefSend(sslSocket *ss, const unsigned char *buf, int len, int flags) |
| 80 { | 89 { |
| 81 PRFileDesc *lower = ss->fd->lower; | 90 PRFileDesc *lower = ss->fd->lower; |
| 82 int sent = 0; | 91 int sent = 0; |
| 83 | 92 |
| 84 #if NSS_DISABLE_NAGLE_DELAYS | 93 #if NSS_DISABLE_NAGLE_DELAYS |
| 85 /* Although this is overkill, we disable Nagle delays completely for | 94 /* Although this is overkill, we disable Nagle delays completely for |
| 86 ** SSL sockets. | 95 ** SSL sockets. |
| 87 */ | 96 */ |
| 88 if (ss->opt.useSecurity && !ss->delayDisabled) { | 97 if (ss->opt.useSecurity && !ss->delayDisabled) { |
| 89 » ssl_EnableNagleDelay(ss, PR_FALSE); /* ignore error */ | 98 ssl_EnableNagleDelay(ss, PR_FALSE); /* ignore error */ |
| 90 » ss->delayDisabled = 1; | 99 ss->delayDisabled = 1; |
| 91 } | 100 } |
| 92 #endif | 101 #endif |
| 93 do { | 102 do { |
| 94 » int rv = lower->methods->send(lower, (const void *)(buf + sent), | 103 int rv = lower->methods->send(lower, (const void *)(buf + sent), |
| 95 » len - sent, flags, ss->wTimeout); | 104 len - sent, flags, ss->wTimeout); |
| 96 » if (rv < 0) { | 105 if (rv < 0) { |
| 97 » PRErrorCode err = PR_GetError(); | 106 PRErrorCode err = PR_GetError(); |
| 98 » if (err == PR_WOULD_BLOCK_ERROR) { | 107 if (err == PR_WOULD_BLOCK_ERROR) { |
| 99 » » ss->lastWriteBlocked = 1; | 108 ss->lastWriteBlocked = 1; |
| 100 » » return sent ? sent : SECFailure; | 109 return sent ? sent : SECFailure; |
| 101 » } | 110 } |
| 102 » ss->lastWriteBlocked = 0; | 111 ss->lastWriteBlocked = 0; |
| 103 » MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR) | 112 MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR) |
| 104 » /* Loser */ | 113 /* Loser */ |
| 105 » return rv; | 114 return rv; |
| 106 » } | 115 } |
| 107 » sent += rv; | 116 sent += rv; |
| 108 » | 117 |
| 109 » if (IS_DTLS(ss) && (len > sent)) { | 118 if (IS_DTLS(ss) && (len > sent)) { |
| 110 » /* We got a partial write so just return it */ | 119 /* We got a partial write so just return it */ |
| 111 » return sent; | 120 return sent; |
| 112 » } | 121 } |
| 113 } while (len > sent); | 122 } while (len > sent); |
| 114 ss->lastWriteBlocked = 0; | 123 ss->lastWriteBlocked = 0; |
| 115 return sent; | 124 return sent; |
| 116 } | 125 } |
| 117 | 126 |
| 118 int ssl_DefRead(sslSocket *ss, unsigned char *buf, int len) | 127 int |
| 128 ssl_DefRead(sslSocket *ss, unsigned char *buf, int len) |
| 119 { | 129 { |
| 120 PRFileDesc *lower = ss->fd->lower; | 130 PRFileDesc *lower = ss->fd->lower; |
| 121 int rv; | 131 int rv; |
| 122 | 132 |
| 123 rv = lower->methods->read(lower, (void *)buf, len); | 133 rv = lower->methods->read(lower, (void *)buf, len); |
| 124 if (rv < 0) { | 134 if (rv < 0) { |
| 125 » DEFINE_ERROR | 135 DEFINE_ERROR |
| 126 » MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR) | 136 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR) |
| 127 } | 137 } |
| 128 return rv; | 138 return rv; |
| 129 } | 139 } |
| 130 | 140 |
| 131 int ssl_DefWrite(sslSocket *ss, const unsigned char *buf, int len) | 141 int |
| 142 ssl_DefWrite(sslSocket *ss, const unsigned char *buf, int len) |
| 132 { | 143 { |
| 133 PRFileDesc *lower = ss->fd->lower; | 144 PRFileDesc *lower = ss->fd->lower; |
| 134 int sent = 0; | 145 int sent = 0; |
| 135 | 146 |
| 136 do { | 147 do { |
| 137 » int rv = lower->methods->write(lower, (const void *)(buf + sent), | 148 int rv = lower->methods->write(lower, (const void *)(buf + sent), |
| 138 » len - sent); | 149 len - sent); |
| 139 » if (rv < 0) { | 150 if (rv < 0) { |
| 140 » PRErrorCode err = PR_GetError(); | 151 PRErrorCode err = PR_GetError(); |
| 141 » if (err == PR_WOULD_BLOCK_ERROR) { | 152 if (err == PR_WOULD_BLOCK_ERROR) { |
| 142 » » ss->lastWriteBlocked = 1; | 153 ss->lastWriteBlocked = 1; |
| 143 » » return sent ? sent : SECFailure; | 154 return sent ? sent : SECFailure; |
| 144 » } | 155 } |
| 145 » ss->lastWriteBlocked = 0; | 156 ss->lastWriteBlocked = 0; |
| 146 » MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR) | 157 MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR) |
| 147 » /* Loser */ | 158 /* Loser */ |
| 148 » return rv; | 159 return rv; |
| 149 » } | 160 } |
| 150 » sent += rv; | 161 sent += rv; |
| 151 } while (len > sent); | 162 } while (len > sent); |
| 152 ss->lastWriteBlocked = 0; | 163 ss->lastWriteBlocked = 0; |
| 153 return sent; | 164 return sent; |
| 154 } | 165 } |
| 155 | 166 |
| 156 int ssl_DefGetpeername(sslSocket *ss, PRNetAddr *name) | 167 int |
| 168 ssl_DefGetpeername(sslSocket *ss, PRNetAddr *name) |
| 157 { | 169 { |
| 158 PRFileDesc *lower = ss->fd->lower; | 170 PRFileDesc *lower = ss->fd->lower; |
| 159 int rv; | 171 int rv; |
| 160 | 172 |
| 161 rv = lower->methods->getpeername(lower, name); | 173 rv = lower->methods->getpeername(lower, name); |
| 162 return rv; | 174 return rv; |
| 163 } | 175 } |
| 164 | 176 |
| 165 int ssl_DefGetsockname(sslSocket *ss, PRNetAddr *name) | 177 int |
| 178 ssl_DefGetsockname(sslSocket *ss, PRNetAddr *name) |
| 166 { | 179 { |
| 167 PRFileDesc *lower = ss->fd->lower; | 180 PRFileDesc *lower = ss->fd->lower; |
| 168 int rv; | 181 int rv; |
| 169 | 182 |
| 170 rv = lower->methods->getsockname(lower, name); | 183 rv = lower->methods->getsockname(lower, name); |
| 171 return rv; | 184 return rv; |
| 172 } | 185 } |
| 173 | 186 |
| 174 int ssl_DefClose(sslSocket *ss) | 187 int |
| 188 ssl_DefClose(sslSocket *ss) |
| 175 { | 189 { |
| 176 PRFileDesc *fd; | 190 PRFileDesc *fd; |
| 177 PRFileDesc *popped; | 191 PRFileDesc *popped; |
| 178 int rv; | 192 int rv; |
| 179 | 193 |
| 180 fd = ss->fd; | 194 fd = ss->fd; |
| 181 | 195 |
| 182 /* First, remove the SSL layer PRFileDesc from the socket's stack, | 196 /* First, remove the SSL layer PRFileDesc from the socket's stack, |
| 183 ** then invoke the SSL layer's PRFileDesc destructor. | 197 ** then invoke the SSL layer's PRFileDesc destructor. |
| 184 ** This must happen before the next layer down is closed. | 198 ** This must happen before the next layer down is closed. |
| 185 */ | 199 */ |
| 186 PORT_Assert(fd->higher == NULL); | 200 PORT_Assert(fd->higher == NULL); |
| 187 if (fd->higher) { | 201 if (fd->higher) { |
| 188 » PORT_SetError(PR_BAD_DESCRIPTOR_ERROR); | 202 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR); |
| 189 » return SECFailure; | 203 return SECFailure; |
| 190 } | 204 } |
| 191 ss->fd = NULL; | 205 ss->fd = NULL; |
| 192 | 206 |
| 193 /* PR_PopIOLayer will swap the contents of the top two PRFileDescs on | 207 /* PR_PopIOLayer will swap the contents of the top two PRFileDescs on |
| 194 ** the stack, and then remove the second one. This way, the address | 208 ** the stack, and then remove the second one. This way, the address |
| 195 ** of the PRFileDesc on the top of the stack doesn't change. | 209 ** of the PRFileDesc on the top of the stack doesn't change. |
| 196 */ | 210 */ |
| 197 popped = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); | 211 popped = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); |
| 198 popped->dtor(popped); | 212 popped->dtor(popped); |
| 199 | 213 |
| 200 /* fd is now the PRFileDesc for the next layer down. | 214 /* fd is now the PRFileDesc for the next layer down. |
| 201 ** Now close the underlying socket. | 215 ** Now close the underlying socket. |
| 202 */ | 216 */ |
| 203 rv = fd->methods->close(fd); | 217 rv = fd->methods->close(fd); |
| 204 | 218 |
| 205 ssl_FreeSocket(ss); | 219 ssl_FreeSocket(ss); |
| 206 | 220 |
| 207 SSL_TRC(5, ("%d: SSL[%d]: closing, rv=%d errno=%d", | 221 SSL_TRC(5, ("%d: SSL[%d]: closing, rv=%d errno=%d", |
| 208 » » SSL_GETPID(), fd, rv, PORT_GetError())); | 222 SSL_GETPID(), fd, rv, PORT_GetError())); |
| 209 return rv; | 223 return rv; |
| 210 } | 224 } |
| OLD | NEW |