security/pkix/lib/pkixder.cpp

branch
TOR_BUG_9701
changeset 3
141e0f1194b1
equal deleted inserted replaced
-1:000000000000 0:b0a250552275
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* Copyright 2013 Mozilla Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "pkixder.h"
19
20 namespace mozilla { namespace pkix { namespace der {
21
22 // not inline
23 Result
24 Fail(PRErrorCode errorCode)
25 {
26 PR_SetError(errorCode, 0);
27 return Failure;
28 }
29
30 // Too complicated to be inline
31 Result
32 ExpectTagAndGetLength(Input& input, uint8_t expectedTag, uint16_t& length)
33 {
34 PR_ASSERT((expectedTag & 0x1F) != 0x1F); // high tag number form not allowed
35
36 uint8_t tag;
37 if (input.Read(tag) != Success) {
38 return Failure;
39 }
40
41 if (tag != expectedTag) {
42 return Fail(SEC_ERROR_BAD_DER);
43 }
44
45 // The short form of length is a single byte with the high order bit set
46 // to zero. The long form of length is one byte with the high order bit
47 // set, followed by N bytes, where N is encoded in the lowest 7 bits of
48 // the first byte.
49 uint8_t length1;
50 if (input.Read(length1) != Success) {
51 return Failure;
52 }
53 if (!(length1 & 0x80)) {
54 length = length1;
55 } else if (length1 == 0x81) {
56 uint8_t length2;
57 if (input.Read(length2) != Success) {
58 return Failure;
59 }
60 if (length2 < 128) {
61 // Not shortest possible encoding
62 return Fail(SEC_ERROR_BAD_DER);
63 }
64 length = length2;
65 } else if (length1 == 0x82) {
66 if (input.Read(length) != Success) {
67 return Failure;
68 }
69 if (length < 256) {
70 // Not shortest possible encoding
71 return Fail(SEC_ERROR_BAD_DER);
72 }
73 } else {
74 // We don't support lengths larger than 2^16 - 1.
75 return Fail(SEC_ERROR_BAD_DER);
76 }
77
78 // Ensure the input is long enough for the length it says it has.
79 return input.EnsureLength(length);
80 }
81
82 } } } // namespace mozilla::pkix::der

mercurial