michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* Copyright 2013 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include "pkixder.h" michael@0: michael@0: namespace mozilla { namespace pkix { namespace der { michael@0: michael@0: // not inline michael@0: Result michael@0: Fail(PRErrorCode errorCode) michael@0: { michael@0: PR_SetError(errorCode, 0); michael@0: return Failure; michael@0: } michael@0: michael@0: // Too complicated to be inline michael@0: Result michael@0: ExpectTagAndGetLength(Input& input, uint8_t expectedTag, uint16_t& length) michael@0: { michael@0: PR_ASSERT((expectedTag & 0x1F) != 0x1F); // high tag number form not allowed michael@0: michael@0: uint8_t tag; michael@0: if (input.Read(tag) != Success) { michael@0: return Failure; michael@0: } michael@0: michael@0: if (tag != expectedTag) { michael@0: return Fail(SEC_ERROR_BAD_DER); michael@0: } michael@0: michael@0: // The short form of length is a single byte with the high order bit set michael@0: // to zero. The long form of length is one byte with the high order bit michael@0: // set, followed by N bytes, where N is encoded in the lowest 7 bits of michael@0: // the first byte. michael@0: uint8_t length1; michael@0: if (input.Read(length1) != Success) { michael@0: return Failure; michael@0: } michael@0: if (!(length1 & 0x80)) { michael@0: length = length1; michael@0: } else if (length1 == 0x81) { michael@0: uint8_t length2; michael@0: if (input.Read(length2) != Success) { michael@0: return Failure; michael@0: } michael@0: if (length2 < 128) { michael@0: // Not shortest possible encoding michael@0: return Fail(SEC_ERROR_BAD_DER); michael@0: } michael@0: length = length2; michael@0: } else if (length1 == 0x82) { michael@0: if (input.Read(length) != Success) { michael@0: return Failure; michael@0: } michael@0: if (length < 256) { michael@0: // Not shortest possible encoding michael@0: return Fail(SEC_ERROR_BAD_DER); michael@0: } michael@0: } else { michael@0: // We don't support lengths larger than 2^16 - 1. michael@0: return Fail(SEC_ERROR_BAD_DER); michael@0: } michael@0: michael@0: // Ensure the input is long enough for the length it says it has. michael@0: return input.EnsureLength(length); michael@0: } michael@0: michael@0: } } } // namespace mozilla::pkix::der