summaryrefslogtreecommitdiff
path: root/mozilla/security/nss/lib/libpkix/pkix_pl_nss/module/pkix_pl_ldapresponse.c
blob: a3915b1a4b486050d6996ca445e11c771242bd87 (plain)
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
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the PKIX-C library.
 *
 * The Initial Developer of the Original Code is
 * Sun Microsystems, Inc.
 * Portions created by the Initial Developer are
 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
 *
 * Contributor(s):
 *   Sun Microsystems, Inc.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * pkix_pl_ldapresponse.c
 *
 */

#include <fcntl.h>
#include "pkix_pl_ldapresponse.h"

/* --Private-LdapResponse-Functions------------------------------------- */

/*
 * FUNCTION: pkix_pl_LdapResponse_Destroy
 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
 */
static PKIX_Error *
pkix_pl_LdapResponse_Destroy(
        PKIX_PL_Object *object,
        void *plContext)
{
        PKIX_PL_LdapResponse *ldapRsp = NULL;
        LDAPMessage *m = NULL;
        LDAPSearchResponseEntry *entry = NULL;
        LDAPSearchResponseResult *result = NULL;
        LDAPSearchResponseAttr **attributes = NULL;
        LDAPSearchResponseAttr *attr = NULL;
        SECItem **valp = NULL;
        SECItem *val = NULL;

        PKIX_ENTER(LDAPRESPONSE, "pkix_pl_LdapResponse_Destroy");
        PKIX_NULLCHECK_ONE(object);

        PKIX_CHECK(pkix_CheckType(object, PKIX_LDAPRESPONSE_TYPE, plContext),
                    PKIX_OBJECTNOTLDAPRESPONSE);

        ldapRsp = (PKIX_PL_LdapResponse *)object;

        m = &ldapRsp->decoded;

        if (m->messageID.data != NULL) {
                PR_Free(m->messageID.data);
        }

        if (m->protocolOp.selector ==
                LDAP_SEARCHRESPONSEENTRY_TYPE) {
                entry = &m->protocolOp.op.searchResponseEntryMsg;
                if (entry->objectName.data != NULL) {
                        PR_Free(entry->objectName.data);
                }
                if (entry->attributes != NULL) {
                        for (attributes = entry->attributes;
                                *attributes != NULL;
                                attributes++) {
                                attr = *attributes;
                                PR_Free(attr->attrType.data);
                                for (valp = attr->val; *valp != NULL; valp++) {
                                        val = *valp;
                                        if (val->data != NULL) {
                                                PR_Free(val->data);
                                        }
                                        PR_Free(val);
                                }
                                PR_Free(attr->val);
                                PR_Free(attr);
                        }
                        PR_Free(entry->attributes);
                }
        } else if (m->protocolOp.selector ==
                LDAP_SEARCHRESPONSERESULT_TYPE) {
                result = &m->protocolOp.op.searchResponseResultMsg;
                if (result->resultCode.data != NULL) {
                        PR_Free(result->resultCode.data);
                }
        }

        PKIX_FREE(ldapRsp->derEncoded.data);

cleanup:

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_Hashcode
 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
 */
static PKIX_Error *
pkix_pl_LdapResponse_Hashcode(
        PKIX_PL_Object *object,
        PKIX_UInt32 *pHashcode,
        void *plContext)
{
        PKIX_UInt32 dataLen = 0;
        PKIX_UInt32 dindex = 0;
        PKIX_UInt32 sizeOfLength = 0;
        PKIX_UInt32 idLen = 0;
        const unsigned char *msgBuf = NULL;
        PKIX_PL_LdapResponse *ldapRsp = NULL;

        PKIX_ENTER(LDAPRESPONSE, "pkix_pl_LdapResponse_Hashcode");
        PKIX_NULLCHECK_TWO(object, pHashcode);

        PKIX_CHECK(pkix_CheckType(object, PKIX_LDAPRESPONSE_TYPE, plContext),
                    PKIX_OBJECTNOTLDAPRESPONSE);

        ldapRsp = (PKIX_PL_LdapResponse *)object;

        *pHashcode = 0;

        /*
         * Two responses that differ only in msgnum are a match! Therefore,
         * start hashcoding beyond the encoded messageID field.
         */
        if (ldapRsp->derEncoded.data) {
                msgBuf = (const unsigned char *)ldapRsp->derEncoded.data;
                /* Is message length short form (one octet) or long form? */
                if ((msgBuf[1] & 0x80) != 0) {
                        sizeOfLength = msgBuf[1] & 0x7F;
                        for (dindex = 0; dindex < sizeOfLength; dindex++) {
                                dataLen = (dataLen << 8) + msgBuf[dindex + 2];
                        }
                } else {
                        dataLen = msgBuf[1];
                }

                /* How many bytes for the messageID? (Assume short form) */
                idLen = msgBuf[dindex + 3] + 2;
                dindex += idLen;
                dataLen -= idLen;
                msgBuf = &msgBuf[dindex + 2];

                PKIX_CHECK(pkix_hash(msgBuf, dataLen, pHashcode, plContext),
                        PKIX_HASHFAILED);
        }

cleanup:

        PKIX_RETURN(LDAPRESPONSE);

}

/*
 * FUNCTION: pkix_pl_LdapResponse_Equals
 * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h)
 */
static PKIX_Error *
pkix_pl_LdapResponse_Equals(
        PKIX_PL_Object *firstObj,
        PKIX_PL_Object *secondObj,
        PKIX_Boolean *pResult,
        void *plContext)
{
        PKIX_PL_LdapResponse *rsp1 = NULL;
        PKIX_PL_LdapResponse *rsp2 = NULL;
        PKIX_UInt32 secondType = 0;
        PKIX_UInt32 firstLen = 0;
        const unsigned char *firstData = NULL;
        const unsigned char *secondData = NULL;
        PKIX_UInt32 sizeOfLength = 0;
        PKIX_UInt32 dindex = 0;
        PKIX_UInt32 i = 0;

        PKIX_ENTER(LDAPRESPONSE, "pkix_pl_LdapResponse_Equals");
        PKIX_NULLCHECK_THREE(firstObj, secondObj, pResult);

        /* test that firstObj is a LdapResponse */
        PKIX_CHECK(pkix_CheckType(firstObj, PKIX_LDAPRESPONSE_TYPE, plContext),
                    PKIX_FIRSTOBJARGUMENTNOTLDAPRESPONSE);

        /*
         * Since we know firstObj is a LdapResponse, if both references are
         * identical, they must be equal
         */
        if (firstObj == secondObj){
                *pResult = PKIX_TRUE;
                goto cleanup;
        }

        /*
         * If secondObj isn't a LdapResponse, we don't throw an error.
         * We simply return a Boolean result of FALSE
         */
        *pResult = PKIX_FALSE;
        PKIX_CHECK(PKIX_PL_Object_GetType(secondObj, &secondType, plContext),
                PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
        if (secondType != PKIX_LDAPRESPONSE_TYPE) {
                goto cleanup;
        }

        rsp1 = (PKIX_PL_LdapResponse *)firstObj;
        rsp2 = (PKIX_PL_LdapResponse *)secondObj;

        /* If either lacks an encoded string, they cannot be compared */
        if (!(rsp1->derEncoded.data) || !(rsp2->derEncoded.data)) {
                goto cleanup;
        }

        if (rsp1->derEncoded.len != rsp2->derEncoded.len) {
                goto cleanup;
        }

        firstData = (const unsigned char *)rsp1->derEncoded.data;
        secondData = (const unsigned char *)rsp2->derEncoded.data;

        /*
         * Two responses that differ only in msgnum are equal! Therefore,
         * start the byte comparison beyond the encoded messageID field.
         */

        /* Is message length short form (one octet) or long form? */
        if ((firstData[1] & 0x80) != 0) {
                sizeOfLength = firstData[1] & 0x7F;
                for (dindex = 0; dindex < sizeOfLength; dindex++) {
                        firstLen = (firstLen << 8) + firstData[dindex + 2];
                }
        } else {
                firstLen = firstData[1];
        }

        /* How many bytes for the messageID? (Assume short form) */
        i = firstData[dindex + 3] + 2;
        dindex += i;
        firstLen -= i;
        firstData = &firstData[dindex + 2];

        /*
         * In theory, we have to calculate where the second message data
         * begins by checking its length encodings. But if these messages
         * are equal, we can re-use the calculation we already did. If they
         * are not equal, the byte comparisons will surely fail.
         */

        secondData = &secondData[dindex + 2];
        
        for (i = 0; i < firstLen; i++) {
                if (firstData[i] != secondData[i]) {
                        goto cleanup;
                }
        }

        *pResult = PKIX_TRUE;

cleanup:

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_RegisterSelf
 * DESCRIPTION:
 *  Registers PKIX_LDAPRESPONSE_TYPE and its related functions with
 *  systemClasses[]
 * PARAMETERS:
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Not Thread Safe - for performance and complexity reasons
 *
 *  Since this function is only called by PKIX_PL_Initialize, which should
 *  only be called once, it is acceptable that this function is not
 *  thread-safe.
 */
PKIX_Error *
pkix_pl_LdapResponse_RegisterSelf(void *plContext)
{
        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
        pkix_ClassTable_Entry entry;

        PKIX_ENTER(LDAPRESPONSE, "pkix_pl_LdapResponse_RegisterSelf");

        entry.description = "LdapResponse";
        entry.objCounter = 0;
        entry.typeObjectSize = sizeof(PKIX_PL_LdapResponse);
        entry.destructor = pkix_pl_LdapResponse_Destroy;
        entry.equalsFunction = pkix_pl_LdapResponse_Equals;
        entry.hashcodeFunction = pkix_pl_LdapResponse_Hashcode;
        entry.toStringFunction = NULL;
        entry.comparator = NULL;
        entry.duplicateFunction = pkix_duplicateImmutable;

        systemClasses[PKIX_LDAPRESPONSE_TYPE] = entry;

        PKIX_RETURN(LDAPRESPONSE);
}

/* --Public-Functions------------------------------------------------------- */

/*
 * FUNCTION: pkix_pl_LdapResponse_Create
 * DESCRIPTION:
 *
 *  This function creates an LdapResponse for the LDAPMessageType provided in
 *  "responseType" and a buffer capacity provided by "totalLength". It copies
 *  into its buffer either "totalLength" or "bytesAvailable" bytes, whichever
 *  is less, from the buffer pointed to by "partialData", storing the number of
 *  bytes copied at "pBytesConsumed" and storing the address of the LdapResponse
 *  at "pLdapResponse".
 *
 *  If a message is complete in a single I/O buffer, the LdapResponse will be
 *  complete when this function returns. If the message carries over into
 *  additional buffers, their contents will be added to the LdapResponse by
 *  susequent calls to pkix_pl_LdapResponse_Append.
 *
 * PARAMETERS
 *  "responseType"
 *      The value of the message type (LDAP_SEARCHRESPONSEENTRY_TYPE or
 *      LDAP_SEARCHRESPONSERESULT_TYPE) for the LdapResponse being created
 *  "totalLength"
 *      The UInt32 value for the total length of the encoded message to be
 *      stored in the LdapResponse
 *  "bytesAvailable"
 *      The UInt32 value for the number of bytes of data available in the
 *      current buffer.
 *  "partialData"
 *      The address from which data is to be copied.
 *  "pBytesConsumed"
 *      The address at which is stored the UInt32 number of bytes taken from the
 *      current buffer. If this number is less than "bytesAvailable", then bytes
 *      remain in the buffer for the next LdapResponse. Must be non-NULL.
 *  "pLdapResponse"
 *      The address where the created LdapResponse is stored. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns an LdapResponse Error if the function fails in a non-fatal way.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_Create(
        LDAPMessageType responseType,
        PKIX_UInt32 totalLength,
        PKIX_UInt32 bytesAvailable,
        void *partialData,
        PKIX_UInt32 *pBytesConsumed,
        PKIX_PL_LdapResponse **pLdapResponse,
        void *plContext)
{
        PKIX_UInt32 bytesConsumed = 0;
        PKIX_PL_LdapResponse *ldapResponse = NULL;
        void *data = NULL;

        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_Create");
        PKIX_NULLCHECK_ONE(pLdapResponse);

        if (bytesAvailable <= totalLength) {
                bytesConsumed = bytesAvailable;
        } else {
                bytesConsumed = totalLength;
        }

        /* create a PKIX_PL_LdapResponse object */
        PKIX_CHECK(PKIX_PL_Object_Alloc
                    (PKIX_LDAPRESPONSE_TYPE,
                    sizeof (PKIX_PL_LdapResponse),
                    (PKIX_PL_Object **)&ldapResponse,
                    plContext),
                    PKIX_COULDNOTCREATEOBJECT);

        ldapResponse->decoded.protocolOp.selector = responseType;
        ldapResponse->totalLength = totalLength;
        ldapResponse->partialLength = bytesConsumed;

        if (totalLength != 0){
                /* Alloc space for array */
                PKIX_NULLCHECK_ONE(partialData);

                PKIX_CHECK(PKIX_PL_Malloc
                    (totalLength,
                    &data,
                    plContext),
                    PKIX_MALLOCFAILED);

                PKIX_PL_NSSCALL
                    (LDAPRESPONSE,
                    PORT_Memcpy,
                    (data, partialData, bytesConsumed));
        }

        ldapResponse->derEncoded.type = siBuffer;
        ldapResponse->derEncoded.data = data;
        ldapResponse->derEncoded.len = totalLength;
        *pBytesConsumed = bytesConsumed;
        *pLdapResponse = ldapResponse;

cleanup:

        if (PKIX_ERROR_RECEIVED){
                PKIX_DECREF(ldapResponse);
        }

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_Append
 * DESCRIPTION:
 *
 *  This function updates the LdapResponse pointed to by "response" with up to
 *  "incrLength" from the buffer pointer to by "incrData", storing the number of
 *  bytes copied at "pBytesConsumed".
 *
 * PARAMETERS
 *  "response"
 *      The address of the LdapResponse being updated. Must be non-zero.
 *  "incrLength"
 *      The UInt32 value for the number of bytes of data available in the
 *      current buffer.
 *  "incrData"
 *      The address from which data is to be copied.
 *  "pBytesConsumed"
 *      The address at which is stored the UInt32 number of bytes taken from the
 *      current buffer. If this number is less than "incrLength", then bytes
 *      remain in the buffer for the next LdapResponse. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns an LdapResponse Error if the function fails in a non-fatal way.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_Append(
        PKIX_PL_LdapResponse *response,
        PKIX_UInt32 incrLength,
        void *incrData,
        PKIX_UInt32 *pBytesConsumed,
        void *plContext)
{
        PKIX_UInt32 newPartialLength = 0;
        PKIX_UInt32 bytesConsumed = 0;
        void *dest = NULL;

        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_Append");
        PKIX_NULLCHECK_TWO(response, pBytesConsumed);

        if (incrLength > 0) {

                /* Calculate how many bytes we have room for. */
                bytesConsumed =
                        response->totalLength - response->partialLength;

                if (bytesConsumed > incrLength) {
                        bytesConsumed = incrLength;
                }

                newPartialLength = response->partialLength + bytesConsumed;

                PKIX_NULLCHECK_ONE(incrData);

                dest = &(((char *)response->derEncoded.data)[
                        response->partialLength]);

                PKIX_PL_NSSCALL
                        (LDAPRESPONSE,
                        PORT_Memcpy,
                        (dest, incrData, bytesConsumed));

                response->partialLength = newPartialLength;
        }

        *pBytesConsumed = bytesConsumed;

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_IsComplete
 * DESCRIPTION:
 *
 *  This function determines whether the LdapResponse pointed to by "response"
 *  contains all the data called for by the "totalLength" parameter provided
 *  when it was created, storing PKIX_TRUE at "pIsComplete" if so, and
 *  PKIX_FALSE otherwise.
 *
 * PARAMETERS
 *  "response"
 *      The address of the LdapResponse being evaluaTED. Must be non-zero.
 *  "incrLength"
 *      The UInt32 value for the number of bytes of data available in the
 *      current buffer.
 *  "incrData"
 *      The address from which data is to be copied.
 *  "pIsComplete"
 *      The address at which is stored the Boolean indication of whether the
 *      LdapResponse is complete. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns an LdapResponse Error if the function fails in a non-fatal way.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_IsComplete(
        PKIX_PL_LdapResponse *response,
        PKIX_Boolean *pIsComplete,
        void *plContext)
{
        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_IsComplete");
        PKIX_NULLCHECK_TWO(response, pIsComplete);

        if (response->totalLength == response->partialLength) {
                *pIsComplete = PKIX_TRUE;
        } else {
                *pIsComplete = PKIX_FALSE;
        }

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_Decode
 * DESCRIPTION:
 *
 *  This function decodes the DER data contained in the LdapResponse pointed to
 *  by "response", using the arena pointed to by "arena", and storing at
 *  "pStatus" SECSuccess if the decoding was successful and SECFailure
 *  otherwise. The decoded message is stored in an element of "response".
 *
 * PARAMETERS
 *  "arena"
 *      The address of the PRArenaPool to be used in the decoding. Must be
 *      non-NULL.
 *  "response"
 *      The address of the LdapResponse whose DER data is to be decoded. Must
 *      be non-NULL.
 *  "pStatus"
 *      The address at which is stored the status from the decoding, SECSuccess
 *      if successful, SECFailure otherwise. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns an LdapResponse Error if the function fails in a non-fatal way.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_Decode(
        PRArenaPool *arena,
        PKIX_PL_LdapResponse *response,
        SECStatus *pStatus,
        void *plContext)
{
        LDAPMessage *msg;
        SECStatus rv = SECFailure;

        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_Decode");
        PKIX_NULLCHECK_THREE(arena, response, pStatus);

        if (response->totalLength != response->partialLength) {
                PKIX_ERROR(PKIX_ATTEMPTTODECODEANINCOMPLETERESPONSE);
        }

        msg = &(response->decoded);

        PKIX_PL_NSSCALL
                (LDAPRESPONSE, PORT_Memset, (msg, 0, sizeof (LDAPMessage)));

        PKIX_PL_NSSCALLRV(LDAPRESPONSE, rv, SEC_ASN1DecodeItem,
            (NULL, msg, PKIX_PL_LDAPMessageTemplate, &(response->derEncoded)));

        *pStatus = rv;
cleanup:

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_GetMessage
 * DESCRIPTION:
 *
 *  This function obtains the decoded message from the LdapResponse pointed to
 *  by "response", storing the result at "pMessage".
 *
 * PARAMETERS
 *  "response"
 *      The address of the LdapResponse whose decoded message is to be
 *      retrieved. Must be non-NULL.
 *  "pMessage"
 *      The address at which is stored the address of the decoded message. Must
 *      be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_GetMessage(
        PKIX_PL_LdapResponse *response,
        LDAPMessage **pMessage,
        void *plContext)
{
        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_GetMessage");
        PKIX_NULLCHECK_TWO(response, pMessage);

        *pMessage = &response->decoded;

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_GetCapacity
 * DESCRIPTION:
 *
 *  This function obtains from the LdapResponse pointed to by "response" the
 *  number of bytes remaining to be read, based on the totalLength that was
 *  provided to LdapResponse_Create and the data subsequently provided to
 *  LdapResponse_Append, storing the result at "pMessage".
 *
 * PARAMETERS
 *  "response"
 *      The address of the LdapResponse whose remaining capacity is to be
 *      retrieved. Must be non-NULL.
 *  "pCapacity"
 *      The address at which is stored the address of the decoded message. Must
 *      be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns an LdapResponse Error if the function fails in a non-fatal way.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_GetCapacity(
        PKIX_PL_LdapResponse *response,
        PKIX_UInt32 *pCapacity,
        void *plContext)
{
        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_GetCapacity");
        PKIX_NULLCHECK_TWO(response, pCapacity);

        *pCapacity = response->totalLength - response->partialLength;

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_GetMessageType
 * DESCRIPTION:
 *
 *  This function obtains the message type from the LdapResponse pointed to
 *  by "response", storing the result at "pMessageType".
 *
 * PARAMETERS
 *  "response"
 *      The address of the LdapResponse whose message type is to be
 *      retrieved. Must be non-NULL.
 *  "pMessageType" 
 *      The address at which is stored the type of the response message. Must
 *      be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_GetMessageType(
        PKIX_PL_LdapResponse *response,
        LDAPMessageType *pMessageType,
        void *plContext)
{
        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_GetMessageType");
        PKIX_NULLCHECK_TWO(response, pMessageType);

        *pMessageType = response->decoded.protocolOp.selector;

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_GetResultCode
 * DESCRIPTION:
 *
 *  This function obtains the result code from the LdapResponse pointed to
 *  by "response", storing the result at "pResultCode".
 *
 * PARAMETERS
 *  "response"
 *      The address of the LdapResponse whose result code is to be
 *      retrieved. Must be non-NULL.
 *  "pResultCode"
 *      The address at which is stored the address of the decoded message. Must
 *      be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns an LdapResponse Error if the function fails in a non-fatal way.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_GetResultCode(
        PKIX_PL_LdapResponse *response,
        LDAPResultCode *pResultCode,
        void *plContext)
{
        LDAPMessageType messageType = 0;
        LDAPSearchResponseResult *resultMsg = NULL;

        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_GetResultCode");
        PKIX_NULLCHECK_TWO(response, pResultCode);

        messageType = response->decoded.protocolOp.selector;

        if (messageType != LDAP_SEARCHRESPONSERESULT_TYPE) {
                PKIX_ERROR(PKIX_GETRESULTCODECALLEDFORNONRESULTMESSAGE);
        }

        resultMsg = &response->decoded.protocolOp.op.searchResponseResultMsg;

        *pResultCode = *(char *)(resultMsg->resultCode.data);

cleanup:

        PKIX_RETURN(LDAPRESPONSE);
}

/*
 * FUNCTION: pkix_pl_LdapResponse_GetAttributes
 * DESCRIPTION:
 *
 *  This function obtains the attributes from the LdapResponse pointed to
 *  by "response", storing the result at "pAttributes".
 *
 * PARAMETERS
 *  "response"
 *      The address of the LdapResponse whose decoded message is to be
 *      retrieved. Must be non-NULL.
 *  "pAttributes"
 *      The address at which is stored the attributes of the message. Must be
 *      non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns an LdapResponse Error if the function fails in a non-fatal way.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_LdapResponse_GetAttributes(
        PKIX_PL_LdapResponse *response,
        LDAPSearchResponseAttr ***pAttributes,
        void *plContext)
{
        LDAPMessageType messageType = 0;

        PKIX_ENTER(LDAPRESPONSE, "PKIX_PL_LdapResponse_GetResultCode");
        PKIX_NULLCHECK_TWO(response, pAttributes);

        messageType = response->decoded.protocolOp.selector;

        if (messageType != LDAP_SEARCHRESPONSEENTRY_TYPE) {
                PKIX_ERROR(PKIX_GETATTRIBUTESCALLEDFORNONENTRYMESSAGE);
        }

        *pAttributes = response->
                decoded.protocolOp.op.searchResponseEntryMsg.attributes;

cleanup:

        PKIX_RETURN(LDAPRESPONSE);
}