]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/cyassl/ctaocrypt/asn.h
Update CyaSSL to latest version.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / cyassl / ctaocrypt / asn.h
1 /* asn.h
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
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.
11  *
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.
16  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifndef NO_ASN
23
24 #ifndef CTAO_CRYPT_ASN_H
25 #define CTAO_CRYPT_ASN_H
26
27 #include <cyassl/ctaocrypt/types.h>
28 #include <cyassl/ctaocrypt/rsa.h>
29 #include <cyassl/ctaocrypt/dh.h>
30 #include <cyassl/ctaocrypt/dsa.h>
31 #include <cyassl/ctaocrypt/sha.h>
32 #include <cyassl/ctaocrypt/md5.h>
33 #include <cyassl/ctaocrypt/asn_public.h>   /* public interface */
34 #ifdef HAVE_ECC
35     #include <cyassl/ctaocrypt/ecc.h>
36 #endif
37
38 #ifdef __cplusplus
39     extern "C" {
40 #endif
41
42
43 enum {
44     ISSUER  = 0,
45     SUBJECT = 1,
46
47     EXTERNAL_SERIAL_SIZE = 32,
48
49     BEFORE  = 0,
50     AFTER   = 1
51 };
52
53 /* ASN Tags   */
54 enum ASN_Tags {        
55     ASN_BOOLEAN           = 0x01,
56     ASN_INTEGER           = 0x02,
57     ASN_BIT_STRING        = 0x03,
58     ASN_OCTET_STRING      = 0x04,
59     ASN_TAG_NULL          = 0x05,
60     ASN_OBJECT_ID         = 0x06,
61     ASN_ENUMERATED        = 0x0a,
62     ASN_UTF8STRING        = 0x0c,
63     ASN_SEQUENCE          = 0x10,
64     ASN_SET               = 0x11,
65     ASN_UTC_TIME          = 0x17,
66     ASN_OTHER_TYPE        = 0x00,
67     ASN_RFC822_TYPE       = 0x01,
68     ASN_DNS_TYPE          = 0x02,
69     ASN_DIR_TYPE          = 0x04,
70     ASN_GENERALIZED_TIME  = 0x18,
71     CRL_EXTENSIONS        = 0xa0,
72     ASN_EXTENSIONS        = 0xa3,
73     ASN_LONG_LENGTH       = 0x80
74 };
75
76 enum  ASN_Flags{
77     ASN_CONSTRUCTED       = 0x20,
78     ASN_CONTEXT_SPECIFIC  = 0x80
79 };
80
81 enum DN_Tags {
82     ASN_COMMON_NAME   = 0x03,   /* CN */
83     ASN_SUR_NAME      = 0x04,   /* SN */
84     ASN_SERIAL_NUMBER = 0x05,   /* serialNumber */
85     ASN_COUNTRY_NAME  = 0x06,   /* C  */
86     ASN_LOCALITY_NAME = 0x07,   /* L  */
87     ASN_STATE_NAME    = 0x08,   /* ST */
88     ASN_ORG_NAME      = 0x0a,   /* O  */
89     ASN_ORGUNIT_NAME  = 0x0b    /* OU */
90 };
91
92 enum PBES {
93     PBE_MD5_DES      = 0,
94     PBE_SHA1_DES     = 1,
95     PBE_SHA1_DES3    = 2,
96     PBE_SHA1_RC4_128 = 3,
97     PBES2            = 13       /* algo ID */
98 };
99
100 enum ENCRYPTION_TYPES {
101     DES_TYPE  = 0,
102     DES3_TYPE = 1,
103     RC4_TYPE  = 2
104 };
105
106 enum ECC_TYPES {
107     ECC_PREFIX_0 = 160,
108     ECC_PREFIX_1 = 161
109 };
110
111 enum Misc_ASN { 
112     ASN_NAME_MAX        = 256,
113     MAX_SALT_SIZE       =  64,     /* MAX PKCS Salt length */
114     MAX_IV_SIZE         =  64,     /* MAX PKCS Iv length */
115     MAX_KEY_SIZE        =  64,     /* MAX PKCS Key  length */
116     PKCS5               =   5,     /* PKCS oid tag */
117     PKCS5v2             =   6,     /* PKCS #5 v2.0 */
118     PKCS12              =  12,     /* PKCS #12 */
119     MAX_UNICODE_SZ      = 256,
120     ASN_BOOL_SIZE       =   2,     /* including type */
121     SHA_SIZE            =  20,
122     RSA_INTS            =   8,     /* RSA ints in private key */
123     MIN_DATE_SIZE       =  13,
124     MAX_DATE_SIZE       =  32,
125     ASN_GEN_TIME_SZ     =  15,     /* 7 numbers * 2 + Zulu tag */
126     MAX_ENCODED_SIG_SZ  = 512,
127     MAX_SIG_SZ          = 256,
128     MAX_ALGO_SZ         =  20,
129     MAX_SEQ_SZ          =   5,     /* enum(seq | con) + length(4) */  
130     MAX_SET_SZ          =   5,     /* enum(set | con) + length(4) */  
131     MAX_OCTET_STR_SZ    =   5,     /* enum(set | con) + length(4) */
132     MAX_EXP_SZ          =   5,     /* enum(contextspec|con|exp) + length(4) */
133     MAX_PRSTR_SZ        =   5,     /* enum(prstr) + length(4) */
134     MAX_VERSION_SZ      =   5,     /* enum + id + version(byte) + (header(2))*/
135     MAX_ENCODED_DIG_SZ  =  73,     /* sha512 + enum(bit or octet) + legnth(4) */
136     MAX_RSA_INT_SZ      = 517,     /* RSA raw sz 4096 for bits + tag + len(4) */
137     MAX_NTRU_KEY_SZ     = 610,     /* NTRU 112 bit public key */
138     MAX_NTRU_ENC_SZ     = 628,     /* NTRU 112 bit DER public encoding */
139     MAX_LENGTH_SZ       =   4,     /* Max length size for DER encoding */
140     MAX_RSA_E_SZ        =  16,     /* Max RSA public e size */
141     MAX_CA_SZ           =  32,     /* Max encoded CA basic constraint length */
142     MAX_SN_SZ           =  35,     /* Max encoded serial number (INT) length */
143 #ifdef CYASSL_CERT_GEN
144     #ifdef CYASSL_CERT_REQ
145                           /* Max encoded cert req attributes length */
146         MAX_ATTRIB_SZ   = MAX_SEQ_SZ * 3 + (11 + MAX_SEQ_SZ) * 2 +
147                           MAX_PRSTR_SZ + CTC_NAME_SIZE, /* 11 is the OID size */
148     #endif
149     #ifdef CYASSL_ALT_NAMES
150         MAX_EXTENSIONS_SZ   = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
151     #else
152         MAX_EXTENSIONS_SZ   = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
153     #endif
154                                    /* Max total extensions, id + len + others */
155 #endif
156     MAX_OCSP_EXT_SZ     = 58,      /* Max OCSP Extension length */
157     MAX_OCSP_NONCE_SZ   = 18,      /* OCSP Nonce size           */
158     EIGHTK_BUF          = 8192,    /* Tmp buffer size           */
159     MAX_PUBLIC_KEY_SZ   = MAX_NTRU_ENC_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2
160                                    /* use bigger NTRU size */
161 };
162
163
164 enum Oid_Types {
165     hashType  = 0,
166     sigType   = 1,
167     keyType   = 2,
168     curveType = 3,
169     blkType   = 4
170 };
171
172
173 enum Hash_Sum  {
174     MD2h    = 646,
175     MD5h    = 649,
176     SHAh    =  88,
177     SHA256h = 414,
178     SHA384h = 415,
179     SHA512h = 416
180 };
181
182
183 enum Block_Sum {
184     DESb  = 69,
185     DES3b = 652
186 };
187
188
189 enum Key_Sum {
190     DSAk   = 515,
191     RSAk   = 645,
192     NTRUk  = 274,
193     ECDSAk = 518
194 };
195
196
197 enum Ecc_Sum {
198     ECC_256R1 = 526,
199     ECC_384R1 = 210,
200     ECC_521R1 = 211,
201     ECC_160R1 = 184,
202     ECC_192R1 = 520,
203     ECC_224R1 = 209
204 };
205
206
207 enum KDF_Sum {
208     PBKDF2_OID = 660
209 };
210
211
212 enum Extensions_Sum {
213     BASIC_CA_OID    = 133,
214     ALT_NAMES_OID   = 131,
215     CRL_DIST_OID    = 145,
216     AUTH_INFO_OID   = 69,
217     CA_ISSUER_OID   = 117,
218     AUTH_KEY_OID    = 149,
219     SUBJ_KEY_OID    = 128,
220     CERT_POLICY_OID = 146,
221     KEY_USAGE_OID   = 129,  /* 2.5.29.15 */
222     INHIBIT_ANY_OID = 168,  /* 2.5.29.54 */
223     EXT_KEY_USAGE_OID = 151, /* 2.5.29.37 */
224     NAME_CONS_OID   = 144   /* 2.5.29.30 */
225 };
226
227 enum CertificatePolicy_Sum {
228     CP_ANY_OID      = 146  /* id-ce 32 0 */
229 };
230
231 enum SepHardwareName_Sum {
232     HW_NAME_OID     = 79   /* 1.3.6.1.5.5.7.8.4 from RFC 4108*/
233 };
234
235 enum AuthInfo_Sum {
236     AIA_OCSP_OID      = 116, /* 1.3.6.1.5.5.7.48.1 */
237     AIA_CA_ISSUER_OID = 117  /* 1.3.6.1.5.5.7.48.2 */
238 };
239
240 enum ExtKeyUsage_Sum { /* From RFC 5280 */
241     EKU_ANY_OID         = 151, /* 2.5.29.37.0, anyExtendedKeyUsage         */
242     EKU_SERVER_AUTH_OID = 71,  /* 1.3.6.1.5.5.7.3.1, id-kp-serverAuth      */
243     EKU_CLIENT_AUTH_OID = 72,  /* 1.3.6.1.5.5.7.3.2, id-kp-clientAuth      */
244     EKU_OCSP_SIGN_OID   = 79,  /* 1.3.6.1.5.5.7.3.9, OCSPSigning           */
245 };
246
247
248 enum VerifyType {
249     NO_VERIFY = 0,
250     VERIFY    = 1
251 };
252
253
254 /* Key usage extension bits */
255 #define KEYUSE_DIGITAL_SIG    0x0100
256 #define KEYUSE_CONTENT_COMMIT 0x0080
257 #define KEYUSE_KEY_ENCIPHER   0x0040
258 #define KEYUSE_DATA_ENCIPHER  0x0020
259 #define KEYUSE_KEY_AGREE      0x0010
260 #define KEYUSE_KEY_CERT_SIGN  0x0008
261 #define KEYUSE_CRL_SIGN       0x0004
262 #define KEYUSE_ENCIPHER_ONLY  0x0002
263 #define KEYUSE_DECIPHER_ONLY  0x0001
264
265 #define EXTKEYUSE_ANY         0x08
266 #define EXTKEYUSE_OCSP_SIGN   0x04
267 #define EXTKEYUSE_CLIENT_AUTH 0x02
268 #define EXTKEYUSE_SERVER_AUTH 0x01
269
270 typedef struct DNS_entry   DNS_entry;
271
272 struct DNS_entry {
273     DNS_entry* next;   /* next on DNS list */
274     char*      name;   /* actual DNS name */
275 };
276
277
278 typedef struct Base_entry  Base_entry;
279
280 struct Base_entry {
281     Base_entry* next;   /* next on name base list */
282     char*       name;   /* actual name base */
283     int         nameSz; /* name length */
284     byte        type;   /* Name base type (DNS or RFC822) */
285 };
286
287
288 struct DecodedName {
289     char*   fullName;
290     int     fullNameLen;
291     int     entryCount;
292     int     cnIdx;
293     int     cnLen;
294     int     snIdx;
295     int     snLen;
296     int     cIdx;
297     int     cLen;
298     int     lIdx;
299     int     lLen;
300     int     stIdx;
301     int     stLen;
302     int     oIdx;
303     int     oLen;
304     int     ouIdx;
305     int     ouLen;
306     int     emailIdx;
307     int     emailLen;
308     int     uidIdx;
309     int     uidLen;
310     int     serialIdx;
311     int     serialLen;
312 };
313
314
315 typedef struct DecodedCert DecodedCert;
316 typedef struct DecodedName DecodedName;
317 typedef struct Signer      Signer;
318
319
320 struct DecodedCert {
321     byte*   publicKey;
322     word32  pubKeySize;
323     int     pubKeyStored;
324     word32  certBegin;               /* offset to start of cert          */
325     word32  sigIndex;                /* offset to start of signature     */
326     word32  sigLength;               /* length of signature              */
327     word32  signatureOID;            /* sum of algorithm object id       */
328     word32  keyOID;                  /* sum of key algo  object id       */
329     int     version;                 /* cert version, 1 or 3             */
330     DNS_entry* altNames;             /* alt names list of dns entries    */
331 #ifndef IGNORE_NAME_CONSTRAINTS
332     DNS_entry* altEmailNames;        /* alt names list of RFC822 entries */
333     Base_entry* permittedNames;      /* Permitted name bases             */
334     Base_entry* excludedNames;       /* Excluded name bases              */
335 #endif /* IGNORE_NAME_CONSTRAINTS */
336     byte    subjectHash[SHA_SIZE];   /* hash of all Names                */
337     byte    issuerHash[SHA_SIZE];    /* hash of all Names                */
338 #ifdef HAVE_OCSP
339     byte    issuerKeyHash[SHA_SIZE]; /* hash of the public Key           */
340 #endif /* HAVE_OCSP */
341     byte*   signature;               /* not owned, points into raw cert  */
342     char*   subjectCN;               /* CommonName                       */
343     int     subjectCNLen;            /* CommonName Length                */
344     char    subjectCNEnc;            /* CommonName Encoding              */
345     int     subjectCNStored;         /* have we saved a copy we own      */
346     char    issuer[ASN_NAME_MAX];    /* full name including common name  */
347     char    subject[ASN_NAME_MAX];   /* full name including common name  */
348     int     verify;                  /* Default to yes, but could be off */
349     byte*   source;                  /* byte buffer holder cert, NOT owner */
350     word32  srcIdx;                  /* current offset into buffer       */
351     word32  maxIdx;                  /* max offset based on init size    */
352     void*   heap;                    /* for user memory overrides        */
353     byte    serial[EXTERNAL_SERIAL_SIZE];  /* raw serial number          */
354     int     serialSz;                /* raw serial bytes stored */
355     byte*   extensions;              /* not owned, points into raw cert  */
356     int     extensionsSz;            /* length of cert extensions */
357     word32  extensionsIdx;           /* if want to go back and parse later */
358     byte*   extAuthInfo;             /* Authority Information Access URI */
359     int     extAuthInfoSz;           /* length of the URI                */
360     byte*   extCrlInfo;              /* CRL Distribution Points          */
361     int     extCrlInfoSz;            /* length of the URI                */
362     byte    extSubjKeyId[SHA_SIZE];  /* Subject Key ID                   */
363     byte    extSubjKeyIdSet;         /* Set when the SKID was read from cert */
364     byte    extAuthKeyId[SHA_SIZE];  /* Authority Key ID                 */
365     byte    extAuthKeyIdSet;         /* Set when the AKID was read from cert */
366 #ifndef IGNORE_NAME_CONSTRAINTS
367     byte    extNameConstraintSet;
368 #endif /* IGNORE_NAME_CONSTRAINTS */
369     byte    isCA;                    /* CA basic constraint true         */
370     byte    extKeyUsageSet;
371     word16  extKeyUsage;             /* Key usage bitfield               */
372     byte    extExtKeyUsageSet;       /* Extended Key Usage               */
373     byte    extExtKeyUsage;          /* Extended Key usage bitfield      */
374 #ifdef OPENSSL_EXTRA
375     byte    extBasicConstSet;
376     byte    extBasicConstCrit;
377     byte    extBasicConstPlSet;
378     word32  pathLength;              /* CA basic constraint path length, opt */
379     byte    extSubjAltNameSet;
380     byte    extSubjAltNameCrit;
381     byte    extAuthKeyIdCrit;
382 #ifndef IGNORE_NAME_CONSTRAINTS
383     byte    extNameConstraintCrit;
384 #endif /* IGNORE_NAME_CONSTRAINTS */
385     byte    extSubjKeyIdCrit;
386     byte    extKeyUsageCrit;
387     byte    extExtKeyUsageCrit;
388     byte*   extExtKeyUsageSrc;
389     word32  extExtKeyUsageSz;
390     word32  extExtKeyUsageCount;
391     byte*   extAuthKeyIdSrc;
392     word32  extAuthKeyIdSz;
393     byte*   extSubjKeyIdSrc;
394     word32  extSubjKeyIdSz;
395 #endif
396 #ifdef HAVE_ECC
397     word32  pkCurveOID;           /* Public Key's curve OID */
398 #endif /* HAVE_ECC */
399     byte*   beforeDate;
400     int     beforeDateLen;
401     byte*   afterDate;
402     int     afterDateLen;
403 #ifdef HAVE_PKCS7
404     byte*   issuerRaw;               /* pointer to issuer inside source */
405     int     issuerRawLen;
406 #endif
407 #ifndef IGNORE_NAME_CONSTRAINT
408     byte*   subjectRaw;               /* pointer to subject inside source */
409     int     subjectRawLen;
410 #endif
411 #if defined(CYASSL_CERT_GEN)
412     /* easy access to subject info for other sign */
413     char*   subjectSN;
414     int     subjectSNLen;
415     char    subjectSNEnc;
416     char*   subjectC;
417     int     subjectCLen;
418     char    subjectCEnc;
419     char*   subjectL;
420     int     subjectLLen;
421     char    subjectLEnc;
422     char*   subjectST;
423     int     subjectSTLen;
424     char    subjectSTEnc;
425     char*   subjectO;
426     int     subjectOLen;
427     char    subjectOEnc;
428     char*   subjectOU;
429     int     subjectOULen;
430     char    subjectOUEnc;
431     char*   subjectEmail;
432     int     subjectEmailLen;
433 #endif /* CYASSL_CERT_GEN */
434 #ifdef OPENSSL_EXTRA
435     DecodedName issuerName;
436     DecodedName subjectName;
437 #endif /* OPENSSL_EXTRA */
438 #ifdef CYASSL_SEP
439     int     deviceTypeSz;
440     byte*   deviceType;
441     int     hwTypeSz;
442     byte*   hwType;
443     int     hwSerialNumSz;
444     byte*   hwSerialNum;
445     #ifdef OPENSSL_EXTRA
446         byte    extCertPolicySet;
447         byte    extCertPolicyCrit;
448     #endif /* OPENSSL_EXTRA */
449 #endif /* CYASSL_SEP */
450 };
451
452
453 #ifdef SHA_DIGEST_SIZE
454     #define SIGNER_DIGEST_SIZE SHA_DIGEST_SIZE
455 #else
456     #define SIGNER_DIGEST_SIZE 20 
457 #endif
458
459 /* CA Signers */
460 /* if change layout change PERSIST_CERT_CACHE functions too */
461 struct Signer {
462     word32  pubKeySize;
463     word32  keyOID;                  /* key type */
464     word16  keyUsage;
465     byte*   publicKey;
466     int     nameLen;
467     char*   name;                    /* common name */
468 #ifndef IGNORE_NAME_CONSTRAINTS
469         Base_entry* permittedNames;
470         Base_entry* excludedNames;
471 #endif /* IGNORE_NAME_CONSTRAINTS */
472     byte    subjectNameHash[SIGNER_DIGEST_SIZE];
473                                      /* sha hash of names in certificate */
474     #ifndef NO_SKID
475         byte    subjectKeyIdHash[SIGNER_DIGEST_SIZE];
476                                      /* sha hash of names in certificate */
477     #endif
478     Signer* next;
479 };
480
481
482 /* not for public consumption but may use for testing sometimes */
483 #ifdef CYASSL_TEST_CERT
484     #define CYASSL_TEST_API CYASSL_API
485 #else
486     #define CYASSL_TEST_API CYASSL_LOCAL
487 #endif
488
489 CYASSL_TEST_API void FreeAltNames(DNS_entry*, void*);
490 #ifndef IGNORE_NAME_CONSTRAINTS
491     CYASSL_TEST_API void FreeNameSubtrees(Base_entry*, void*);
492 #endif /* IGNORE_NAME_CONSTRAINTS */
493 CYASSL_TEST_API void InitDecodedCert(DecodedCert*, byte*, word32, void*);
494 CYASSL_TEST_API void FreeDecodedCert(DecodedCert*);
495 CYASSL_TEST_API int  ParseCert(DecodedCert*, int type, int verify, void* cm);
496
497 CYASSL_LOCAL int ParseCertRelative(DecodedCert*, int type, int verify,void* cm);
498 CYASSL_LOCAL int DecodeToKey(DecodedCert*, int verify);
499
500 CYASSL_LOCAL word32 EncodeSignature(byte* out, const byte* digest, word32 digSz,
501                                     int hashOID);
502
503 CYASSL_LOCAL Signer* MakeSigner(void*);
504 CYASSL_LOCAL void    FreeSigner(Signer*, void*);
505 CYASSL_LOCAL void    FreeSignerTable(Signer**, int, void*);
506
507
508 CYASSL_LOCAL int ToTraditional(byte* buffer, word32 length);
509 CYASSL_LOCAL int ToTraditionalEnc(byte* buffer, word32 length,const char*, int);
510
511 CYASSL_LOCAL int ValidateDate(const byte* date, byte format, int dateType);
512
513 /* ASN.1 helper functions */
514 CYASSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len,
515                            word32 maxIdx);
516 CYASSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len,
517                              word32 maxIdx);
518 CYASSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len,
519                         word32 maxIdx);
520 CYASSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx,
521                               int* version);
522 CYASSL_LOCAL int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx,
523                         word32 maxIdx);
524 CYASSL_LOCAL int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
525                            word32 maxIdx);
526 CYASSL_LOCAL word32 SetLength(word32 length, byte* output);
527 CYASSL_LOCAL word32 SetSequence(word32 len, byte* output);
528 CYASSL_LOCAL word32 SetOctetString(word32 len, byte* output);
529 CYASSL_LOCAL word32 SetImplicit(byte tag, byte number, word32 len,byte* output);
530 CYASSL_LOCAL word32 SetExplicit(byte number, word32 len, byte* output);
531 CYASSL_LOCAL word32 SetSet(word32 len, byte* output);
532 CYASSL_LOCAL word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz);
533 CYASSL_LOCAL int SetMyVersion(word32 version, byte* output, int header);
534 CYASSL_LOCAL int SetSerialNumber(const byte* sn, word32 snSz, byte* output);
535 CYASSL_LOCAL int GetNameHash(const byte* source, word32* idx, byte* hash,
536                              int maxIdx);
537
538 #ifdef HAVE_ECC
539     /* ASN sig helpers */
540     CYASSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
541                                       mp_int* s);
542     CYASSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
543                                        mp_int* r, mp_int* s);
544 #endif
545
546 #ifdef CYASSL_CERT_GEN
547
548 enum cert_enums {
549     NAME_ENTRIES    =  8,
550     JOINT_LEN       =  2,
551     EMAIL_JOINT_LEN =  9,
552     RSA_KEY         = 10,
553     NTRU_KEY        = 11,
554     ECC_KEY         = 12
555 };
556
557
558 #endif /* CYASSL_CERT_GEN */
559
560
561
562 /* for pointer use */
563 typedef struct CertStatus CertStatus;
564
565 #ifdef HAVE_OCSP
566
567 enum Ocsp_Response_Status {
568     OCSP_SUCCESSFUL        = 0, /* Response has valid confirmations */
569     OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
570     OCSP_INTERNAL_ERROR    = 2, /* Internal error in issuer */
571     OCSP_TRY_LATER         = 3, /* Try again later */
572     OCSP_SIG_REQUIRED      = 5, /* Must sign the request (4 is skipped) */
573     OCSP_UNAUTHROIZED      = 6  /* Request unauthorized */
574 };
575
576
577 enum Ocsp_Cert_Status {
578     CERT_GOOD    = 0,
579     CERT_REVOKED = 1,
580     CERT_UNKNOWN = 2
581 };
582
583
584 enum Ocsp_Sums {
585     OCSP_BASIC_OID = 117,
586     OCSP_NONCE_OID = 118
587 };
588
589
590 typedef struct OcspRequest  OcspRequest;
591 typedef struct OcspResponse OcspResponse;
592
593
594 struct CertStatus {
595     CertStatus* next;
596
597     byte serial[EXTERNAL_SERIAL_SIZE];
598     int serialSz;
599
600     int status;
601
602     byte thisDate[MAX_DATE_SIZE];
603     byte nextDate[MAX_DATE_SIZE];
604     byte thisDateFormat;
605     byte nextDateFormat;
606 };
607
608
609 struct OcspResponse {
610     int     responseStatus;  /* return code from Responder */
611
612     byte*   response;        /* Pointer to beginning of OCSP Response */
613     word32  responseSz;      /* length of the OCSP Response */
614
615     byte    producedDate[MAX_DATE_SIZE];
616                                                          /* Date at which this response was signed */
617     byte    producedDateFormat; /* format of the producedDate */
618     byte*   issuerHash;
619     byte*   issuerKeyHash;
620
621     byte*   cert;
622     word32  certSz;
623
624     byte*   sig;             /* Pointer to sig in source */
625     word32  sigSz;           /* Length in octets for the sig */
626     word32  sigOID;          /* OID for hash used for sig */
627
628     CertStatus* status;      /* certificate status to fill out */
629
630     byte*   nonce;           /* pointer to nonce inside ASN.1 response */
631     int     nonceSz;         /* length of the nonce string */
632
633     byte*   source;          /* pointer to source buffer, not owned */
634     word32  maxIdx;          /* max offset based on init size */
635 };
636
637
638 struct OcspRequest {
639     DecodedCert* cert;
640
641     byte    useNonce;
642     byte    nonce[MAX_OCSP_NONCE_SZ];
643     int     nonceSz;
644
645     byte*   issuerHash;      /* pointer to issuerHash in source cert */
646     byte*   issuerKeyHash;   /* pointer to issuerKeyHash in source cert */
647     byte*   serial;          /* pointer to serial number in source cert */
648     int     serialSz;        /* length of the serial number */
649
650     byte*   dest;            /* pointer to the destination ASN.1 buffer */
651     word32  destSz;          /* length of the destination buffer */
652 };
653
654
655 CYASSL_LOCAL void InitOcspResponse(OcspResponse*, CertStatus*, byte*, word32);
656 CYASSL_LOCAL int  OcspResponseDecode(OcspResponse*);
657
658 CYASSL_LOCAL void InitOcspRequest(OcspRequest*, DecodedCert*,
659                                                           byte, byte*, word32);
660 CYASSL_LOCAL int  EncodeOcspRequest(OcspRequest*);
661
662 CYASSL_LOCAL int  CompareOcspReqResp(OcspRequest*, OcspResponse*);
663
664
665 #endif /* HAVE_OCSP */
666
667
668 /* for pointer use */
669 typedef struct RevokedCert RevokedCert;
670
671 #ifdef HAVE_CRL
672
673 struct RevokedCert {
674     byte         serialNumber[EXTERNAL_SERIAL_SIZE];
675     int          serialSz;
676     RevokedCert* next;
677 };
678
679 typedef struct DecodedCRL DecodedCRL;
680
681 struct DecodedCRL {
682     word32  certBegin;               /* offset to start of cert          */
683     word32  sigIndex;                /* offset to start of signature     */
684     word32  sigLength;               /* length of signature              */
685     word32  signatureOID;            /* sum of algorithm object id       */
686     byte*   signature;               /* pointer into raw source, not owned */
687     byte    issuerHash[SHA_DIGEST_SIZE];  /* issuer hash                 */ 
688     byte    crlHash[SHA_DIGEST_SIZE];     /* raw crl data hash           */ 
689     byte    lastDate[MAX_DATE_SIZE]; /* last date updated  */
690     byte    nextDate[MAX_DATE_SIZE]; /* next update date   */
691     byte    lastDateFormat;          /* format of last date */
692     byte    nextDateFormat;          /* format of next date */
693     RevokedCert* certs;              /* revoked cert list  */
694     int          totalCerts;         /* number on list     */
695 };
696
697 CYASSL_LOCAL void InitDecodedCRL(DecodedCRL*);
698 CYASSL_LOCAL int  ParseCRL(DecodedCRL*, const byte* buff, word32 sz, void* cm);
699 CYASSL_LOCAL void FreeDecodedCRL(DecodedCRL*);
700
701
702 #endif /* HAVE_CRL */
703
704
705 #ifdef __cplusplus
706     } /* extern "C" */
707 #endif
708
709 #endif /* CTAO_CRYPT_ASN_H */
710
711 #endif /* !NO_ASN */