Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 7:481bce714567 1 /* asn_public.h
wolfSSL 7:481bce714567 2 *
wolfSSL 7:481bce714567 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 7:481bce714567 4 *
wolfSSL 7:481bce714567 5 * This file is part of wolfSSL.
wolfSSL 7:481bce714567 6 *
wolfSSL 7:481bce714567 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 7:481bce714567 8 * it under the terms of the GNU General Public License as published by
wolfSSL 7:481bce714567 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 7:481bce714567 10 * (at your option) any later version.
wolfSSL 7:481bce714567 11 *
wolfSSL 7:481bce714567 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 7:481bce714567 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 7:481bce714567 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 7:481bce714567 15 * GNU General Public License for more details.
wolfSSL 7:481bce714567 16 *
wolfSSL 7:481bce714567 17 * You should have received a copy of the GNU General Public License
wolfSSL 7:481bce714567 18 * along with this program; if not, write to the Free Software
wolfSSL 7:481bce714567 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 7:481bce714567 20 */
wolfSSL 7:481bce714567 21
wolfSSL 7:481bce714567 22
wolfSSL 7:481bce714567 23
wolfSSL 7:481bce714567 24 #ifndef WOLF_CRYPT_ASN_PUBLIC_H
wolfSSL 7:481bce714567 25 #define WOLF_CRYPT_ASN_PUBLIC_H
wolfSSL 7:481bce714567 26
wolfSSL 7:481bce714567 27 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 7:481bce714567 28 #ifdef HAVE_ECC
wolfSSL 7:481bce714567 29 #include <wolfssl/wolfcrypt/ecc.h>
wolfSSL 7:481bce714567 30 #endif
wolfSSL 7:481bce714567 31 #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
wolfSSL 7:481bce714567 32 #include <wolfssl/wolfcrypt/rsa.h>
wolfSSL 7:481bce714567 33 #endif
wolfSSL 7:481bce714567 34
wolfSSL 7:481bce714567 35 #ifdef __cplusplus
wolfSSL 7:481bce714567 36 extern "C" {
wolfSSL 7:481bce714567 37 #endif
wolfSSL 7:481bce714567 38
wolfSSL 7:481bce714567 39 /* Certificate file Type */
wolfSSL 7:481bce714567 40 enum CertType {
wolfSSL 7:481bce714567 41 CERT_TYPE = 0,
wolfSSL 7:481bce714567 42 PRIVATEKEY_TYPE,
wolfSSL 7:481bce714567 43 DH_PARAM_TYPE,
wolfSSL 7:481bce714567 44 DSA_PARAM_TYPE,
wolfSSL 7:481bce714567 45 CRL_TYPE,
wolfSSL 7:481bce714567 46 CA_TYPE,
wolfSSL 7:481bce714567 47 ECC_PRIVATEKEY_TYPE,
wolfSSL 7:481bce714567 48 DSA_PRIVATEKEY_TYPE,
wolfSSL 7:481bce714567 49 CERTREQ_TYPE,
wolfSSL 7:481bce714567 50 DSA_TYPE,
wolfSSL 7:481bce714567 51 ECC_TYPE,
wolfSSL 7:481bce714567 52 RSA_TYPE,
wolfSSL 7:481bce714567 53 PUBLICKEY_TYPE,
wolfSSL 7:481bce714567 54 RSA_PUBLICKEY_TYPE,
wolfSSL 7:481bce714567 55 ECC_PUBLICKEY_TYPE,
wolfSSL 7:481bce714567 56 TRUSTED_PEER_TYPE
wolfSSL 7:481bce714567 57 };
wolfSSL 7:481bce714567 58
wolfSSL 7:481bce714567 59
wolfSSL 7:481bce714567 60 /* Signature type, by OID sum */
wolfSSL 7:481bce714567 61 enum Ctc_SigType {
wolfSSL 7:481bce714567 62 CTC_SHAwDSA = 517,
wolfSSL 7:481bce714567 63 CTC_MD2wRSA = 646,
wolfSSL 7:481bce714567 64 CTC_MD5wRSA = 648,
wolfSSL 7:481bce714567 65 CTC_SHAwRSA = 649,
wolfSSL 7:481bce714567 66 CTC_SHAwECDSA = 520,
wolfSSL 7:481bce714567 67 CTC_SHA224wRSA = 658,
wolfSSL 7:481bce714567 68 CTC_SHA224wECDSA = 527,
wolfSSL 7:481bce714567 69 CTC_SHA256wRSA = 655,
wolfSSL 7:481bce714567 70 CTC_SHA256wECDSA = 524,
wolfSSL 7:481bce714567 71 CTC_SHA384wRSA = 656,
wolfSSL 7:481bce714567 72 CTC_SHA384wECDSA = 525,
wolfSSL 7:481bce714567 73 CTC_SHA512wRSA = 657,
wolfSSL 7:481bce714567 74 CTC_SHA512wECDSA = 526
wolfSSL 7:481bce714567 75 };
wolfSSL 7:481bce714567 76
wolfSSL 7:481bce714567 77 enum Ctc_Encoding {
wolfSSL 7:481bce714567 78 CTC_UTF8 = 0x0c, /* utf8 */
wolfSSL 7:481bce714567 79 CTC_PRINTABLE = 0x13 /* printable */
wolfSSL 7:481bce714567 80 };
wolfSSL 7:481bce714567 81
wolfSSL 7:481bce714567 82 enum Ctc_Misc {
wolfSSL 7:481bce714567 83 CTC_COUNTRY_SIZE = 2,
wolfSSL 7:481bce714567 84 CTC_NAME_SIZE = 64,
wolfSSL 7:481bce714567 85 CTC_DATE_SIZE = 32,
wolfSSL 7:481bce714567 86 CTC_MAX_ALT_SIZE = 16384, /* may be huge */
wolfSSL 7:481bce714567 87 CTC_SERIAL_SIZE = 8,
wolfSSL 7:481bce714567 88 #ifdef WOLFSSL_CERT_EXT
wolfSSL 7:481bce714567 89 /* AKID could contains: hash + (Option) AuthCertIssuer,AuthCertSerialNum
wolfSSL 7:481bce714567 90 * We support only hash */
wolfSSL 7:481bce714567 91 CTC_MAX_SKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
wolfSSL 7:481bce714567 92 CTC_MAX_AKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
wolfSSL 7:481bce714567 93 CTC_MAX_CERTPOL_SZ = 64,
wolfSSL 7:481bce714567 94 CTC_MAX_CERTPOL_NB = 2 /* Max number of Certificate Policy */
wolfSSL 7:481bce714567 95 #endif /* WOLFSSL_CERT_EXT */
wolfSSL 7:481bce714567 96 };
wolfSSL 7:481bce714567 97
wolfSSL 7:481bce714567 98 #ifdef WOLFSSL_CERT_GEN
wolfSSL 7:481bce714567 99
wolfSSL 7:481bce714567 100 #ifndef HAVE_ECC
wolfSSL 7:481bce714567 101 typedef struct ecc_key ecc_key;
wolfSSL 7:481bce714567 102 #endif
wolfSSL 7:481bce714567 103 #ifdef NO_RSA
wolfSSL 7:481bce714567 104 typedef struct RsaKey RsaKey;
wolfSSL 7:481bce714567 105 #endif
wolfSSL 7:481bce714567 106
wolfSSL 7:481bce714567 107 typedef struct CertName {
wolfSSL 7:481bce714567 108 char country[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 109 char countryEnc;
wolfSSL 7:481bce714567 110 char state[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 111 char stateEnc;
wolfSSL 7:481bce714567 112 char locality[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 113 char localityEnc;
wolfSSL 7:481bce714567 114 char sur[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 115 char surEnc;
wolfSSL 7:481bce714567 116 char org[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 117 char orgEnc;
wolfSSL 7:481bce714567 118 char unit[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 119 char unitEnc;
wolfSSL 7:481bce714567 120 char commonName[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 121 char commonNameEnc;
wolfSSL 7:481bce714567 122 char email[CTC_NAME_SIZE]; /* !!!! email has to be last !!!! */
wolfSSL 7:481bce714567 123 } CertName;
wolfSSL 7:481bce714567 124
wolfSSL 7:481bce714567 125
wolfSSL 7:481bce714567 126 /* for user to fill for certificate generation */
wolfSSL 7:481bce714567 127 typedef struct Cert {
wolfSSL 7:481bce714567 128 int version; /* x509 version */
wolfSSL 7:481bce714567 129 byte serial[CTC_SERIAL_SIZE]; /* serial number */
wolfSSL 7:481bce714567 130 int sigType; /* signature algo type */
wolfSSL 7:481bce714567 131 CertName issuer; /* issuer info */
wolfSSL 7:481bce714567 132 int daysValid; /* validity days */
wolfSSL 7:481bce714567 133 int selfSigned; /* self signed flag */
wolfSSL 7:481bce714567 134 CertName subject; /* subject info */
wolfSSL 7:481bce714567 135 int isCA; /* is this going to be a CA */
wolfSSL 7:481bce714567 136 /* internal use only */
wolfSSL 7:481bce714567 137 int bodySz; /* pre sign total size */
wolfSSL 7:481bce714567 138 int keyType; /* public key type of subject */
wolfSSL 7:481bce714567 139 #ifdef WOLFSSL_ALT_NAMES
wolfSSL 7:481bce714567 140 byte altNames[CTC_MAX_ALT_SIZE]; /* altNames copy */
wolfSSL 7:481bce714567 141 int altNamesSz; /* altNames size in bytes */
wolfSSL 7:481bce714567 142 byte beforeDate[CTC_DATE_SIZE]; /* before date copy */
wolfSSL 7:481bce714567 143 int beforeDateSz; /* size of copy */
wolfSSL 7:481bce714567 144 byte afterDate[CTC_DATE_SIZE]; /* after date copy */
wolfSSL 7:481bce714567 145 int afterDateSz; /* size of copy */
wolfSSL 7:481bce714567 146 #endif
wolfSSL 7:481bce714567 147 #ifdef WOLFSSL_CERT_EXT
wolfSSL 7:481bce714567 148 byte skid[CTC_MAX_SKID_SIZE]; /* Subject Key Identifier */
wolfSSL 7:481bce714567 149 int skidSz; /* SKID size in bytes */
wolfSSL 7:481bce714567 150 byte akid[CTC_MAX_AKID_SIZE]; /* Authority Key Identifier */
wolfSSL 7:481bce714567 151 int akidSz; /* AKID size in bytes */
wolfSSL 7:481bce714567 152 word16 keyUsage; /* Key Usage */
wolfSSL 7:481bce714567 153 char certPolicies[CTC_MAX_CERTPOL_NB][CTC_MAX_CERTPOL_SZ];
wolfSSL 7:481bce714567 154 word16 certPoliciesNb; /* Number of Cert Policy */
wolfSSL 7:481bce714567 155 #endif
wolfSSL 7:481bce714567 156 #ifdef WOLFSSL_CERT_REQ
wolfSSL 7:481bce714567 157 char challengePw[CTC_NAME_SIZE];
wolfSSL 7:481bce714567 158 #endif
wolfSSL 7:481bce714567 159 void* heap; /* heap hint */
wolfSSL 7:481bce714567 160 } Cert;
wolfSSL 7:481bce714567 161
wolfSSL 7:481bce714567 162
wolfSSL 7:481bce714567 163 /* Initialize and Set Certificate defaults:
wolfSSL 7:481bce714567 164 version = 3 (0x2)
wolfSSL 7:481bce714567 165 serial = 0 (Will be randomly generated)
wolfSSL 7:481bce714567 166 sigType = SHA_WITH_RSA
wolfSSL 7:481bce714567 167 issuer = blank
wolfSSL 7:481bce714567 168 daysValid = 500
wolfSSL 7:481bce714567 169 selfSigned = 1 (true) use subject as issuer
wolfSSL 7:481bce714567 170 subject = blank
wolfSSL 7:481bce714567 171 isCA = 0 (false)
wolfSSL 7:481bce714567 172 keyType = RSA_KEY (default)
wolfSSL 7:481bce714567 173 */
wolfSSL 7:481bce714567 174 WOLFSSL_API void wc_InitCert(Cert*);
wolfSSL 7:481bce714567 175 WOLFSSL_API int wc_MakeCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
wolfSSL 7:481bce714567 176 ecc_key*, WC_RNG*);
wolfSSL 7:481bce714567 177 #ifdef WOLFSSL_CERT_REQ
wolfSSL 7:481bce714567 178 WOLFSSL_API int wc_MakeCertReq(Cert*, byte* derBuffer, word32 derSz,
wolfSSL 7:481bce714567 179 RsaKey*, ecc_key*);
wolfSSL 7:481bce714567 180 #endif
wolfSSL 7:481bce714567 181 WOLFSSL_API int wc_SignCert(int requestSz, int sigType, byte* derBuffer,
wolfSSL 7:481bce714567 182 word32 derSz, RsaKey*, ecc_key*, WC_RNG*);
wolfSSL 7:481bce714567 183 WOLFSSL_API int wc_MakeSelfCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
wolfSSL 7:481bce714567 184 WC_RNG*);
wolfSSL 7:481bce714567 185 WOLFSSL_API int wc_SetIssuer(Cert*, const char*);
wolfSSL 7:481bce714567 186 WOLFSSL_API int wc_SetSubject(Cert*, const char*);
wolfSSL 7:481bce714567 187 #ifdef WOLFSSL_ALT_NAMES
wolfSSL 7:481bce714567 188 WOLFSSL_API int wc_SetAltNames(Cert*, const char*);
wolfSSL 7:481bce714567 189 #endif
wolfSSL 7:481bce714567 190 WOLFSSL_API int wc_SetIssuerBuffer(Cert*, const byte*, int);
wolfSSL 7:481bce714567 191 WOLFSSL_API int wc_SetSubjectBuffer(Cert*, const byte*, int);
wolfSSL 7:481bce714567 192 WOLFSSL_API int wc_SetAltNamesBuffer(Cert*, const byte*, int);
wolfSSL 7:481bce714567 193 WOLFSSL_API int wc_SetDatesBuffer(Cert*, const byte*, int);
wolfSSL 7:481bce714567 194
wolfSSL 7:481bce714567 195 #ifdef WOLFSSL_CERT_EXT
wolfSSL 7:481bce714567 196 WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
wolfSSL 7:481bce714567 197 ecc_key *eckey);
wolfSSL 7:481bce714567 198 WOLFSSL_API int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz);
wolfSSL 7:481bce714567 199 WOLFSSL_API int wc_SetAuthKeyId(Cert *cert, const char* file);
wolfSSL 7:481bce714567 200 WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
wolfSSL 7:481bce714567 201 ecc_key *eckey);
wolfSSL 7:481bce714567 202 WOLFSSL_API int wc_SetSubjectKeyId(Cert *cert, const char* file);
wolfSSL 7:481bce714567 203
wolfSSL 7:481bce714567 204 #ifdef HAVE_NTRU
wolfSSL 7:481bce714567 205 WOLFSSL_API int wc_SetSubjectKeyIdFromNtruPublicKey(Cert *cert, byte *ntruKey,
wolfSSL 7:481bce714567 206 word16 ntruKeySz);
wolfSSL 7:481bce714567 207 #endif
wolfSSL 7:481bce714567 208
wolfSSL 7:481bce714567 209 /* Set the KeyUsage.
wolfSSL 7:481bce714567 210 * Value is a string separated tokens with ','. Accepted tokens are :
wolfSSL 7:481bce714567 211 * digitalSignature,nonRepudiation,contentCommitment,keyCertSign,cRLSign,
wolfSSL 7:481bce714567 212 * dataEncipherment,keyAgreement,keyEncipherment,encipherOnly and decipherOnly.
wolfSSL 7:481bce714567 213 *
wolfSSL 7:481bce714567 214 * nonRepudiation and contentCommitment are for the same usage.
wolfSSL 7:481bce714567 215 */
wolfSSL 7:481bce714567 216 WOLFSSL_API int wc_SetKeyUsage(Cert *cert, const char *value);
wolfSSL 7:481bce714567 217
wolfSSL 7:481bce714567 218 #endif /* WOLFSSL_CERT_EXT */
wolfSSL 7:481bce714567 219
wolfSSL 7:481bce714567 220 #ifdef HAVE_NTRU
wolfSSL 7:481bce714567 221 WOLFSSL_API int wc_MakeNtruCert(Cert*, byte* derBuffer, word32 derSz,
wolfSSL 7:481bce714567 222 const byte* ntruKey, word16 keySz,
wolfSSL 7:481bce714567 223 WC_RNG*);
wolfSSL 7:481bce714567 224 #endif
wolfSSL 7:481bce714567 225
wolfSSL 7:481bce714567 226 #endif /* WOLFSSL_CERT_GEN */
wolfSSL 7:481bce714567 227
wolfSSL 7:481bce714567 228 #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
wolfSSL 7:481bce714567 229 #ifndef WOLFSSL_PEMPUBKEY_TODER_DEFINED
wolfSSL 7:481bce714567 230 #ifndef NO_FILESYSTEM
wolfSSL 7:481bce714567 231 /* forward from wolfssl */
wolfSSL 7:481bce714567 232 WOLFSSL_API int wolfSSL_PemPubKeyToDer(const char* fileName,
wolfSSL 7:481bce714567 233 unsigned char* derBuf, int derSz);
wolfSSL 7:481bce714567 234 #endif
wolfSSL 7:481bce714567 235
wolfSSL 7:481bce714567 236 /* forward from wolfssl */
wolfSSL 7:481bce714567 237 WOLFSSL_API int wolfSSL_PubKeyPemToDer(const unsigned char*, int,
wolfSSL 7:481bce714567 238 unsigned char*, int);
wolfSSL 7:481bce714567 239 #define WOLFSSL_PEMPUBKEY_TODER_DEFINED
wolfSSL 7:481bce714567 240 #endif /* WOLFSSL_PEMPUBKEY_TODER_DEFINED */
wolfSSL 7:481bce714567 241 #endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */
wolfSSL 7:481bce714567 242
wolfSSL 7:481bce714567 243 #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN) || !defined(NO_DSA)
wolfSSL 7:481bce714567 244 WOLFSSL_API int wc_DerToPem(const byte* der, word32 derSz, byte* output,
wolfSSL 7:481bce714567 245 word32 outputSz, int type);
wolfSSL 7:481bce714567 246 WOLFSSL_API int wc_DerToPemEx(const byte* der, word32 derSz, byte* output,
wolfSSL 7:481bce714567 247 word32 outputSz, byte *cipherIno, int type);
wolfSSL 7:481bce714567 248 #endif
wolfSSL 7:481bce714567 249
wolfSSL 7:481bce714567 250 #ifdef HAVE_ECC
wolfSSL 7:481bce714567 251 /* private key helpers */
wolfSSL 7:481bce714567 252 WOLFSSL_API int wc_EccPrivateKeyDecode(const byte*, word32*,
wolfSSL 7:481bce714567 253 ecc_key*, word32);
wolfSSL 7:481bce714567 254 WOLFSSL_API int wc_EccKeyToDer(ecc_key*, byte* output, word32 inLen);
wolfSSL 7:481bce714567 255
wolfSSL 7:481bce714567 256 /* public key helper */
wolfSSL 7:481bce714567 257 WOLFSSL_API int wc_EccPublicKeyDecode(const byte*, word32*,
wolfSSL 7:481bce714567 258 ecc_key*, word32);
wolfSSL 7:481bce714567 259 #if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
wolfSSL 7:481bce714567 260 WOLFSSL_API int wc_EccPublicKeyToDer(ecc_key*, byte* output,
wolfSSL 7:481bce714567 261 word32 inLen, int with_AlgCurve);
wolfSSL 7:481bce714567 262 #endif
wolfSSL 7:481bce714567 263 #endif
wolfSSL 7:481bce714567 264
wolfSSL 7:481bce714567 265 /* DER encode signature */
wolfSSL 7:481bce714567 266 WOLFSSL_API word32 wc_EncodeSignature(byte* out, const byte* digest,
wolfSSL 7:481bce714567 267 word32 digSz, int hashOID);
wolfSSL 7:481bce714567 268 WOLFSSL_API int wc_GetCTC_HashOID(int type);
wolfSSL 7:481bce714567 269
wolfSSL 7:481bce714567 270 /* Time */
wolfSSL 7:481bce714567 271 /* Returns seconds (Epoch/UTC)
wolfSSL 7:481bce714567 272 * timePtr: is "time_t", which is typically "long"
wolfSSL 7:481bce714567 273 * Example:
wolfSSL 7:481bce714567 274 long lTime;
wolfSSL 7:481bce714567 275 rc = wc_GetTime(&lTime, (word32)sizeof(lTime));
wolfSSL 7:481bce714567 276 */
wolfSSL 7:481bce714567 277 WOLFSSL_API int wc_GetTime(void* timePtr, word32 timeSize);
wolfSSL 7:481bce714567 278
wolfSSL 7:481bce714567 279 #ifdef __cplusplus
wolfSSL 7:481bce714567 280 } /* extern "C" */
wolfSSL 7:481bce714567 281 #endif
wolfSSL 7:481bce714567 282
wolfSSL 7:481bce714567 283 #endif /* WOLF_CRYPT_ASN_PUBLIC_H */
wolfSSL 7:481bce714567 284
wolfSSL 7:481bce714567 285