source: asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfssl/wolfcrypt/asn_public.h@ 372

Last change on this file since 372 was 372, checked in by coas-nagasima, 5 years ago

wolfsslを3.15.7にバージョンアップ

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 17.3 KB
Line 
1/* asn_public.h
2 *
3 * Copyright (C) 2006-2017 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL.
6 *
7 * wolfSSL 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 * wolfSSL 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-1335, USA
20 */
21
22/*!
23 \file wolfssl/wolfcrypt/asn_public.h
24*/
25
26#ifndef WOLF_CRYPT_ASN_PUBLIC_H
27#define WOLF_CRYPT_ASN_PUBLIC_H
28
29#include <wolfssl/wolfcrypt/types.h>
30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/* guard on redeclaration */
36#ifndef WC_ECCKEY_TYPE_DEFINED
37 typedef struct ecc_key ecc_key;
38 #define WC_ECCKEY_TYPE_DEFINED
39#endif
40#ifndef WC_ED25519KEY_TYPE_DEFINED
41 typedef struct ed25519_key ed25519_key;
42 #define WC_ED25519KEY_TYPE_DEFINED
43#endif
44#ifndef WC_RSAKEY_TYPE_DEFINED
45 typedef struct RsaKey RsaKey;
46 #define WC_RSAKEY_TYPE_DEFINED
47#endif
48#ifndef WC_RNG_TYPE_DEFINED
49 typedef struct WC_RNG WC_RNG;
50 #define WC_RNG_TYPE_DEFINED
51#endif
52
53
54/* Certificate file Type */
55enum CertType {
56 CERT_TYPE = 0,
57 PRIVATEKEY_TYPE,
58 DH_PARAM_TYPE,
59 DSA_PARAM_TYPE,
60 CRL_TYPE,
61 CA_TYPE,
62 ECC_PRIVATEKEY_TYPE,
63 DSA_PRIVATEKEY_TYPE,
64 CERTREQ_TYPE,
65 DSA_TYPE,
66 ECC_TYPE,
67 RSA_TYPE,
68 PUBLICKEY_TYPE,
69 RSA_PUBLICKEY_TYPE,
70 ECC_PUBLICKEY_TYPE,
71 TRUSTED_PEER_TYPE,
72 EDDSA_PRIVATEKEY_TYPE,
73 ED25519_TYPE,
74 PKCS12_TYPE,
75 PKCS8_PRIVATEKEY_TYPE,
76 PKCS8_ENC_PRIVATEKEY_TYPE
77};
78
79
80/* Signature type, by OID sum */
81enum Ctc_SigType {
82 CTC_SHAwDSA = 517,
83 CTC_MD2wRSA = 646,
84 CTC_MD5wRSA = 648,
85 CTC_SHAwRSA = 649,
86 CTC_SHAwECDSA = 520,
87 CTC_SHA224wRSA = 658,
88 CTC_SHA224wECDSA = 523,
89 CTC_SHA256wRSA = 655,
90 CTC_SHA256wECDSA = 524,
91 CTC_SHA384wRSA = 656,
92 CTC_SHA384wECDSA = 525,
93 CTC_SHA512wRSA = 657,
94 CTC_SHA512wECDSA = 526,
95 CTC_ED25519 = 256
96};
97
98enum Ctc_Encoding {
99 CTC_UTF8 = 0x0c, /* utf8 */
100 CTC_PRINTABLE = 0x13 /* printable */
101};
102
103#ifndef WC_CTC_NAME_SIZE
104 #define WC_CTC_NAME_SIZE 64
105#endif
106#ifndef WC_CTC_MAX_ALT_SIZE
107 #define WC_CTC_MAX_ALT_SIZE 16384
108#endif
109
110enum Ctc_Misc {
111 CTC_COUNTRY_SIZE = 2,
112 CTC_NAME_SIZE = WC_CTC_NAME_SIZE,
113 CTC_DATE_SIZE = 32,
114 CTC_MAX_ALT_SIZE = WC_CTC_MAX_ALT_SIZE, /* may be huge, default: 16384 */
115 CTC_SERIAL_SIZE = 16,
116#ifdef WOLFSSL_CERT_EXT
117 /* AKID could contains: hash + (Option) AuthCertIssuer,AuthCertSerialNum
118 * We support only hash */
119 CTC_MAX_SKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
120 CTC_MAX_AKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
121 CTC_MAX_CERTPOL_SZ = 64,
122 CTC_MAX_CERTPOL_NB = 2 /* Max number of Certificate Policy */
123#endif /* WOLFSSL_CERT_EXT */
124};
125
126/* DER buffer */
127typedef struct DerBuffer {
128 byte* buffer;
129 void* heap;
130 word32 length;
131 int type; /* enum CertType */
132 int dynType; /* DYNAMIC_TYPE_* */
133} DerBuffer;
134
135enum {
136 IV_SZ = 32, /* max iv sz */
137 NAME_SZ = 80, /* max one line */
138
139 PEM_PASS_READ = 0,
140 PEM_PASS_WRITE = 1,
141};
142
143
144typedef int (pem_password_cb)(char* passwd, int sz, int rw, void* userdata);
145
146typedef struct EncryptedInfo {
147 pem_password_cb* passwd_cb;
148 void* passwd_userdata;
149
150 long consumed; /* tracks PEM bytes consumed */
151
152 int cipherType;
153 word32 keySz;
154 word32 ivSz; /* salt or encrypted IV size */
155
156 char name[NAME_SZ]; /* cipher name, such as "DES-CBC" */
157 byte iv[IV_SZ]; /* salt or encrypted IV */
158
159 word16 set:1; /* if encryption set */
160} EncryptedInfo;
161
162
163#if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)
164#ifdef WOLFSSL_EKU_OID
165 #ifndef CTC_MAX_EKU_NB
166 #define CTC_MAX_EKU_NB 1
167 #endif
168 #ifndef CTC_MAX_EKU_OID_SZ
169 #define CTC_MAX_EKU_OID_SZ 30
170 #endif
171#else
172 #undef CTC_MAX_EKU_OID_SZ
173 #define CTC_MAX_EKU_OID_SZ 0
174#endif
175#endif /* WOLFSSL_CERT_GEN || WOLFSSL_CERT_EXT */
176
177#ifdef WOLFSSL_CERT_GEN
178
179#ifdef WOLFSSL_MULTI_ATTRIB
180#ifndef CTC_MAX_ATTRIB
181 #define CTC_MAX_ATTRIB 4
182#endif
183
184/* ASN Encoded Name field */
185typedef struct NameAttrib {
186 int sz; /* actual string value length */
187 int id; /* id of name */
188 int type; /* enc of name */
189 char value[CTC_NAME_SIZE]; /* name */
190} NameAttrib;
191#endif /* WOLFSSL_MULTI_ATTRIB */
192
193
194typedef struct CertName {
195 char country[CTC_NAME_SIZE];
196 char countryEnc;
197 char state[CTC_NAME_SIZE];
198 char stateEnc;
199 char locality[CTC_NAME_SIZE];
200 char localityEnc;
201 char sur[CTC_NAME_SIZE];
202 char surEnc;
203 char org[CTC_NAME_SIZE];
204 char orgEnc;
205 char unit[CTC_NAME_SIZE];
206 char unitEnc;
207 char commonName[CTC_NAME_SIZE];
208 char commonNameEnc;
209 char serialDev[CTC_NAME_SIZE];
210 char serialDevEnc;
211#ifdef WOLFSSL_CERT_EXT
212 char busCat[CTC_NAME_SIZE];
213 char busCatEnc;
214 char joiC[CTC_NAME_SIZE];
215 char joiCEnc;
216 char joiSt[CTC_NAME_SIZE];
217 char joiStEnc;
218#endif
219 char email[CTC_NAME_SIZE]; /* !!!! email has to be last !!!! */
220#ifdef WOLFSSL_MULTI_ATTRIB
221 NameAttrib name[CTC_MAX_ATTRIB];
222#endif
223} CertName;
224
225
226/* for user to fill for certificate generation */
227typedef struct Cert {
228 int version; /* x509 version */
229 byte serial[CTC_SERIAL_SIZE]; /* serial number */
230 int serialSz; /* serial size */
231 int sigType; /* signature algo type */
232 CertName issuer; /* issuer info */
233 int daysValid; /* validity days */
234 int selfSigned; /* self signed flag */
235 CertName subject; /* subject info */
236 int isCA; /* is this going to be a CA */
237 /* internal use only */
238 int bodySz; /* pre sign total size */
239 int keyType; /* public key type of subject */
240#ifdef WOLFSSL_ALT_NAMES
241 byte altNames[CTC_MAX_ALT_SIZE]; /* altNames copy */
242 int altNamesSz; /* altNames size in bytes */
243 byte beforeDate[CTC_DATE_SIZE]; /* before date copy */
244 int beforeDateSz; /* size of copy */
245 byte afterDate[CTC_DATE_SIZE]; /* after date copy */
246 int afterDateSz; /* size of copy */
247#endif
248#ifdef WOLFSSL_CERT_EXT
249 byte skid[CTC_MAX_SKID_SIZE]; /* Subject Key Identifier */
250 int skidSz; /* SKID size in bytes */
251 byte akid[CTC_MAX_AKID_SIZE]; /* Authority Key Identifier */
252 int akidSz; /* AKID size in bytes */
253 word16 keyUsage; /* Key Usage */
254 byte extKeyUsage; /* Extended Key Usage */
255#ifdef WOLFSSL_EKU_OID
256 /* Extended Key Usage OIDs */
257 byte extKeyUsageOID[CTC_MAX_EKU_NB][CTC_MAX_EKU_OID_SZ];
258 byte extKeyUsageOIDSz[CTC_MAX_EKU_NB];
259#endif
260 char certPolicies[CTC_MAX_CERTPOL_NB][CTC_MAX_CERTPOL_SZ];
261 word16 certPoliciesNb; /* Number of Cert Policy */
262 byte issRaw[sizeof(CertName)]; /* raw issuer info */
263 byte sbjRaw[sizeof(CertName)]; /* raw subject info */
264#endif
265#ifdef WOLFSSL_CERT_REQ
266 char challengePw[CTC_NAME_SIZE];
267#endif
268 void* heap; /* heap hint */
269} Cert;
270
271
272
273/* Initialize and Set Certificate defaults:
274 version = 3 (0x2)
275 serial = 0 (Will be randomly generated)
276 sigType = SHA_WITH_RSA
277 issuer = blank
278 daysValid = 500
279 selfSigned = 1 (true) use subject as issuer
280 subject = blank
281 isCA = 0 (false)
282 keyType = RSA_KEY (default)
283*/
284WOLFSSL_API int wc_InitCert(Cert*);
285WOLFSSL_API int wc_MakeCert_ex(Cert* cert, byte* derBuffer, word32 derSz,
286 int keyType, void* key, WC_RNG* rng);
287WOLFSSL_API int wc_MakeCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
288 ecc_key*, WC_RNG*);
289#ifdef WOLFSSL_CERT_REQ
290 WOLFSSL_API int wc_MakeCertReq_ex(Cert*, byte* derBuffer, word32 derSz,
291 int, void*);
292 WOLFSSL_API int wc_MakeCertReq(Cert*, byte* derBuffer, word32 derSz,
293 RsaKey*, ecc_key*);
294#endif
295WOLFSSL_API int wc_SignCert_ex(int requestSz, int sType, byte* buffer,
296 word32 buffSz, int keyType, void* key,
297 WC_RNG* rng);
298WOLFSSL_API int wc_SignCert(int requestSz, int sigType, byte* derBuffer,
299 word32 derSz, RsaKey*, ecc_key*, WC_RNG*);
300WOLFSSL_API int wc_MakeSelfCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
301 WC_RNG*);
302WOLFSSL_API int wc_SetIssuer(Cert*, const char*);
303WOLFSSL_API int wc_SetSubject(Cert*, const char*);
304#ifdef WOLFSSL_ALT_NAMES
305 WOLFSSL_API int wc_SetAltNames(Cert*, const char*);
306#endif
307WOLFSSL_API int wc_SetIssuerBuffer(Cert*, const byte*, int);
308WOLFSSL_API int wc_SetSubjectBuffer(Cert*, const byte*, int);
309WOLFSSL_API int wc_SetAltNamesBuffer(Cert*, const byte*, int);
310WOLFSSL_API int wc_SetDatesBuffer(Cert*, const byte*, int);
311
312#ifndef NO_ASN_TIME
313WOLFSSL_API int wc_GetCertDates(Cert* cert, struct tm* before,
314 struct tm* after);
315#endif
316
317#ifdef WOLFSSL_CERT_EXT
318WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey_ex(Cert *cert, int keyType,
319 void* key);
320WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
321 ecc_key *eckey);
322WOLFSSL_API int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz);
323WOLFSSL_API int wc_SetAuthKeyId(Cert *cert, const char* file);
324WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey_ex(Cert *cert, int keyType,
325 void* key);
326WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
327 ecc_key *eckey);
328WOLFSSL_API int wc_SetSubjectKeyId(Cert *cert, const char* file);
329WOLFSSL_API int wc_GetSubjectRaw(byte **subjectRaw, Cert *cert);
330WOLFSSL_API int wc_SetSubjectRaw(Cert* cert, const byte* der, int derSz);
331WOLFSSL_API int wc_SetIssuerRaw(Cert* cert, const byte* der, int derSz);
332
333#ifdef HAVE_NTRU
334WOLFSSL_API int wc_SetSubjectKeyIdFromNtruPublicKey(Cert *cert, byte *ntruKey,
335 word16 ntruKeySz);
336#endif
337
338/* Set the KeyUsage.
339 * Value is a string separated tokens with ','. Accepted tokens are :
340 * digitalSignature,nonRepudiation,contentCommitment,keyCertSign,cRLSign,
341 * dataEncipherment,keyAgreement,keyEncipherment,encipherOnly and decipherOnly.
342 *
343 * nonRepudiation and contentCommitment are for the same usage.
344 */
345WOLFSSL_API int wc_SetKeyUsage(Cert *cert, const char *value);
346
347/* Set ExtendedKeyUsage
348 * Value is a string separated tokens with ','. Accepted tokens are :
349 * any,serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,OCSPSigning
350 */
351WOLFSSL_API int wc_SetExtKeyUsage(Cert *cert, const char *value);
352
353
354#ifdef WOLFSSL_EKU_OID
355/* Set ExtendedKeyUsage with unique OID
356 * oid is expected to be in byte representation
357 */
358WOLFSSL_API int wc_SetExtKeyUsageOID(Cert *cert, const char *oid, word32 sz,
359 byte idx, void* heap);
360#endif /* WOLFSSL_EKU_OID */
361#endif /* WOLFSSL_CERT_EXT */
362
363 #ifdef HAVE_NTRU
364 WOLFSSL_API int wc_MakeNtruCert(Cert*, byte* derBuffer, word32 derSz,
365 const byte* ntruKey, word16 keySz,
366 WC_RNG*);
367 #endif
368
369#endif /* WOLFSSL_CERT_GEN */
370
371WOLFSSL_API int wc_GetDateInfo(const byte* certDate, int certDateSz,
372 const byte** date, byte* format, int* length);
373#ifndef NO_ASN_TIME
374WOLFSSL_API int wc_GetDateAsCalendarTime(const byte* date, int length,
375 byte format, struct tm* time);
376#endif
377
378#if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
379
380 WOLFSSL_API int wc_PemGetHeaderFooter(int type, const char** header,
381 const char** footer);
382
383#endif
384
385WOLFSSL_API int wc_AllocDer(DerBuffer** pDer, word32 length, int type, void* heap);
386WOLFSSL_API void wc_FreeDer(DerBuffer** pDer);
387
388#ifdef WOLFSSL_PEM_TO_DER
389 WOLFSSL_API int wc_PemToDer(const unsigned char* buff, long longSz, int type,
390 DerBuffer** pDer, void* heap, EncryptedInfo* info, int* eccKey);
391
392 WOLFSSL_API int wc_KeyPemToDer(const unsigned char*, int,
393 unsigned char*, int, const char*);
394 WOLFSSL_API int wc_CertPemToDer(const unsigned char*, int,
395 unsigned char*, int, int);
396#endif /* WOLFSSL_PEM_TO_DER */
397
398#if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
399 #ifndef NO_FILESYSTEM
400 WOLFSSL_API int wc_PemPubKeyToDer(const char* fileName,
401 unsigned char* derBuf, int derSz);
402 #endif
403
404 WOLFSSL_API int wc_PubKeyPemToDer(const unsigned char*, int,
405 unsigned char*, int);
406#endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */
407
408#ifdef WOLFSSL_CERT_GEN
409 #ifndef NO_FILESYSTEM
410 WOLFSSL_API int wc_PemCertToDer(const char* fileName,
411 unsigned char* derBuf, int derSz);
412 #endif
413#endif /* WOLFSSL_CERT_GEN */
414
415#ifdef WOLFSSL_DER_TO_PEM
416 WOLFSSL_API int wc_DerToPem(const byte* der, word32 derSz, byte* output,
417 word32 outputSz, int type);
418 WOLFSSL_API int wc_DerToPemEx(const byte* der, word32 derSz, byte* output,
419 word32 outputSz, byte *cipherIno, int type);
420#endif
421
422#if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
423 WOLFSSL_API int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx,
424 word32 inSz, const byte** n, word32* nSz, const byte** e, word32* eSz);
425#endif
426
427#ifdef HAVE_ECC
428 /* private key helpers */
429 WOLFSSL_API int wc_EccPrivateKeyDecode(const byte*, word32*,
430 ecc_key*, word32);
431 WOLFSSL_API int wc_EccKeyToDer(ecc_key*, byte* output, word32 inLen);
432 WOLFSSL_API int wc_EccPrivateKeyToDer(ecc_key* key, byte* output,
433 word32 inLen);
434 WOLFSSL_API int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output,
435 word32* outLen);
436
437 /* public key helper */
438 WOLFSSL_API int wc_EccPublicKeyDecode(const byte*, word32*,
439 ecc_key*, word32);
440 WOLFSSL_API int wc_EccPublicKeyToDer(ecc_key*, byte* output,
441 word32 inLen, int with_AlgCurve);
442 #endif
443
444#ifdef HAVE_ED25519
445 /* private key helpers */
446 WOLFSSL_API int wc_Ed25519PrivateKeyDecode(const byte*, word32*,
447 ed25519_key*, word32);
448 WOLFSSL_API int wc_Ed25519KeyToDer(ed25519_key* key, byte* output,
449 word32 inLen);
450 WOLFSSL_API int wc_Ed25519PrivateKeyToDer(ed25519_key* key, byte* output,
451 word32 inLen);
452
453 /* public key helper */
454 WOLFSSL_API int wc_Ed25519PublicKeyDecode(const byte*, word32*,
455 ed25519_key*, word32);
456 #if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
457 WOLFSSL_API int wc_Ed25519PublicKeyToDer(ed25519_key*, byte* output,
458 word32 inLen, int with_AlgCurve);
459 #endif
460#endif
461
462/* DER encode signature */
463WOLFSSL_API word32 wc_EncodeSignature(byte* out, const byte* digest,
464 word32 digSz, int hashOID);
465WOLFSSL_API int wc_GetCTC_HashOID(int type);
466
467WOLFSSL_API int wc_GetPkcs8TraditionalOffset(byte* input,
468 word32* inOutIdx, word32 sz);
469WOLFSSL_API int wc_CreatePKCS8Key(byte* out, word32* outSz,
470 byte* key, word32 keySz, int algoID, const byte* curveOID, word32 oidSz);
471
472#ifndef NO_ASN_TIME
473/* Time */
474/* Returns seconds (Epoch/UTC)
475 * timePtr: is "time_t", which is typically "long"
476 * Example:
477 long lTime;
478 rc = wc_GetTime(&lTime, (word32)sizeof(lTime));
479*/
480WOLFSSL_API int wc_GetTime(void* timePtr, word32 timeSize);
481#endif
482
483#ifdef WOLFSSL_ENCRYPTED_KEYS
484 WOLFSSL_API int wc_EncryptedInfoGet(EncryptedInfo* info,
485 const char* cipherInfo);
486#endif
487
488
489#ifdef WOLFSSL_CERT_PIV
490
491typedef struct _wc_CertPIV {
492 const byte* cert;
493 word32 certSz;
494 const byte* certErrDet;
495 word32 certErrDetSz;
496 const byte* nonce; /* Identiv Only */
497 word32 nonceSz; /* Identiv Only */
498 const byte* signedNonce; /* Identiv Only */
499 word32 signedNonceSz; /* Identiv Only */
500
501 /* flags */
502 word16 compression:2;
503 word16 isX509:1;
504 word16 isIdentiv:1;
505} wc_CertPIV;
506
507WOLFSSL_API int wc_ParseCertPIV(wc_CertPIV* cert, const byte* buf, word32 totalSz);
508#endif /* WOLFSSL_CERT_PIV */
509
510
511#ifdef __cplusplus
512 } /* extern "C" */
513#endif
514
515#endif /* WOLF_CRYPT_ASN_PUBLIC_H */
516
Note: See TracBrowser for help on using the repository browser.