security/nss/cmd/lib/secutil.h

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 #ifndef _SEC_UTIL_H_
michael@0 5 #define _SEC_UTIL_H_
michael@0 6
michael@0 7 #include "seccomon.h"
michael@0 8 #include "secitem.h"
michael@0 9 #include "secport.h"
michael@0 10 #include "prerror.h"
michael@0 11 #include "base64.h"
michael@0 12 #include "key.h"
michael@0 13 #include "secpkcs7.h"
michael@0 14 #include "secasn1.h"
michael@0 15 #include "secder.h"
michael@0 16 #include <stdio.h>
michael@0 17
michael@0 18 #include "basicutil.h"
michael@0 19 #include "sslerr.h"
michael@0 20 #include "sslt.h"
michael@0 21
michael@0 22
michael@0 23 #define SEC_CT_PRIVATE_KEY "private-key"
michael@0 24 #define SEC_CT_PUBLIC_KEY "public-key"
michael@0 25 #define SEC_CT_CERTIFICATE "certificate"
michael@0 26 #define SEC_CT_CERTIFICATE_REQUEST "certificate-request"
michael@0 27 #define SEC_CT_CERTIFICATE_ID "certificate-identity"
michael@0 28 #define SEC_CT_PKCS7 "pkcs7"
michael@0 29 #define SEC_CT_CRL "crl"
michael@0 30 #define SEC_CT_NAME "name"
michael@0 31
michael@0 32 #define NS_CERTREQ_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----"
michael@0 33 #define NS_CERTREQ_TRAILER "-----END NEW CERTIFICATE REQUEST-----"
michael@0 34
michael@0 35 #define NS_CERT_HEADER "-----BEGIN CERTIFICATE-----"
michael@0 36 #define NS_CERT_TRAILER "-----END CERTIFICATE-----"
michael@0 37
michael@0 38 #define NS_CRL_HEADER "-----BEGIN CRL-----"
michael@0 39 #define NS_CRL_TRAILER "-----END CRL-----"
michael@0 40
michael@0 41 #define SECU_Strerror PORT_ErrorToString
michael@0 42
michael@0 43
michael@0 44 typedef struct {
michael@0 45 enum {
michael@0 46 PW_NONE = 0,
michael@0 47 PW_FROMFILE = 1,
michael@0 48 PW_PLAINTEXT = 2,
michael@0 49 PW_EXTERNAL = 3
michael@0 50 } source;
michael@0 51 char *data;
michael@0 52 } secuPWData;
michael@0 53
michael@0 54 /*
michael@0 55 ** Change a password on a token, or initialize a token with a password
michael@0 56 ** if it does not already have one.
michael@0 57 ** Use passwd to send the password in plaintext, pwFile to specify a
michael@0 58 ** file containing the password, or NULL for both to prompt the user.
michael@0 59 */
michael@0 60 SECStatus SECU_ChangePW(PK11SlotInfo *slot, char *passwd, char *pwFile);
michael@0 61
michael@0 62 /*
michael@0 63 ** Change a password on a token, or initialize a token with a password
michael@0 64 ** if it does not already have one.
michael@0 65 ** In this function, you can specify both the old and new passwords
michael@0 66 ** as either a string or file. NOTE: any you don't specify will
michael@0 67 ** be prompted for
michael@0 68 */
michael@0 69 SECStatus SECU_ChangePW2(PK11SlotInfo *slot, char *oldPass, char *newPass,
michael@0 70 char *oldPwFile, char *newPwFile);
michael@0 71
michael@0 72 /* These were stolen from the old sec.h... */
michael@0 73 /*
michael@0 74 ** Check a password for legitimacy. Passwords must be at least 8
michael@0 75 ** characters long and contain one non-alphabetic. Return DSTrue if the
michael@0 76 ** password is ok, DSFalse otherwise.
michael@0 77 */
michael@0 78 extern PRBool SEC_CheckPassword(char *password);
michael@0 79
michael@0 80 /*
michael@0 81 ** Blind check of a password. Complement to SEC_CheckPassword which
michael@0 82 ** ignores length and content type, just retuning DSTrue is the password
michael@0 83 ** exists, DSFalse if NULL
michael@0 84 */
michael@0 85 extern PRBool SEC_BlindCheckPassword(char *password);
michael@0 86
michael@0 87 /*
michael@0 88 ** Get a password.
michael@0 89 ** First prompt with "msg" on "out", then read the password from "in".
michael@0 90 ** The password is then checked using "chkpw".
michael@0 91 */
michael@0 92 extern char *SEC_GetPassword(FILE *in, FILE *out, char *msg,
michael@0 93 PRBool (*chkpw)(char *));
michael@0 94
michael@0 95 char *SECU_FilePasswd(PK11SlotInfo *slot, PRBool retry, void *arg);
michael@0 96
michael@0 97 char *SECU_GetPasswordString(void *arg, char *prompt);
michael@0 98
michael@0 99 /*
michael@0 100 ** Write a dongle password.
michael@0 101 ** Uses MD5 to hash constant system data (hostname, etc.), and then
michael@0 102 ** creates RC4 key to encrypt a password "pw" into a file "fd".
michael@0 103 */
michael@0 104 extern SECStatus SEC_WriteDongleFile(int fd, char *pw);
michael@0 105
michael@0 106 /*
michael@0 107 ** Get a dongle password.
michael@0 108 ** Uses MD5 to hash constant system data (hostname, etc.), and then
michael@0 109 ** creates RC4 key to decrypt and return a password from file "fd".
michael@0 110 */
michael@0 111 extern char *SEC_ReadDongleFile(int fd);
michael@0 112
michael@0 113
michael@0 114 /* End stolen headers */
michael@0 115
michael@0 116 /* Just sticks the two strings together with a / if needed */
michael@0 117 char *SECU_AppendFilenameToDir(char *dir, char *filename);
michael@0 118
michael@0 119 /* Returns result of getenv("SSL_DIR") or NULL */
michael@0 120 extern char *SECU_DefaultSSLDir(void);
michael@0 121
michael@0 122 /*
michael@0 123 ** Should be called once during initialization to set the default
michael@0 124 ** directory for looking for cert.db, key.db, and cert-nameidx.db files
michael@0 125 ** Removes trailing '/' in 'base'
michael@0 126 ** If 'base' is NULL, defaults to set to .netscape in home directory.
michael@0 127 */
michael@0 128 extern char *SECU_ConfigDirectory(const char* base);
michael@0 129
michael@0 130 /*
michael@0 131 ** Basic callback function for SSL_GetClientAuthDataHook
michael@0 132 */
michael@0 133 extern int
michael@0 134 SECU_GetClientAuthData(void *arg, PRFileDesc *fd,
michael@0 135 struct CERTDistNamesStr *caNames,
michael@0 136 struct CERTCertificateStr **pRetCert,
michael@0 137 struct SECKEYPrivateKeyStr **pRetKey);
michael@0 138
michael@0 139 extern PRBool SECU_GetWrapEnabled(void);
michael@0 140 extern void SECU_EnableWrap(PRBool enable);
michael@0 141
michael@0 142 extern PRBool SECU_GetUtf8DisplayEnabled(void);
michael@0 143 extern void SECU_EnableUtf8Display(PRBool enable);
michael@0 144
michael@0 145 /* revalidate the cert and print information about cert verification
michael@0 146 * failure at time == now */
michael@0 147 extern void
michael@0 148 SECU_printCertProblems(FILE *outfile, CERTCertDBHandle *handle,
michael@0 149 CERTCertificate *cert, PRBool checksig,
michael@0 150 SECCertificateUsage certUsage, void *pinArg, PRBool verbose);
michael@0 151
michael@0 152 /* revalidate the cert and print information about cert verification
michael@0 153 * failure at specified time */
michael@0 154 extern void
michael@0 155 SECU_printCertProblemsOnDate(FILE *outfile, CERTCertDBHandle *handle,
michael@0 156 CERTCertificate *cert, PRBool checksig, SECCertificateUsage certUsage,
michael@0 157 void *pinArg, PRBool verbose, PRTime datetime);
michael@0 158
michael@0 159 /* print out CERTVerifyLog info. */
michael@0 160 extern void
michael@0 161 SECU_displayVerifyLog(FILE *outfile, CERTVerifyLog *log,
michael@0 162 PRBool verbose);
michael@0 163
michael@0 164 /* Read in a DER from a file, may be ascii */
michael@0 165 extern SECStatus
michael@0 166 SECU_ReadDERFromFile(SECItem *der, PRFileDesc *inFile, PRBool ascii,
michael@0 167 PRBool warnOnPrivateKeyInAsciiFile);
michael@0 168
michael@0 169 /* Print integer value and hex */
michael@0 170 extern void SECU_PrintInteger(FILE *out, const SECItem *i, const char *m,
michael@0 171 int level);
michael@0 172
michael@0 173 /* Print ObjectIdentifier symbolically */
michael@0 174 extern SECOidTag SECU_PrintObjectID(FILE *out, const SECItem *oid,
michael@0 175 const char *m, int level);
michael@0 176
michael@0 177 /* Print AlgorithmIdentifier symbolically */
michael@0 178 extern void SECU_PrintAlgorithmID(FILE *out, SECAlgorithmID *a, char *m,
michael@0 179 int level);
michael@0 180
michael@0 181 /*
michael@0 182 * Format and print the UTC Time "t". If the tag message "m" is not NULL,
michael@0 183 * do indent formatting based on "level" and add a newline afterward;
michael@0 184 * otherwise just print the formatted time string only.
michael@0 185 */
michael@0 186 extern void SECU_PrintUTCTime(FILE *out, const SECItem *t, const char *m,
michael@0 187 int level);
michael@0 188
michael@0 189 /*
michael@0 190 * Format and print the Generalized Time "t". If the tag message "m"
michael@0 191 * is not NULL, * do indent formatting based on "level" and add a newline
michael@0 192 * afterward; otherwise just print the formatted time string only.
michael@0 193 */
michael@0 194 extern void SECU_PrintGeneralizedTime(FILE *out, const SECItem *t,
michael@0 195 const char *m, int level);
michael@0 196
michael@0 197 /*
michael@0 198 * Format and print the UTC or Generalized Time "t". If the tag message
michael@0 199 * "m" is not NULL, do indent formatting based on "level" and add a newline
michael@0 200 * afterward; otherwise just print the formatted time string only.
michael@0 201 */
michael@0 202 extern void SECU_PrintTimeChoice(FILE *out, const SECItem *t, const char *m,
michael@0 203 int level);
michael@0 204
michael@0 205 /* callback for listing certs through pkcs11 */
michael@0 206 extern SECStatus SECU_PrintCertNickname(CERTCertListNode* cert, void *data);
michael@0 207
michael@0 208 /* Dump all certificate nicknames in a database */
michael@0 209 extern SECStatus
michael@0 210 SECU_PrintCertificateNames(CERTCertDBHandle *handle, PRFileDesc* out,
michael@0 211 PRBool sortByName, PRBool sortByTrust);
michael@0 212
michael@0 213 /* See if nickname already in database. Return 1 true, 0 false, -1 error */
michael@0 214 int SECU_CheckCertNameExists(CERTCertDBHandle *handle, char *nickname);
michael@0 215
michael@0 216 /* Dump contents of cert req */
michael@0 217 extern int SECU_PrintCertificateRequest(FILE *out, SECItem *der, char *m,
michael@0 218 int level);
michael@0 219
michael@0 220 /* Dump contents of certificate */
michael@0 221 extern int SECU_PrintCertificate(FILE *out, const SECItem *der, const char *m,
michael@0 222 int level);
michael@0 223
michael@0 224 extern int SECU_PrintDumpDerIssuerAndSerial(FILE *out, SECItem *der, char *m,
michael@0 225 int level);
michael@0 226
michael@0 227 /* Dump contents of a DER certificate name (issuer or subject) */
michael@0 228 extern int SECU_PrintDERName(FILE *out, SECItem *der, const char *m, int level);
michael@0 229
michael@0 230 /* print trust flags on a cert */
michael@0 231 extern void SECU_PrintTrustFlags(FILE *out, CERTCertTrust *trust, char *m,
michael@0 232 int level);
michael@0 233
michael@0 234 extern int SECU_PrintSubjectPublicKeyInfo(FILE *out, SECItem *der, char *m,
michael@0 235 int level);
michael@0 236
michael@0 237 #ifdef HAVE_EPV_TEMPLATE
michael@0 238 /* Dump contents of private key */
michael@0 239 extern int SECU_PrintPrivateKey(FILE *out, SECItem *der, char *m, int level);
michael@0 240 #endif
michael@0 241
michael@0 242 /* Dump contents of an RSA public key */
michael@0 243 extern void SECU_PrintRSAPublicKey(FILE *out, SECKEYPublicKey *pk, char *m, int level);
michael@0 244
michael@0 245 /* Dump contents of a DSA public key */
michael@0 246 extern void SECU_PrintDSAPublicKey(FILE *out, SECKEYPublicKey *pk, char *m, int level);
michael@0 247
michael@0 248 /* Print the MD5 and SHA1 fingerprints of a cert */
michael@0 249 extern int SECU_PrintFingerprints(FILE *out, SECItem *derCert, char *m,
michael@0 250 int level);
michael@0 251
michael@0 252 /* Pretty-print any PKCS7 thing */
michael@0 253 extern int SECU_PrintPKCS7ContentInfo(FILE *out, SECItem *der, char *m,
michael@0 254 int level);
michael@0 255
michael@0 256 /* Init PKCS11 stuff */
michael@0 257 extern SECStatus SECU_PKCS11Init(PRBool readOnly);
michael@0 258
michael@0 259 /* Dump contents of signed data */
michael@0 260 extern int SECU_PrintSignedData(FILE *out, SECItem *der, const char *m,
michael@0 261 int level, SECU_PPFunc inner);
michael@0 262
michael@0 263 /* Dump contents of signed data, excluding the signature */
michael@0 264 extern int SECU_PrintSignedContent(FILE *out, SECItem *der, char *m, int level,
michael@0 265 SECU_PPFunc inner);
michael@0 266
michael@0 267 /* Print cert data and its trust flags */
michael@0 268 extern SECStatus SEC_PrintCertificateAndTrust(CERTCertificate *cert,
michael@0 269 const char *label,
michael@0 270 CERTCertTrust *trust);
michael@0 271
michael@0 272 extern int SECU_PrintCrl(FILE *out, SECItem *der, char *m, int level);
michael@0 273
michael@0 274 extern void
michael@0 275 SECU_PrintCRLInfo(FILE *out, CERTCrl *crl, char *m, int level);
michael@0 276
michael@0 277 extern void SECU_PrintString(FILE *out, const SECItem *si, const char *m,
michael@0 278 int level);
michael@0 279 extern void SECU_PrintAny(FILE *out, const SECItem *i, const char *m, int level);
michael@0 280
michael@0 281 extern void SECU_PrintPolicy(FILE *out, SECItem *value, char *msg, int level);
michael@0 282 extern void SECU_PrintPrivKeyUsagePeriodExtension(FILE *out, SECItem *value,
michael@0 283 char *msg, int level);
michael@0 284
michael@0 285 extern void SECU_PrintExtensions(FILE *out, CERTCertExtension **extensions,
michael@0 286 char *msg, int level);
michael@0 287
michael@0 288 extern void SECU_PrintNameQuotesOptional(FILE *out, CERTName *name,
michael@0 289 const char *msg, int level,
michael@0 290 PRBool quotes);
michael@0 291 extern void SECU_PrintName(FILE *out, CERTName *name, const char *msg,
michael@0 292 int level);
michael@0 293 extern void SECU_PrintRDN(FILE *out, CERTRDN *rdn, const char *msg, int level);
michael@0 294
michael@0 295 #ifdef SECU_GetPassword
michael@0 296 /* Convert a High public Key to a Low public Key */
michael@0 297 extern SECKEYLowPublicKey *SECU_ConvHighToLow(SECKEYPublicKey *pubHighKey);
michael@0 298 #endif
michael@0 299
michael@0 300 extern char *SECU_GetModulePassword(PK11SlotInfo *slot, PRBool retry, void *arg);
michael@0 301
michael@0 302 extern SECStatus DER_PrettyPrint(FILE *out, const SECItem *it, PRBool raw);
michael@0 303
michael@0 304 extern char *SECU_SECModDBName(void);
michael@0 305
michael@0 306 /* Fetch and register an oid if it hasn't been done already */
michael@0 307 extern void SECU_cert_fetchOID(SECOidTag *data, const SECOidData *src);
michael@0 308
michael@0 309 extern SECStatus SECU_RegisterDynamicOids(void);
michael@0 310
michael@0 311 /* Identifies hash algorithm tag by its string representation. */
michael@0 312 extern SECOidTag SECU_StringToSignatureAlgTag(const char *alg);
michael@0 313
michael@0 314 /* Store CRL in output file or pk11 db. Also
michael@0 315 * encodes with base64 and exports to file if ascii flag is set
michael@0 316 * and file is not NULL. */
michael@0 317 extern SECStatus SECU_StoreCRL(PK11SlotInfo *slot, SECItem *derCrl,
michael@0 318 PRFileDesc *outFile, PRBool ascii, char *url);
michael@0 319
michael@0 320
michael@0 321 /*
michael@0 322 ** DER sign a single block of data using private key encryption and the
michael@0 323 ** MD5 hashing algorithm. This routine first computes a digital signature
michael@0 324 ** using SEC_SignData, then wraps it with an CERTSignedData and then der
michael@0 325 ** encodes the result.
michael@0 326 ** "arena" is the memory arena to use to allocate data from
michael@0 327 ** "sd" returned CERTSignedData
michael@0 328 ** "result" the final der encoded data (memory is allocated)
michael@0 329 ** "buf" the input data to sign
michael@0 330 ** "len" the amount of data to sign
michael@0 331 ** "pk" the private key to encrypt with
michael@0 332 */
michael@0 333 extern SECStatus SECU_DerSignDataCRL(PLArenaPool *arena, CERTSignedData *sd,
michael@0 334 unsigned char *buf, int len,
michael@0 335 SECKEYPrivateKey *pk, SECOidTag algID);
michael@0 336
michael@0 337 typedef enum {
michael@0 338 noKeyFound = 1,
michael@0 339 noSignatureMatch = 2,
michael@0 340 failToEncode = 3,
michael@0 341 failToSign = 4,
michael@0 342 noMem = 5
michael@0 343 } SignAndEncodeFuncExitStat;
michael@0 344
michael@0 345 extern SECStatus
michael@0 346 SECU_SignAndEncodeCRL(CERTCertificate *issuer, CERTSignedCrl *signCrl,
michael@0 347 SECOidTag hashAlgTag, SignAndEncodeFuncExitStat *resCode);
michael@0 348
michael@0 349 extern SECStatus
michael@0 350 SECU_CopyCRL(PLArenaPool *destArena, CERTCrl *destCrl, CERTCrl *srcCrl);
michael@0 351
michael@0 352 /*
michael@0 353 ** Finds the crl Authority Key Id extension. Returns NULL if no such extension
michael@0 354 ** was found.
michael@0 355 */
michael@0 356 CERTAuthKeyID *
michael@0 357 SECU_FindCRLAuthKeyIDExten (PLArenaPool *arena, CERTSignedCrl *crl);
michael@0 358
michael@0 359 /*
michael@0 360 * Find the issuer of a crl. Cert usage should be checked before signing a crl.
michael@0 361 */
michael@0 362 CERTCertificate *
michael@0 363 SECU_FindCrlIssuer(CERTCertDBHandle *dbHandle, SECItem* subject,
michael@0 364 CERTAuthKeyID* id, PRTime validTime);
michael@0 365
michael@0 366
michael@0 367 /* call back function used in encoding of an extension. Called from
michael@0 368 * SECU_EncodeAndAddExtensionValue */
michael@0 369 typedef SECStatus (* EXTEN_EXT_VALUE_ENCODER) (PLArenaPool *extHandleArena,
michael@0 370 void *value, SECItem *encodedValue);
michael@0 371
michael@0 372 /* Encodes and adds extensions to the CRL or CRL entries. */
michael@0 373 SECStatus
michael@0 374 SECU_EncodeAndAddExtensionValue(PLArenaPool *arena, void *extHandle,
michael@0 375 void *value, PRBool criticality, int extenType,
michael@0 376 EXTEN_EXT_VALUE_ENCODER EncodeValueFn);
michael@0 377
michael@0 378 /* Caller ensures that dst is at least item->len*2+1 bytes long */
michael@0 379 void
michael@0 380 SECU_SECItemToHex(const SECItem * item, char * dst);
michael@0 381
michael@0 382 /* Requires 0x prefix. Case-insensitive. Will do in-place replacement if
michael@0 383 * successful */
michael@0 384 SECStatus
michael@0 385 SECU_SECItemHexStringToBinary(SECItem* srcdest);
michael@0 386
michael@0 387 /* Parse a version range string, with "min" and "max" version numbers,
michael@0 388 * separated by colon (":"), and return the result in vr and v2.
michael@0 389 *
michael@0 390 * Both min and max values are optional.
michael@0 391 * The following syntax is used to specify the enabled protocol versions:
michael@0 392 * A string with only a max value is expected as ":{max}",
michael@0 393 * and all implemented versions less than or equal to max will be enabled.
michael@0 394 * A string with only a min value is expected as "{min}:",
michael@0 395 * and all implemented versions greater than or equal to min will be enabled.
michael@0 396 * A string consisting of a colon only means "all versions enabled".
michael@0 397 *
michael@0 398 * Because output parameter type SSLVersionRange doesn't allow to set
michael@0 399 * version 2 values, we use a separate boolean output parameter
michael@0 400 * to return whether SSL 2 is enabled.
michael@0 401 *
michael@0 402 * In order to avoid a link dependency from libsectool to libssl,
michael@0 403 * the caller must provide the desired default values for the min/max values,
michael@0 404 * by providing defaultEnableSSL2 and defaultVersionRange
michael@0 405 * (which can be obtained from libssl by calling SSL_VersionRangeGetSupported).
michael@0 406 */
michael@0 407 SECStatus
michael@0 408 SECU_ParseSSLVersionRangeString(const char *input,
michael@0 409 const SSLVersionRange defaultVersionRange,
michael@0 410 const PRBool defaultEnableSSL2,
michael@0 411 SSLVersionRange *vrange,
michael@0 412 PRBool *enableSSL2);
michael@0 413
michael@0 414 /*
michael@0 415 *
michael@0 416 * Error messaging
michael@0 417 *
michael@0 418 */
michael@0 419
michael@0 420 void printflags(char *trusts, unsigned int flags);
michael@0 421
michael@0 422 #if !defined(XP_UNIX) && !defined(XP_OS2)
michael@0 423 extern int ffs(unsigned int i);
michael@0 424 #endif
michael@0 425
michael@0 426 /* Finds certificate by searching it in the DB or by examinig file
michael@0 427 * in the local directory. */
michael@0 428 CERTCertificate*
michael@0 429 SECU_FindCertByNicknameOrFilename(CERTCertDBHandle *handle,
michael@0 430 char *name, PRBool ascii,
michael@0 431 void *pwarg);
michael@0 432 #include "secerr.h"
michael@0 433 #include "sslerr.h"
michael@0 434
michael@0 435 #endif /* _SEC_UTIL_H_ */

mercurial