Loading...
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 | /* * Copyright (c) 2005-2017 Apple Inc. All Rights Reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * DER_Decode.h - DER decoding routines */ #ifndef _DER_DECODE_H_ #define _DER_DECODE_H_ #include <libDER/libDER_config.h> #include <libDER/libDER.h> __BEGIN_DECLS /* * Decoding one item consists of extracting its tag, a pointer * to the actual content, and the length of the content. Those * three are represented by a DERDecodedInfo. */ typedef struct { DERTag tag; DERItem content; } DERDecodedInfo; /* * Basic decoding primitive. Only works with: * * -- definite length encoding * -- one-byte tags * -- max content length fits in a DERSize * * No malloc or copy of the contents is performed; the returned * content->content.data is a pointer into the incoming der data. */ DERReturn DERDecodeItem( const DERItem *der, /* data to decode */ DERDecodedInfo *decoded); /* RETURNED */ /* * Basic decoding primitive. Allows for decoding with a partial buffer. * if allowPartialBuffer is true. A partial buffer would normally fail * because the encoded length would be greater than the size of the buffer passed in. * Only works with: * * -- definite length encoding * -- one-byte tags (unless DER_MULTIBYTE_TAGS is defined) * -- max content length fits in a DERSize * * No malloc or copy of the contents is performed; the returned * content->content.data is a pointer into the incoming der data. * * WARNING: Using a partial buffer can return a DERDecodedInfo object with * a length larger than the buffer. It is recommended to instead use * DERDecodeItemPartialBufferGetLength if you need partial buffers. * */ DER_bounds_safety_replaced(DERDecodeItemPartialBufferGetLength) DERReturn DERDecodeItemPartialBuffer( const DERItem *der, /* data to decode */ DERDecodedInfo *decoded, /* RETURNED */ DERBool allowPartialBuffer); /* * Same as above, but returns a DERDecodedInfo with a length no larger than the buffer. * The actual encoded length can be retrieved from encodedLength parameter. * encodedLength can be NULL to achieve the same behavior as DERDecodeItemPartialBuffer, * with allowPartialBuffer=false * * NOTE: The DERDecoded length will never be larger than the input buffer. * This is a key difference from DERDecodeItemPartialBuffer which could return invalid length. * */ DERReturn DERDecodeItemPartialBufferGetLength( const DERItem *der, /* data to decode */ DERDecodedInfo *decoded, /* RETURNED */ DERSize *encodedLength); /* * Given a BIT_STRING, in the form of its raw content bytes, * obtain the number of unused bits and the raw bit string bytes. */ DERReturn DERParseBitString( const DERItem *contents, DERItem *bitStringBytes, /* RETURNED */ DERByte *numUnusedBits); /* RETURNED */ /* * Given a BOOLEAN, in the form of its raw content bytes, * obtain it's value. */ DERReturn DERParseBoolean( const DERItem *contents, DERBool *value); /* RETURNED */ DERReturn DERParseBooleanWithDefault( const DERItem *contents, DERBool defaultValue, DERBool *value); /* RETURNED */ /* * Given a positive INTEGER, in the form of its raw content bytes, * obtain it's value as a 32 bit or 64 bit quantity. * Returns DR_BufOverflow if the value is too large to fit in the return type */ DERReturn DERParseInteger( const DERItem *contents, DERInt *value); /* RETURNED */ DERReturn DERParseInteger64( const DERItem *contents, DERLong *value); /* RETURNED */ DERReturn DERParseIntegerSigned( const DERItem *contents, DERSignedInt *value); /* RETURNED */ DERReturn DERParseInteger64Signed( const DERItem *contents, DERSignedLong *value); /* RETURNED */ /* * Sequence/set decode support. */ /* state representing a sequence or set being decoded */ typedef struct { DERByte *DER_ended_by(end) nextItem; DERByte *end; } DERSequence; /* * To decode a set or sequence, call DERDecodeSeqInit or * DERDecodeSeqContentInit once, then call DERDecodeSeqNext to * get each enclosed item. * * DERDecodeSeqNext returns DR_EndOfSequence when no more * items are available. */ /* * Use this to parse the top level sequence's tag and content length. */ DERReturn DERDecodeSeqInit( const DERItem *der, /* data to decode */ DERTag *tag, /* RETURNED tag of sequence/set. This will be * either ASN1_CONSTR_SEQUENCE or * ASN1_CONSTR_SET. */ DERSequence *derSeq); /* RETURNED, to use in DERDecodeSeqNext */ /* * Use this to start in on decoding a sequence's content, when * the top-level tag and content have already been decoded. */ DERReturn DERDecodeSeqContentInit( const DERItem *content, DERSequence *derSeq); /* RETURNED, to use in DERDecodeSeqNext */ //getting rid of the attribute #if defined(_MSC_VER) #define __attribute__(x) #endif /* obtain the next decoded item in a sequence or set */ __attribute__((warn_unused_result)) DERReturn DERDecodeSeqNext( DERSequence *derSeq, DERDecodedInfo *decoded); /* RETURNED */ /* * High level sequence decode. */ #if __BLOCKS__ /* Decode a sequence or set using a code block on each subitem. * This function implements the oft-repeated while(DERDecodeSeqNext == DR_Success) pattern. * To stop decoding, set stop to true. If an error (that should cause a stop) occurs, return a non-success status. */ #if !defined(_MSC_VER) DERReturn DERDecodeSequenceWithBlock(const DERItem *der, DERReturn (^operation)(DERDecodedInfo *content, bool *stop)); #else DERReturn DERDecodeSequenceWithBlock(const DERItem* der, DERReturn (*operation)(DERDecodedInfo* content, bool* stop)); #endif /* Decode sequence or set content using a code block on each subitem. * This function implements the oft-repeated while(DERDecodeSeqNext == DR_Success) pattern. * To stop decoding, set stop to true. If an error (that should cause a stop) occurs, return a non-success status. */ #if !defined(_MSC_VER) DERReturn DERDecodeSequenceContentWithBlock(const DERItem *der, DERReturn (^operation)(DERDecodedInfo *content, bool *stop)); #else DERReturn DERDecodeSequenceContentWithBlock(const DERItem* der, DERReturn(*operation)(DERDecodedInfo* content, bool* stop)); #endif #endif // __BLOCKS__ /* * Per-item decode options. */ /* Explicit default, no options */ #define DER_DEC_NO_OPTS 0x0000 /* This item optional, can be skipped during decode */ #define DER_DEC_OPTIONAL 0x0001 /* Skip the tag check; accept anything. */ #define DER_DEC_ASN_ANY 0x0002 /* Skip item, no write to DERDecodedInfo (but tag check still performed) */ #define DER_DEC_SKIP 0x0004 /* Save full DER encoding in DERDecodedInfo, including tag and length. Normally * only the content is saved. */ #define DER_DEC_SAVE_DER 0x0008 /* * High level sequence parse, starting with top-level tag and content. * Top level tag must be ASN1_CONSTR_SEQUENCE - if it's not, and that's * OK, use DERParseSequenceContent(). * * These never return DR_EndOfSequence - if an *unexpected* end of sequence * occurs, return DR_IncompleteSeq. * * Results of the decoding of one item are placed in a DERItem whose address * is the dest arg plus the offset value in the associated DERItemSpec. * * Items which are optional (DER_DEC_OPTIONAL) and which are not found, * leave their associated DERDecodedInfos unmodified. * * Processing of a sequence ends on detection of any error or after the * last DERItemSpec is processed. * * The sizeToZero argument, if nonzero, indicates the number of bytes * starting at dest to zero before processing the sequence. This is * generally desirable, particularly if there are any DER_DEC_OPTIONAL * items in the sequence; skipped optional items are detected by the * caller via a NULL DERDecodedInfo.content.data; if this hasn't been * explicitly zeroed (generally, by passing a nonzero value of sizeToZero), * skipped items can't be detected. */ DERReturn DERParseSequenceToObject( const DERItem *der, DERShort numItems, /* size of itemSpecs[] */ const DERItemSpec *DER_counted_by(numItems) itemSpecs, void *DER_sized_by(destSize) dest, /* DERDecodedInfo(s) here RETURNED */ DERSize destSize, DERSize sizeToZero); /* optional */ /* same as DERParseSequenceToObject, but without a destination size */ DER_bounds_safety_replaced(DERParseSequenceToObject) DERReturn DERParseSequence( const DERItem *der, DERShort numItems, /* size of itemSpecs[] */ const DERItemSpec *DER_counted_by(numItems) itemSpecs, void *DER_unsafe_indexable dest, /* DERDecodedInfo(s) here RETURNED */ DERSize sizeToZero); /* optional */ /* high level sequence parse, starting with sequence's content */ DERReturn DERParseSequenceContentToObject( const DERItem *content, DERShort numItems, /* size of itemSpecs[] */ const DERItemSpec *DER_counted_by(numItems) itemSpecs, void *DER_sized_by(destSize) dest, /* DERDecodedInfo(s) here RETURNED */ DERSize destSize, DERSize sizeToZero); /* optional */ /* same as DERParseSequenceContentToObject, but without a destination size */ DER_bounds_safety_replaced(DERParseSequenceContentToObject) DERReturn DERParseSequenceContent( const DERItem *content, DERShort numItems, /* size of itemSpecs[] */ const DERItemSpec *DER_counted_by(numItems) itemSpecs, void *DER_unsafe_indexable dest, /* DERDecodedInfo(s) here RETURNED */ DERSize sizeToZero); /* optional */ __END_DECLS #endif /* _DER_DECODE_H_ */ |