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,
65 ASN_GENERALIZED_TIME = 0x18,
66 CRL_EXTENSIONS = 0xa0,
67 ASN_EXTENSIONS = 0xa3,
68 ASN_LONG_LENGTH = 0x80
72 ASN_CONSTRUCTED = 0x20,
73 ASN_CONTEXT_SPECIFIC = 0x80
77 ASN_COMMON_NAME = 0x03, /* CN */
78 ASN_SUR_NAME = 0x04, /* SN */
79 ASN_COUNTRY_NAME = 0x06, /* C */
80 ASN_LOCALITY_NAME = 0x07, /* L */
81 ASN_STATE_NAME = 0x08, /* ST */
82 ASN_ORG_NAME = 0x0a, /* O */
83 ASN_ORGUNIT_NAME = 0x0b /* OU */
91 PBES2 = 13 /* algo ID */
94 enum ENCRYPTION_TYPES {
107 MAX_SALT_SIZE = 64, /* MAX PKCS Salt length */
108 MAX_IV_SIZE = 64, /* MAX PKCS Iv length */
109 MAX_KEY_SIZE = 64, /* MAX PKCS Key length */
110 PKCS5 = 5, /* PKCS oid tag */
111 PKCS5v2 = 6, /* PKCS #5 v2.0 */
112 PKCS12 = 12, /* PKCS #12 */
113 MAX_UNICODE_SZ = 256,
114 ASN_BOOL_SIZE = 2, /* including type */
116 RSA_INTS = 8, /* RSA ints in private key */
119 ASN_GEN_TIME_SZ = 15, /* 7 numbers * 2 + Zulu tag */
120 MAX_ENCODED_SIG_SZ = 512,
123 MAX_SEQ_SZ = 5, /* enum(seq | con) + length(4) */
124 MAX_SET_SZ = 5, /* enum(set | con) + length(4) */
125 MAX_VERSION_SZ = 5, /* enum + id + version(byte) + (header(2))*/
126 MAX_ENCODED_DIG_SZ = 73, /* sha512 + enum(bit or octet) + legnth(4) */
127 MAX_RSA_INT_SZ = 517, /* RSA raw sz 4096 for bits + tag + len(4) */
128 MAX_NTRU_KEY_SZ = 610, /* NTRU 112 bit public key */
129 MAX_NTRU_ENC_SZ = 628, /* NTRU 112 bit DER public encoding */
130 MAX_LENGTH_SZ = 4, /* Max length size for DER encoding */
131 MAX_RSA_E_SZ = 16, /* Max RSA public e size */
132 MAX_CA_SZ = 32, /* Max encoded CA basic constraint length */
133 MAX_SN_SZ = 35, /* Max encoded serial number (INT) length */
134 #ifdef CYASSL_CERT_GEN
135 #ifdef CYASSL_ALT_NAMES
136 MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
138 MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
140 /* Max total extensions, id + len + others */
142 MAX_OCSP_EXT_SZ = 58, /* Max OCSP Extension length */
143 MAX_OCSP_NONCE_SZ = 18, /* OCSP Nonce size */
144 MAX_PUBLIC_KEY_SZ = MAX_NTRU_ENC_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2
145 /* use bigger NTRU size */
189 enum Extensions_Sum {
204 typedef struct DNS_entry DNS_entry;
207 DNS_entry* next; /* next on DNS list */
208 char* name; /* actual DNS name */
211 typedef struct DecodedCert DecodedCert;
212 typedef struct Signer Signer;
219 word32 certBegin; /* offset to start of cert */
220 word32 sigIndex; /* offset to start of signature */
221 word32 sigLength; /* length of signature */
222 word32 signatureOID; /* sum of algorithm object id */
223 word32 keyOID; /* sum of key algo object id */
224 DNS_entry* altNames; /* alt names list of dns entries */
225 byte subjectHash[SHA_SIZE]; /* hash of all Names */
226 byte issuerHash[SHA_SIZE]; /* hash of all Names */
228 byte issuerKeyHash[SHA_SIZE]; /* hash of the public Key */
229 #endif /* HAVE_OCSP */
230 byte* signature; /* not owned, points into raw cert */
231 char* subjectCN; /* CommonName */
233 int subjectCNStored; /* have we saved a copy we own */
234 char issuer[ASN_NAME_MAX]; /* full name including common name */
235 char subject[ASN_NAME_MAX]; /* full name including common name */
236 int verify; /* Default to yes, but could be off */
237 byte* source; /* byte buffer holder cert, NOT owner */
238 word32 srcIdx; /* current offset into buffer */
239 word32 maxIdx; /* max offset based on init size */
240 void* heap; /* for user memory overrides */
241 byte serial[EXTERNAL_SERIAL_SIZE]; /* raw serial number */
242 int serialSz; /* raw serial bytes stored */
243 byte* extensions; /* not owned, points into raw cert */
244 int extensionsSz; /* length of cert extensions */
245 word32 extensionsIdx; /* if want to go back and parse later */
246 byte* extAuthInfo; /* Authority Information Access URI */
247 int extAuthInfoSz; /* length of the URI */
248 byte* extCrlInfo; /* CRL Distribution Points */
249 int extCrlInfoSz; /* length of the URI */
250 byte isCA; /* CA basic constraint true */
251 #ifdef CYASSL_CERT_GEN
252 /* easy access to subject info for other sign */
271 #endif /* CYASSL_CERT_GEN */
279 word32 keyOID; /* key type */
280 char* name; /* common name */
281 byte hash[SHA_DIGEST_SIZE]; /* sha hash of names in certificate */
286 /* not for public consumption but may use for testing sometimes */
287 #ifdef CYASSL_TEST_CERT
288 #define CYASSL_TEST_API CYASSL_API
290 #define CYASSL_TEST_API CYASSL_LOCAL
293 CYASSL_TEST_API void FreeAltNames(DNS_entry*, void*);
294 CYASSL_TEST_API void InitDecodedCert(DecodedCert*, byte*, word32, void*);
295 CYASSL_TEST_API void FreeDecodedCert(DecodedCert*);
296 CYASSL_TEST_API int ParseCert(DecodedCert*, int type, int verify, void* cm);
298 CYASSL_LOCAL int ParseCertRelative(DecodedCert*, int type, int verify,void* cm);
299 CYASSL_LOCAL int DecodeToKey(DecodedCert*, int verify);
301 CYASSL_LOCAL word32 EncodeSignature(byte* out, const byte* digest, word32 digSz,
304 CYASSL_LOCAL Signer* MakeSigner(void*);
305 CYASSL_LOCAL void FreeSigners(Signer*, void*);
308 CYASSL_LOCAL int ToTraditional(byte* buffer, word32 length);
309 CYASSL_LOCAL int ToTraditionalEnc(byte* buffer, word32 length,const char*, int);
311 CYASSL_LOCAL int ValidateDate(const byte* date, byte format, int dateType);
314 /* ASN sig helpers */
315 CYASSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
317 CYASSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
318 mp_int* r, mp_int* s);
319 /* private key helpers */
320 CYASSL_LOCAL int EccPrivateKeyDecode(const byte* input,word32* inOutIdx,
324 #ifdef CYASSL_CERT_GEN
335 #endif /* CYASSL_CERT_GEN */
339 /* for pointer use */
340 typedef struct CertStatus CertStatus;
344 enum Ocsp_Response_Status {
345 OCSP_SUCCESSFUL = 0, /* Response has valid confirmations */
346 OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
347 OCSP_INTERNAL_ERROR = 2, /* Internal error in issuer */
348 OCSP_TRY_LATER = 3, /* Try again later */
349 OCSP_SIG_REQUIRED = 5, /* Must sign the request (4 is skipped) */
350 OCSP_UNAUTHROIZED = 6 /* Request unauthorized */
354 enum Ocsp_Cert_Status {
362 OCSP_BASIC_OID = 117,
367 typedef struct OcspRequest OcspRequest;
368 typedef struct OcspResponse OcspResponse;
374 byte serial[EXTERNAL_SERIAL_SIZE];
379 byte thisDate[MAX_DATE_SIZE];
380 byte nextDate[MAX_DATE_SIZE];
386 struct OcspResponse {
387 int responseStatus; /* return code from Responder */
389 byte* response; /* Pointer to beginning of OCSP Response */
390 word32 responseSz; /* length of the OCSP Response */
392 byte producedDate[MAX_DATE_SIZE];
393 /* Date at which this response was signed */
394 byte producedDateFormat; /* format of the producedDate */
401 byte* sig; /* Pointer to sig in source */
402 word32 sigSz; /* Length in octets for the sig */
403 word32 sigOID; /* OID for hash used for sig */
405 CertStatus* status; /* certificate status to fill out */
407 byte* nonce; /* pointer to nonce inside ASN.1 response */
408 int nonceSz; /* length of the nonce string */
410 byte* source; /* pointer to source buffer, not owned */
411 word32 maxIdx; /* max offset based on init size */
418 byte nonce[MAX_OCSP_NONCE_SZ];
421 byte* issuerHash; /* pointer to issuerHash in source cert */
422 byte* issuerKeyHash; /* pointer to issuerKeyHash in source cert */
423 byte* serial; /* pointer to serial number in source cert */
424 int serialSz; /* length of the serial number */
426 byte* dest; /* pointer to the destination ASN.1 buffer */
427 word32 destSz; /* length of the destination buffer */
431 CYASSL_LOCAL void InitOcspResponse(OcspResponse*, CertStatus*, byte*, word32);
432 CYASSL_LOCAL int OcspResponseDecode(OcspResponse*);
434 CYASSL_LOCAL void InitOcspRequest(OcspRequest*, DecodedCert*, byte*, word32);
435 CYASSL_LOCAL int EncodeOcspRequest(OcspRequest*);
437 CYASSL_LOCAL int CompareOcspReqResp(OcspRequest*, OcspResponse*);
440 #endif /* HAVE_OCSP */
443 /* for pointer use */
444 typedef struct RevokedCert RevokedCert;
449 byte serialNumber[EXTERNAL_SERIAL_SIZE];
454 typedef struct DecodedCRL DecodedCRL;
457 word32 certBegin; /* offset to start of cert */
458 word32 sigIndex; /* offset to start of signature */
459 word32 sigLength; /* length of signature */
460 word32 signatureOID; /* sum of algorithm object id */
461 byte* signature; /* pointer into raw source, not owned */
462 byte issuerHash[SHA_DIGEST_SIZE]; /* issuer hash */
463 byte crlHash[MD5_DIGEST_SIZE]; /* raw crl data hash */
464 byte lastDate[MAX_DATE_SIZE]; /* last date updated */
465 byte nextDate[MAX_DATE_SIZE]; /* next update date */
466 byte lastDateFormat; /* format of last date */
467 byte nextDateFormat; /* format of next date */
468 RevokedCert* certs; /* revoked cert list */
469 int totalCerts; /* number on list */
472 CYASSL_LOCAL void InitDecodedCRL(DecodedCRL*);
473 CYASSL_LOCAL int ParseCRL(DecodedCRL*, const byte* buff, long sz, void* cm);
474 CYASSL_LOCAL void FreeDecodedCRL(DecodedCRL*);
477 #endif /* HAVE_CRL */
484 #endif /* CTAO_CRYPT_ASN_H */