3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 #ifndef CTAO_CRYPT_ASN_H
24 #define CTAO_CRYPT_ASN_H
26 #include <cyassl/ctaocrypt/types.h>
27 #include <cyassl/ctaocrypt/rsa.h>
28 #include <cyassl/ctaocrypt/dh.h>
29 #include <cyassl/ctaocrypt/dsa.h>
30 #include <cyassl/ctaocrypt/sha.h>
31 #include <cyassl/ctaocrypt/md5.h>
32 #include <cyassl/ctaocrypt/asn_public.h> /* public interface */
34 #include <cyassl/ctaocrypt/ecc.h>
46 EXTERNAL_SERIAL_SIZE = 32,
56 ASN_BIT_STRING = 0x03,
57 ASN_OCTET_STRING = 0x04,
60 ASN_ENUMERATED = 0x0a,
64 ASN_GENERALIZED_TIME = 0x18,
65 CRL_EXTENSIONS = 0xa0,
66 ASN_EXTENSIONS = 0xa3,
67 ASN_LONG_LENGTH = 0x80
71 ASN_CONSTRUCTED = 0x20,
72 ASN_CONTEXT_SPECIFIC = 0x80
76 ASN_COMMON_NAME = 0x03, /* CN */
77 ASN_SUR_NAME = 0x04, /* SN */
78 ASN_COUNTRY_NAME = 0x06, /* C */
79 ASN_LOCALITY_NAME = 0x07, /* L */
80 ASN_STATE_NAME = 0x08, /* ST */
81 ASN_ORG_NAME = 0x0a, /* O */
82 ASN_ORGUNIT_NAME = 0x0b /* OU */
90 PBES2 = 13 /* algo ID */
93 enum ENCRYPTION_TYPES {
106 MAX_SALT_SIZE = 64, /* MAX PKCS Salt length */
107 MAX_IV_SIZE = 64, /* MAX PKCS Iv length */
108 MAX_KEY_SIZE = 64, /* MAX PKCS Key length */
109 PKCS5 = 5, /* PKCS oid tag */
110 PKCS5v2 = 6, /* PKCS #5 v2.0 */
111 PKCS12 = 12, /* PKCS #12 */
112 MAX_UNICODE_SZ = 256,
113 ASN_BOOL_SIZE = 2, /* including type */
115 RSA_INTS = 8, /* RSA ints in private key */
118 ASN_GEN_TIME_SZ = 15, /* 7 numbers * 2 + Zulu tag */
119 MAX_ENCODED_SIG_SZ = 512,
122 MAX_SEQ_SZ = 5, /* enum(seq | con) + length(4) */
123 MAX_SET_SZ = 5, /* enum(set | con) + length(4) */
124 MAX_VERSION_SZ = 5, /* enum + id + version(byte) + (header(2))*/
125 MAX_ENCODED_DIG_SZ = 73, /* sha512 + enum(bit or octet) + legnth(4) */
126 MAX_RSA_INT_SZ = 517, /* RSA raw sz 4096 for bits + tag + len(4) */
127 MAX_NTRU_KEY_SZ = 610, /* NTRU 112 bit public key */
128 MAX_NTRU_ENC_SZ = 628, /* NTRU 112 bit DER public encoding */
129 MAX_LENGTH_SZ = 4, /* Max length size for DER encoding */
130 MAX_RSA_E_SZ = 16, /* Max RSA public e size */
131 MAX_CA_SZ = 32, /* Max encoded CA basic constraint length */
132 MAX_SN_SZ = 35, /* Max encoded serial number (INT) length */
133 #ifdef CYASSL_CERT_GEN
134 #ifdef CYASSL_ALT_NAMES
135 MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
137 MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
139 /* Max total extensions, id + len + others */
141 MAX_PUBLIC_KEY_SZ = MAX_NTRU_ENC_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2
142 /* use bigger NTRU size */
186 enum Extensions_Sum {
201 typedef struct DecodedCert DecodedCert;
202 typedef struct Signer Signer;
209 word32 certBegin; /* offset to start of cert */
210 word32 sigIndex; /* offset to start of signature */
211 word32 sigLength; /* length of signature */
212 word32 signatureOID; /* sum of algorithm object id */
213 word32 keyOID; /* sum of key algo object id */
214 byte subjectHash[SHA_SIZE]; /* hash of all Names */
215 byte issuerHash[SHA_SIZE]; /* hash of all Names */
217 byte issuerKeyHash[SHA_SIZE]; /* hash of the public Key */
218 #endif /* HAVE_OCSP */
219 byte* signature; /* not owned, points into raw cert */
220 char* subjectCN; /* CommonName */
222 char issuer[ASN_NAME_MAX]; /* full name including common name */
223 char subject[ASN_NAME_MAX]; /* full name including common name */
224 int verify; /* Default to yes, but could be off */
225 byte* source; /* byte buffer holder cert, NOT owner */
226 word32 srcIdx; /* current offset into buffer */
227 word32 maxIdx; /* max offset based on init size */
228 void* heap; /* for user memory overrides */
229 byte serial[EXTERNAL_SERIAL_SIZE]; /* raw serial number */
230 int serialSz; /* raw serial bytes stored */
231 byte* extensions; /* not owned, points into raw cert */
232 int extensionsSz; /* length of cert extensions */
233 word32 extensionsIdx; /* if want to go back and parse later */
234 byte* extAuthInfo; /* Authority Information Access URI */
235 int extAuthInfoSz; /* length of the URI */
236 byte* extCrlInfo; /* CRL Distribution Points */
237 int extCrlInfoSz; /* length of the URI */
238 byte isCA; /* CA basic constraint true */
239 #ifdef CYASSL_CERT_GEN
240 /* easy access to subject info for other sign */
259 #endif /* CYASSL_CERT_GEN */
267 word32 keyOID; /* key type */
268 char* name; /* common name */
269 byte hash[SHA_DIGEST_SIZE]; /* sha hash of names in certificate */
274 /* not for public consumption but may use for testing sometimes */
275 #ifdef CYASSL_TEST_CERT
276 #define CYASSL_TEST_API CYASSL_API
278 #define CYASSL_TEST_API CYASSL_LOCAL
281 CYASSL_TEST_API void InitDecodedCert(DecodedCert*, byte*, word32, void*);
282 CYASSL_TEST_API void FreeDecodedCert(DecodedCert*);
283 CYASSL_TEST_API int ParseCert(DecodedCert*, int type, int verify, void* cm);
285 CYASSL_LOCAL int ParseCertRelative(DecodedCert*, int type, int verify,void* cm);
286 CYASSL_LOCAL int DecodeToKey(DecodedCert*, int verify);
288 CYASSL_LOCAL word32 EncodeSignature(byte* out, const byte* digest, word32 digSz,
291 CYASSL_LOCAL Signer* MakeSigner(void*);
292 CYASSL_LOCAL void FreeSigners(Signer*, void*);
295 CYASSL_LOCAL int ToTraditional(byte* buffer, word32 length);
296 CYASSL_LOCAL int ToTraditionalEnc(byte* buffer, word32 length,const char*, int);
300 /* ASN sig helpers */
301 CYASSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
303 CYASSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
304 mp_int* r, mp_int* s);
305 /* private key helpers */
306 CYASSL_LOCAL int EccPrivateKeyDecode(const byte* input,word32* inOutIdx,
310 #ifdef CYASSL_CERT_GEN
321 #endif /* CYASSL_CERT_GEN */
326 enum Ocsp_Response_Status {
327 OCSP_SUCCESSFUL = 0, /* Response has valid confirmations */
328 OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
329 OCSP_INTERNAL_ERROR = 2, /* Internal error in issuer */
330 OCSP_TRY_LATER = 3, /* Try again later */
331 OCSP_SIG_REQUIRED = 5, /* Must sign the request (4 is skipped) */
332 OCSP_UNAUTHROIZED = 6 /* Request unauthorized */
336 enum Ocsp_Cert_Status {
348 #define STATUS_LIST_SIZE 5
351 typedef struct OcspResponse OcspResponse;
354 struct OcspResponse {
355 int responseStatus; /* return code from Responder */
357 word32 respBegin; /* index to beginning of OCSP Response */
358 word32 respLength; /* length of the OCSP Response */
360 int version; /* Response version number */
362 word32 sigIndex; /* Index into source for start of sig */
363 word32 sigLength; /* Length in octets for the sig */
364 word32 sigOID; /* OID for hash used for sig */
366 int certStatusCount; /* Count of certificate statuses, Note
367 * 1:1 correspondence between certStatus
368 * and certSerialNumber */
369 byte certSN[STATUS_LIST_SIZE][EXTERNAL_SERIAL_SIZE];
370 int certSNsz[STATUS_LIST_SIZE];
371 /* Certificate serial number array. */
372 word32 certStatus[STATUS_LIST_SIZE];
373 /* Certificate status array */
375 byte* source; /* pointer to source buffer, not owned */
376 word32 maxIdx; /* max offset based on init size */
377 void* heap; /* for user memory overrides */
381 CYASSL_LOCAL void InitOcspResponse(OcspResponse*, byte*, word32, void*);
382 CYASSL_LOCAL void FreeOcspResponse(OcspResponse*);
383 CYASSL_LOCAL int OcspResponseDecode(OcspResponse*);
384 CYASSL_LOCAL int EncodeOcspRequest(DecodedCert*, byte*, word32);
387 #endif /* HAVE_OCSP */
390 /* for pointer use */
391 typedef struct RevokedCert RevokedCert;
396 byte serialNumber[EXTERNAL_SERIAL_SIZE];
401 typedef struct DecodedCRL DecodedCRL;
404 word32 certBegin; /* offset to start of cert */
405 word32 sigIndex; /* offset to start of signature */
406 word32 sigLength; /* length of signature */
407 word32 signatureOID; /* sum of algorithm object id */
408 byte* signature; /* pointer into raw source, not owned */
409 byte issuerHash[SHA_DIGEST_SIZE]; /* issuer hash */
410 byte crlHash[MD5_DIGEST_SIZE]; /* raw crl data hash */
411 byte lastDate[MAX_DATE_SIZE]; /* last date updated */
412 byte nextDate[MAX_DATE_SIZE]; /* next update date */
413 RevokedCert* certs; /* revoked cert list */
414 int totalCerts; /* number on list */
417 CYASSL_LOCAL void InitDecodedCRL(DecodedCRL*);
418 CYASSL_LOCAL int ParseCRL(DecodedCRL*, const byte* buff, long sz);
419 CYASSL_LOCAL void FreeDecodedCRL(DecodedCRL*);
422 #endif /* HAVE_CRL */
429 #endif /* CTAO_CRYPT_ASN_H */