1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
|
/*
* libjingle
* Copyright 2004--2008, Google Inc.
* Copyright 2004--2011, RTFM, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <vector>
#if HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
#if HAVE_NSS_SSL_H
#include "talk/base/nssstreamadapter.h"
#include "keyhi.h"
#include "nspr.h"
#include "nss.h"
#include "pk11pub.h"
#include "secerr.h"
#ifdef NSS_SSL_RELATIVE_PATH
#include "ssl.h"
#include "sslerr.h"
#include "sslproto.h"
#else
#include "net/third_party/nss/ssl/ssl.h"
#include "net/third_party/nss/ssl/sslerr.h"
#include "net/third_party/nss/ssl/sslproto.h"
#endif
#include "talk/base/nssidentity.h"
#include "talk/base/thread.h"
namespace talk_base {
PRDescIdentity NSSStreamAdapter::nspr_layer_identity = PR_INVALID_IO_LAYER;
#define UNIMPLEMENTED \
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); \
LOG(LS_ERROR) \
<< "Call to unimplemented function "<< __FUNCTION__; ASSERT(false)
#ifdef SRTP_AES128_CM_HMAC_SHA1_80
#define HAVE_DTLS_SRTP
#endif
#ifdef HAVE_DTLS_SRTP
// SRTP cipher suite table
struct SrtpCipherMapEntry {
const char* external_name;
PRUint16 cipher_id;
};
// This isn't elegant, but it's better than an external reference
static const SrtpCipherMapEntry kSrtpCipherMap[] = {
{"AES_CM_128_HMAC_SHA1_80", SRTP_AES128_CM_HMAC_SHA1_80 },
{"AES_CM_128_HMAC_SHA1_32", SRTP_AES128_CM_HMAC_SHA1_32 },
{NULL, 0}
};
#endif
// Implementation of NSPR methods
static PRStatus StreamClose(PRFileDesc *socket) {
// Noop
return PR_SUCCESS;
}
static PRInt32 StreamRead(PRFileDesc *socket, void *buf, PRInt32 length) {
StreamInterface *stream = reinterpret_cast<StreamInterface *>(socket->secret);
size_t read;
int error;
StreamResult result = stream->Read(buf, length, &read, &error);
if (result == SR_SUCCESS) {
return read;
}
if (result == SR_EOS) {
return 0;
}
if (result == SR_BLOCK) {
PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
return -1;
}
PR_SetError(PR_UNKNOWN_ERROR, error);
return -1;
}
static PRInt32 StreamWrite(PRFileDesc *socket, const void *buf,
PRInt32 length) {
StreamInterface *stream = reinterpret_cast<StreamInterface *>(socket->secret);
size_t written;
int error;
StreamResult result = stream->Write(buf, length, &written, &error);
if (result == SR_SUCCESS) {
return written;
}
if (result == SR_BLOCK) {
LOG(LS_INFO) <<
"NSSStreamAdapter: write to underlying transport would block";
PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
return -1;
}
LOG(LS_ERROR) << "Write error";
PR_SetError(PR_UNKNOWN_ERROR, error);
return -1;
}
static PRInt32 StreamAvailable(PRFileDesc *socket) {
UNIMPLEMENTED;
return -1;
}
PRInt64 StreamAvailable64(PRFileDesc *socket) {
UNIMPLEMENTED;
return -1;
}
static PRStatus StreamSync(PRFileDesc *socket) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PROffset32 StreamSeek(PRFileDesc *socket, PROffset32 offset,
PRSeekWhence how) {
UNIMPLEMENTED;
return -1;
}
static PROffset64 StreamSeek64(PRFileDesc *socket, PROffset64 offset,
PRSeekWhence how) {
UNIMPLEMENTED;
return -1;
}
static PRStatus StreamFileInfo(PRFileDesc *socket, PRFileInfo *info) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PRStatus StreamFileInfo64(PRFileDesc *socket, PRFileInfo64 *info) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PRInt32 StreamWritev(PRFileDesc *socket, const PRIOVec *iov,
PRInt32 iov_size, PRIntervalTime timeout) {
UNIMPLEMENTED;
return -1;
}
static PRStatus StreamConnect(PRFileDesc *socket, const PRNetAddr *addr,
PRIntervalTime timeout) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PRFileDesc *StreamAccept(PRFileDesc *sd, PRNetAddr *addr,
PRIntervalTime timeout) {
UNIMPLEMENTED;
return NULL;
}
static PRStatus StreamBind(PRFileDesc *socket, const PRNetAddr *addr) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PRStatus StreamListen(PRFileDesc *socket, PRIntn depth) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PRStatus StreamShutdown(PRFileDesc *socket, PRIntn how) {
UNIMPLEMENTED;
return PR_FAILURE;
}
// Note: this is always nonblocking and ignores the timeout.
// TODO(ekr@rtfm.com): In future verify that the socket is
// actually in non-blocking mode.
// This function does not support peek.
static PRInt32 StreamRecv(PRFileDesc *socket, void *buf, PRInt32 amount,
PRIntn flags, PRIntervalTime to) {
ASSERT(flags == 0);
if (flags != 0) {
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return -1;
}
return StreamRead(socket, buf, amount);
}
// Note: this is always nonblocking and assumes a zero timeout.
// This function does not support peek.
static PRInt32 StreamSend(PRFileDesc *socket, const void *buf,
PRInt32 amount, PRIntn flags,
PRIntervalTime to) {
ASSERT(flags == 0);
return StreamWrite(socket, buf, amount);
}
static PRInt32 StreamRecvfrom(PRFileDesc *socket, void *buf,
PRInt32 amount, PRIntn flags,
PRNetAddr *addr, PRIntervalTime to) {
UNIMPLEMENTED;
return -1;
}
static PRInt32 StreamSendto(PRFileDesc *socket, const void *buf,
PRInt32 amount, PRIntn flags,
const PRNetAddr *addr, PRIntervalTime to) {
UNIMPLEMENTED;
return -1;
}
static PRInt16 StreamPoll(PRFileDesc *socket, PRInt16 in_flags,
PRInt16 *out_flags) {
UNIMPLEMENTED;
return -1;
}
static PRInt32 StreamAcceptRead(PRFileDesc *sd, PRFileDesc **nd,
PRNetAddr **raddr,
void *buf, PRInt32 amount, PRIntervalTime t) {
UNIMPLEMENTED;
return -1;
}
static PRInt32 StreamTransmitFile(PRFileDesc *sd, PRFileDesc *socket,
const void *headers, PRInt32 hlen,
PRTransmitFileFlags flags, PRIntervalTime t) {
UNIMPLEMENTED;
return -1;
}
static PRStatus StreamGetPeerName(PRFileDesc *socket, PRNetAddr *addr) {
// TODO(ekr@rtfm.com): Modify to return unique names for each channel
// somehow, as opposed to always the same static address. The current
// implementation messes up the session cache, which is why it's off
// elsewhere
addr->inet.family = PR_AF_INET;
addr->inet.port = 0;
addr->inet.ip = 0;
return PR_SUCCESS;
}
static PRStatus StreamGetSockName(PRFileDesc *socket, PRNetAddr *addr) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PRStatus StreamGetSockOption(PRFileDesc *socket, PRSocketOptionData *opt) {
switch (opt->option) {
case PR_SockOpt_Nonblocking:
opt->value.non_blocking = PR_TRUE;
return PR_SUCCESS;
default:
UNIMPLEMENTED;
break;
}
return PR_FAILURE;
}
// Imitate setting socket options. These are mostly noops.
static PRStatus StreamSetSockOption(PRFileDesc *socket,
const PRSocketOptionData *opt) {
switch (opt->option) {
case PR_SockOpt_Nonblocking:
return PR_SUCCESS;
case PR_SockOpt_NoDelay:
return PR_SUCCESS;
default:
UNIMPLEMENTED;
break;
}
return PR_FAILURE;
}
static PRInt32 StreamSendfile(PRFileDesc *out, PRSendFileData *in,
PRTransmitFileFlags flags, PRIntervalTime to) {
UNIMPLEMENTED;
return -1;
}
static PRStatus StreamConnectContinue(PRFileDesc *socket, PRInt16 flags) {
UNIMPLEMENTED;
return PR_FAILURE;
}
static PRIntn StreamReserved(PRFileDesc *socket) {
UNIMPLEMENTED;
return -1;
}
static const struct PRIOMethods nss_methods = {
PR_DESC_LAYERED,
StreamClose,
StreamRead,
StreamWrite,
StreamAvailable,
StreamAvailable64,
StreamSync,
StreamSeek,
StreamSeek64,
StreamFileInfo,
StreamFileInfo64,
StreamWritev,
StreamConnect,
StreamAccept,
StreamBind,
StreamListen,
StreamShutdown,
StreamRecv,
StreamSend,
StreamRecvfrom,
StreamSendto,
StreamPoll,
StreamAcceptRead,
StreamTransmitFile,
StreamGetSockName,
StreamGetPeerName,
StreamReserved,
StreamReserved,
StreamGetSockOption,
StreamSetSockOption,
StreamSendfile,
StreamConnectContinue,
StreamReserved,
StreamReserved,
StreamReserved,
StreamReserved
};
NSSStreamAdapter::NSSStreamAdapter(StreamInterface *stream)
: SSLStreamAdapterHelper(stream),
ssl_fd_(NULL),
cert_ok_(false) {
}
bool NSSStreamAdapter::Init() {
if (nspr_layer_identity == PR_INVALID_IO_LAYER) {
nspr_layer_identity = PR_GetUniqueIdentity("nssstreamadapter");
}
PRFileDesc *pr_fd = PR_CreateIOLayerStub(nspr_layer_identity, &nss_methods);
if (!pr_fd)
return false;
pr_fd->secret = reinterpret_cast<PRFilePrivate *>(stream());
PRFileDesc *ssl_fd;
if (ssl_mode_ == SSL_MODE_DTLS) {
ssl_fd = DTLS_ImportFD(NULL, pr_fd);
} else {
ssl_fd = SSL_ImportFD(NULL, pr_fd);
}
ASSERT(ssl_fd != NULL); // This should never happen
if (!ssl_fd) {
PR_Close(pr_fd);
return false;
}
SECStatus rv;
// Turn on security.
rv = SSL_OptionSet(ssl_fd, SSL_SECURITY, PR_TRUE);
if (rv != SECSuccess) {
LOG(LS_ERROR) << "Error enabling security on SSL Socket";
return false;
}
// Disable SSLv2.
rv = SSL_OptionSet(ssl_fd, SSL_ENABLE_SSL2, PR_FALSE);
if (rv != SECSuccess) {
LOG(LS_ERROR) << "Error disabling SSL2";
return false;
}
// Disable caching.
// TODO(ekr@rtfm.com): restore this when I have the caching
// identity set.
rv = SSL_OptionSet(ssl_fd, SSL_NO_CACHE, PR_TRUE);
if (rv != SECSuccess) {
LOG(LS_ERROR) << "Error disabling cache";
return false;
}
// Disable session tickets.
rv = SSL_OptionSet(ssl_fd, SSL_ENABLE_SESSION_TICKETS, PR_FALSE);
if (rv != SECSuccess) {
LOG(LS_ERROR) << "Error enabling tickets";
return false;
}
// Disable renegotiation.
rv = SSL_OptionSet(ssl_fd, SSL_ENABLE_RENEGOTIATION,
SSL_RENEGOTIATE_NEVER);
if (rv != SECSuccess) {
LOG(LS_ERROR) << "Error disabling renegotiation";
return false;
}
// Disable false start.
rv = SSL_OptionSet(ssl_fd, SSL_ENABLE_FALSE_START, PR_FALSE);
if (rv != SECSuccess) {
LOG(LS_ERROR) << "Error disabling false start";
return false;
}
ssl_fd_ = ssl_fd;
return true;
}
NSSStreamAdapter::~NSSStreamAdapter() {
if (ssl_fd_)
PR_Close(ssl_fd_);
};
int NSSStreamAdapter::BeginSSL() {
SECStatus rv;
if (!Init()) {
Error("Init", -1, false);
return -1;
}
ASSERT(state_ == SSL_CONNECTING);
// The underlying stream has been opened. If we are in peer-to-peer mode
// then a peer certificate must have been specified by now.
ASSERT(!ssl_server_name_.empty() ||
peer_certificate_.get() != NULL ||
!peer_certificate_digest_algorithm_.empty());
LOG(LS_INFO) << "BeginSSL: "
<< (!ssl_server_name_.empty() ? ssl_server_name_ :
"with peer");
if (role_ == SSL_CLIENT) {
LOG(LS_INFO) << "BeginSSL: as client";
rv = SSL_GetClientAuthDataHook(ssl_fd_, GetClientAuthDataHook,
this);
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
} else {
LOG(LS_INFO) << "BeginSSL: as server";
NSSIdentity *identity;
if (identity_.get()) {
identity = static_cast<NSSIdentity *>(identity_.get());
} else {
LOG(LS_ERROR) << "Can't be an SSL server without an identity";
Error("BeginSSL", -1, false);
return -1;
}
rv = SSL_ConfigSecureServer(ssl_fd_, identity->certificate().certificate(),
identity->keypair()->privkey(),
kt_rsa);
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
// Insist on a certificate from the client
rv = SSL_OptionSet(ssl_fd_, SSL_REQUEST_CERTIFICATE, PR_TRUE);
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
rv = SSL_OptionSet(ssl_fd_, SSL_REQUIRE_CERTIFICATE, PR_TRUE);
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
}
// Set the version range.
SSLVersionRange vrange;
vrange.min = (ssl_mode_ == SSL_MODE_DTLS) ?
SSL_LIBRARY_VERSION_TLS_1_1 :
SSL_LIBRARY_VERSION_TLS_1_0;
vrange.max = SSL_LIBRARY_VERSION_TLS_1_1;
rv = SSL_VersionRangeSet(ssl_fd_, &vrange);
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
// SRTP
#ifdef HAVE_DTLS_SRTP
if (!srtp_ciphers_.empty()) {
rv = SSL_SetSRTPCiphers(ssl_fd_, &srtp_ciphers_[0], srtp_ciphers_.size());
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
}
#endif
// Certificate validation
rv = SSL_AuthCertificateHook(ssl_fd_, AuthCertificateHook, this);
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
// Now start the handshake
rv = SSL_ResetHandshake(ssl_fd_, role_ == SSL_SERVER ? PR_TRUE : PR_FALSE);
if (rv != SECSuccess) {
Error("BeginSSL", -1, false);
return -1;
}
return ContinueSSL();
}
int NSSStreamAdapter::ContinueSSL() {
LOG(LS_INFO) << "ContinueSSL";
ASSERT(state_ == SSL_CONNECTING);
// Clear the DTLS timer
Thread::Current()->Clear(this, MSG_DTLS_TIMEOUT);
SECStatus rv = SSL_ForceHandshake(ssl_fd_);
if (rv == SECSuccess) {
LOG(LS_INFO) << "Handshake complete";
ASSERT(cert_ok_);
if (!cert_ok_) {
Error("ContinueSSL", -1, true);
return -1;
}
state_ = SSL_CONNECTED;
StreamAdapterInterface::OnEvent(stream(), SE_OPEN|SE_READ|SE_WRITE, 0);
return 0;
}
PRInt32 err = PR_GetError();
switch (err) {
case SSL_ERROR_RX_MALFORMED_HANDSHAKE:
if (ssl_mode_ != SSL_MODE_DTLS) {
Error("ContinueSSL", -1, true);
return -1;
} else {
LOG(LS_INFO) << "Malformed DTLS message. Ignoring.";
// Fall through
}
case PR_WOULD_BLOCK_ERROR:
LOG(LS_INFO) << "Would have blocked";
if (ssl_mode_ == SSL_MODE_DTLS) {
PRIntervalTime timeout;
SECStatus rv = DTLS_GetHandshakeTimeout(ssl_fd_, &timeout);
if (rv == SECSuccess) {
LOG(LS_INFO) << "Timeout is " << timeout << " ms";
Thread::Current()->PostDelayed(PR_IntervalToMilliseconds(timeout),
this, MSG_DTLS_TIMEOUT, 0);
}
}
return 0;
default:
LOG(LS_INFO) << "Error " << err;
break;
}
Error("ContinueSSL", -1, true);
return -1;
}
void NSSStreamAdapter::Cleanup() {
if (state_ != SSL_ERROR) {
state_ = SSL_CLOSED;
}
if (ssl_fd_) {
PR_Close(ssl_fd_);
ssl_fd_ = NULL;
}
identity_.reset();
peer_certificate_.reset();
Thread::Current()->Clear(this, MSG_DTLS_TIMEOUT);
}
StreamResult NSSStreamAdapter::Read(void* data, size_t data_len,
size_t* read, int* error) {
// SSL_CONNECTED sanity check.
switch (state_) {
case SSL_NONE:
case SSL_WAIT:
case SSL_CONNECTING:
return SR_BLOCK;
case SSL_CONNECTED:
break;
case SSL_CLOSED:
return SR_EOS;
case SSL_ERROR:
default:
if (error)
*error = ssl_error_code_;
return SR_ERROR;
}
PRInt32 rv = PR_Read(ssl_fd_, data, data_len);
if (rv == 0) {
return SR_EOS;
}
// Error
if (rv < 0) {
PRInt32 err = PR_GetError();
switch (err) {
case PR_WOULD_BLOCK_ERROR:
return SR_BLOCK;
default:
Error("Read", -1, false);
*error = err; // libjingle semantics are that this is impl-specific
return SR_ERROR;
}
}
// Success
*read = rv;
return SR_SUCCESS;
}
StreamResult NSSStreamAdapter::Write(const void* data, size_t data_len,
size_t* written, int* error) {
// SSL_CONNECTED sanity check.
switch (state_) {
case SSL_NONE:
case SSL_WAIT:
case SSL_CONNECTING:
return SR_BLOCK;
case SSL_CONNECTED:
break;
case SSL_ERROR:
case SSL_CLOSED:
default:
if (error)
*error = ssl_error_code_;
return SR_ERROR;
}
PRInt32 rv = PR_Write(ssl_fd_, data, data_len);
// Error
if (rv < 0) {
PRInt32 err = PR_GetError();
switch (err) {
case PR_WOULD_BLOCK_ERROR:
return SR_BLOCK;
default:
Error("Write", -1, false);
*error = err; // libjingle semantics are that this is impl-specific
return SR_ERROR;
}
}
// Success
*written = rv;
return SR_SUCCESS;
}
void NSSStreamAdapter::OnEvent(StreamInterface* stream, int events,
int err) {
int events_to_signal = 0;
int signal_error = 0;
ASSERT(stream == this->stream());
if ((events & SE_OPEN)) {
LOG(LS_INFO) << "NSSStreamAdapter::OnEvent SE_OPEN";
if (state_ != SSL_WAIT) {
ASSERT(state_ == SSL_NONE);
events_to_signal |= SE_OPEN;
} else {
state_ = SSL_CONNECTING;
if (int err = BeginSSL()) {
Error("BeginSSL", err, true);
return;
}
}
}
if ((events & (SE_READ|SE_WRITE))) {
LOG(LS_INFO) << "NSSStreamAdapter::OnEvent"
<< ((events & SE_READ) ? " SE_READ" : "")
<< ((events & SE_WRITE) ? " SE_WRITE" : "");
if (state_ == SSL_NONE) {
events_to_signal |= events & (SE_READ|SE_WRITE);
} else if (state_ == SSL_CONNECTING) {
if (int err = ContinueSSL()) {
Error("ContinueSSL", err, true);
return;
}
} else if (state_ == SSL_CONNECTED) {
if (events & SE_WRITE) {
LOG(LS_INFO) << " -- onStreamWriteable";
events_to_signal |= SE_WRITE;
}
if (events & SE_READ) {
LOG(LS_INFO) << " -- onStreamReadable";
events_to_signal |= SE_READ;
}
}
}
if ((events & SE_CLOSE)) {
LOG(LS_INFO) << "NSSStreamAdapter::OnEvent(SE_CLOSE, " << err << ")";
Cleanup();
events_to_signal |= SE_CLOSE;
// SE_CLOSE is the only event that uses the final parameter to OnEvent().
ASSERT(signal_error == 0);
signal_error = err;
}
if (events_to_signal)
StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error);
}
void NSSStreamAdapter::OnMessage(Message* msg) {
// Process our own messages and then pass others to the superclass
if (MSG_DTLS_TIMEOUT == msg->message_id) {
LOG(LS_INFO) << "DTLS timeout expired";
ContinueSSL();
} else {
StreamInterface::OnMessage(msg);
}
}
// Certificate verification callback. Called to check any certificate
SECStatus NSSStreamAdapter::AuthCertificateHook(void *arg,
PRFileDesc *fd,
PRBool checksig,
PRBool isServer) {
LOG(LS_INFO) << "NSSStreamAdapter::AuthCertificateHook";
NSSCertificate peer_cert(SSL_PeerCertificate(fd));
bool ok = false;
// TODO(ekr@rtfm.com): Should we be enforcing self-signed like
// the OpenSSL version?
NSSStreamAdapter *stream = reinterpret_cast<NSSStreamAdapter *>(arg);
if (stream->peer_certificate_.get()) {
LOG(LS_INFO) << "Checking against specified certificate";
// The peer certificate was specified
if (reinterpret_cast<NSSCertificate *>(stream->peer_certificate_.get())->
Equals(&peer_cert)) {
LOG(LS_INFO) << "Accepted peer certificate";
ok = true;
}
} else if (!stream->peer_certificate_digest_algorithm_.empty()) {
LOG(LS_INFO) << "Checking against specified digest";
// The peer certificate digest was specified
unsigned char digest[64]; // Maximum size
std::size_t digest_length;
if (!peer_cert.ComputeDigest(
stream->peer_certificate_digest_algorithm_,
digest, sizeof(digest), &digest_length)) {
LOG(LS_ERROR) << "Digest computation failed";
} else {
Buffer computed_digest(digest, digest_length);
if (computed_digest == stream->peer_certificate_digest_value_) {
LOG(LS_INFO) << "Accepted peer certificate";
ok = true;
}
}
} else {
// Other modes, but we haven't implemented yet
// TODO(ekr@rtfm.com): Implement real certificate validation
UNIMPLEMENTED;
}
if (ok) {
stream->cert_ok_ = true;
// Record the peer's certificate chain.
CERTCertList* cert_list = SSL_PeerCertificateChain(fd);
ASSERT(cert_list != NULL);
stream->peer_certificate_.reset(new NSSCertificate(cert_list));
CERT_DestroyCertList(cert_list);
return SECSuccess;
}
if (!ok && stream->ignore_bad_cert()) {
LOG(LS_WARNING) << "Ignoring cert error while verifying cert chain";
stream->cert_ok_ = true;
return SECSuccess;
}
PORT_SetError(SEC_ERROR_UNTRUSTED_CERT);
return SECFailure;
}
SECStatus NSSStreamAdapter::GetClientAuthDataHook(void *arg, PRFileDesc *fd,
CERTDistNames *caNames,
CERTCertificate **pRetCert,
SECKEYPrivateKey **pRetKey) {
LOG(LS_INFO) << "Client cert requested";
NSSStreamAdapter *stream = reinterpret_cast<NSSStreamAdapter *>(arg);
if (!stream->identity_.get()) {
LOG(LS_ERROR) << "No identity available";
return SECFailure;
}
NSSIdentity *identity = static_cast<NSSIdentity *>(stream->identity_.get());
// Destroyed internally by NSS
*pRetCert = CERT_DupCertificate(identity->certificate().certificate());
*pRetKey = SECKEY_CopyPrivateKey(identity->keypair()->privkey());
return SECSuccess;
}
// RFC 5705 Key Exporter
bool NSSStreamAdapter::ExportKeyingMaterial(const std::string& label,
const uint8* context,
size_t context_len,
bool use_context,
uint8* result,
size_t result_len) {
SECStatus rv = SSL_ExportKeyingMaterial(ssl_fd_,
label.c_str(), label.size(),
use_context,
context, context_len,
result, result_len);
return rv == SECSuccess;
}
bool NSSStreamAdapter::SetDtlsSrtpCiphers(
const std::vector<std::string>& ciphers) {
#ifdef HAVE_DTLS_SRTP
std::vector<PRUint16> internal_ciphers;
if (state_ != SSL_NONE)
return false;
for (std::vector<std::string>::const_iterator cipher = ciphers.begin();
cipher != ciphers.end(); ++cipher) {
bool found = false;
for (const SrtpCipherMapEntry *entry = kSrtpCipherMap; entry->cipher_id;
++entry) {
if (*cipher == entry->external_name) {
found = true;
internal_ciphers.push_back(entry->cipher_id);
break;
}
}
if (!found) {
LOG(LS_ERROR) << "Could not find cipher: " << *cipher;
return false;
}
}
if (internal_ciphers.empty())
return false;
srtp_ciphers_ = internal_ciphers;
return true;
#else
return false;
#endif
}
bool NSSStreamAdapter::GetDtlsSrtpCipher(std::string* cipher) {
#ifdef HAVE_DTLS_SRTP
ASSERT(state_ == SSL_CONNECTED);
if (state_ != SSL_CONNECTED)
return false;
PRUint16 selected_cipher;
SECStatus rv = SSL_GetSRTPCipher(ssl_fd_, &selected_cipher);
if (rv == SECFailure)
return false;
for (const SrtpCipherMapEntry *entry = kSrtpCipherMap;
entry->cipher_id; ++entry) {
if (selected_cipher == entry->cipher_id) {
*cipher = entry->external_name;
return true;
}
}
ASSERT(false); // This should never happen
#endif
return false;
}
bool NSSContext::initialized;
NSSContext *NSSContext::global_nss_context;
// Static initialization and shutdown
NSSContext *NSSContext::Instance() {
if (!global_nss_context) {
NSSContext *new_ctx = new NSSContext();
if (!(new_ctx->slot_ = PK11_GetInternalSlot())) {
delete new_ctx;
goto fail;
}
global_nss_context = new_ctx;
}
fail:
return global_nss_context;
}
bool NSSContext::InitializeSSL(VerificationCallback callback) {
ASSERT(!callback);
if (!initialized) {
SECStatus rv;
rv = NSS_NoDB_Init(NULL);
if (rv != SECSuccess) {
LOG(LS_ERROR) << "Couldn't initialize NSS error=" <<
PORT_GetError();
return false;
}
NSS_SetDomesticPolicy();
initialized = true;
}
return true;
}
bool NSSContext::InitializeSSLThread() {
// Not needed
return true;
}
bool NSSContext::CleanupSSL() {
// Not needed
return true;
}
bool NSSStreamAdapter::HaveDtls() {
return true;
}
bool NSSStreamAdapter::HaveDtlsSrtp() {
#ifdef HAVE_DTLS_SRTP
return true;
#else
return false;
#endif
}
bool NSSStreamAdapter::HaveExporter() {
return true;
}
} // namespace talk_base
#endif // HAVE_NSS_SSL_H
|