char* const get_encryption_password() { char* pass1 = NULL; char* pass2 = NULL; /* if we are encrypting, ask for password twice */ int i=0; while(1) { if(i>2) { fprintf(stderr,"The password must be entered in correctly.\n"); exit(-1); } pass1 = read_pass("Enter encryption password: "******"Re-enter encryption password: "******"Passwords do not match!\n"); i++; continue; } /* erase one of the plaintext password instances from memory * as soon as we check the two instances against each other */ // while(*pass2) *pass2++ = 0; guaranteed_memset(pass2,0,strlen(pass1)); break; } return pass1; }
// Used for cleaning password strings void CleanPasswdString(char *str) { if (str) { guaranteed_memset(str, 0, strlen(str)); free(str); } }
/** * Destroy the secure memory pool */ void secmem_destroy_pool(void) { int i; if (!pool) return; for (i = 0; i < SECPAGES; i++) pageindex[i] = 0; guaranteed_memset(pool, 0, poolsize); munmap(pool, poolsize); poolsize = 0; pool = NULL; }
/** * Free a page of secure memory * @page: page to be free:d, illegal page pointers will be ignored */ void secmem_free_page(char *page) { int i; // Bogus pointers will not match and are ignored for (i = 0; i < SECPAGES; i++) { if (pool + (pagesize * i) == page) { pageindex[i] = 0; guaranteed_memset(page, 0, pagesize); break; } } return; }
void decrypt_file(FILE* ifp, FILE* ofp, int algo) { BYTE block[4 * 4]; long int fsize = 0; int size = 0; size = fread(&fsize, sizeof(long int), 1, ifp); if (size <= 0) { fprintf(stderr,"*** Error reading data\n"); exit(-1); } while (1) { guaranteed_memset(block, 0, 16); size = fread(block, 1, 16, ifp); if (size <= 0) break; switch(algo) { case RIJNDAEL: blockDecryptR(&cipherInst, &keyInstR, block, 128, block); break; case SERPENT: blockDecryptS(&cipherInst, &keyInstS, block, 128, block); break; case TWOFISH: blockDecryptT(&cipherInstT, &keyInstT, block, 128, block); break; } /* on last block, eliminate the padding if it exists */ if(fsize < 17) /* last block */ { if (fwrite(block, (int)fsize, 1, ofp) != 1) { fprintf(stderr,"** ERROR writing data from last block\n"); exit(-1); } break; } else /* otherwise, normal writing to decrypt file */ { if (fwrite(block, 16, 1, ofp) != 1) { fprintf(stderr,"** ERROR writing data\n"); exit(-1); } } fsize -= size; } }
static TokenError addTokenFile(const char *filename) { int fileLen; char *fileData; if (!platform_readFile(filename, &fileData, &fileLen)) return TokenError_FileNotReadable; TokenError error = backend_addFile(notifier, fileData, fileLen, strdup(filename)); guaranteed_memset(fileData, 0, fileLen); free(fileData); return error; }
void encrypt_file(FILE* ifp, long int fsize, FILE* ofp, int algo) { BYTE block[4 * 4]; int size = 0; /* we are writing the filesize into the very beginning of the encrypted file, so we can handle the decryption later on */ if (fwrite(&fsize, sizeof(long int), 1, ofp) != 1) { fprintf(stderr,"** ERROR writing data\n"); exit(-1); } while (1) { guaranteed_memset(block, 0, 16); size = fread(block, 1, 16, ifp); if (size <= 0) { fprintf(stderr,"** ERROR reading data\n"); exit(-1); } switch(algo) { case RIJNDAEL: blockEncryptR(&cipherInst, &keyInstR, block, 128, block); break; case SERPENT: blockEncryptS(&cipherInst, &keyInstS, block, 128, block); break; case TWOFISH: blockEncryptT(&cipherInstT, &keyInstT, block, 128, block); break; } /* Note that on the last block, we are padding to 16 bytes, this is done to ensure complete decryption later on since the encryption and decryption is dependent on block sizes of 16 bytes. Without padding, decryption would fail on the last block. */ if (fwrite(block, 16, 1, ofp) != 1) { fprintf(stderr,"** ERROR writing data\n"); exit(-1); } if(fsize<17) break; fsize -= size; } }
/** * Called when a command is being sent from the plugin. */ void pipeCommand(PipeCommand command, const char *url, const char *hostname, const char *ip) { switch (command) { case PC_GetVersion: { char *versionString = bankid_getVersion(); pipe_sendString(stdout, versionString); free(versionString); pipe_flush(stdout); platform_leaveMainloop(); break; } case PC_Authenticate: case PC_Sign: { char *challenge = pipe_readString(stdin); int32_t serverTime = pipe_readInt(stdin); free(pipe_readOptionalString(stdin)); // Just ignore the policies list for now char *subjectFilter = pipe_readOptionalString(stdin); char *messageEncoding = NULL, *message = NULL, *invisibleMessage = NULL; if (command == PC_Sign) { messageEncoding = pipe_readString(stdin); message = pipe_readString(stdin); invisibleMessage = pipe_readOptionalString(stdin); } // Validate input BankIDError error = BIDERR_OK; if (!is_https_url(url)) { error = BIDERR_NotSSL; } else if (!is_canonical_base64(challenge) || !is_valid_hostname(hostname) || !is_valid_ip_address(ip) || (command == PC_Sign && ( !is_canonical_base64(message) || (invisibleMessage && !is_canonical_base64(invisibleMessage)) ))) { error = BIDERR_InternalError; } if (error != BIDERR_OK) { pipe_sendInt(stdout, error); pipe_sendString(stdout, ""); pipe_flush(stdout); platform_leaveMainloop(); return; } if (subjectFilter && !is_canonical_base64(subjectFilter)) { // The subject filter is invalid. Ignore it free(subjectFilter); subjectFilter = NULL; } Token *token; char *password = NULL; long password_maxsize = 0; char *signature = NULL; char *decodedSubjectFilter = NULL; error = BIDERR_UserCancel; // Allocate a secure page for the password password = secmem_get_page(&password_maxsize); if (!password || !password_maxsize) { pipe_sendInt(stdout, BIDERR_InternalError); pipe_sendString(stdout, ""); pipe_flush(stdout); platform_leaveMainloop(); return; } if (subjectFilter) { decodedSubjectFilter = base64_decode(subjectFilter); free(subjectFilter); } // Pass all parameters to the user interface platform_startSign(url, hostname, ip, browserWindowId); BackendNotifier *notifier = backend_createNotifier( decodedSubjectFilter, (command == PC_Sign ? KeyUsage_Signing : KeyUsage_Authentication), notifyCallback); platform_setNotifier(notifier); platform_addKeyDirectories(); backend_scanTokens(notifier); free(decodedSubjectFilter); if (command == PC_Sign) { if (!message) abort(); char *decodedMessage = base64_decode(message); platform_setMessage(decodedMessage); free(decodedMessage); } while (platform_sign(&token, password, password_maxsize)) { // Set the password (not used by all backends) token_usePassword(token, password); // Try to authenticate/sign if (command == PC_Authenticate) { error = bankid_authenticate(token, challenge, serverTime, hostname, ip, &signature); } else { error = bankid_sign(token, challenge, serverTime, hostname, ip, messageEncoding, message, invisibleMessage, &signature); } guaranteed_memset(password, 0, password_maxsize); if (error == BIDERR_OK) break; // An error occurred const TokenError tokenError = token_getLastError(token); platform_showError(tokenError); if (tokenError == TokenError_BadPassword || tokenError == TokenError_BadPin) { platform_focusPassword(); // also removes focus from the Sign button } error = BIDERR_UserCancel; } secmem_free_page(password); platform_endSign(); backend_freeNotifier(notifier); free(messageEncoding); free(message); free(invisibleMessage); free(challenge); pipe_sendInt(stdout, error); pipe_sendString(stdout, (signature ? signature : "")); pipe_flush(stdout); free(signature); platform_leaveMainloop(); break; } case PC_CreateRequest: { char *request = NULL; BankIDError error = BIDERR_InternalError; long password_maxsize = 0; char *name = NULL; char *password = NULL; // Read input RegutilInfo input; memset(&input, 0, sizeof(input)); input.minPasswordLength = pipe_readInt(stdin); input.minPasswordNonDigits = pipe_readInt(stdin); input.minPasswordDigits = pipe_readInt(stdin); while (pipe_readInt(stdin) == PLS_MoreData) { // PKCS10 RegutilPKCS10 *pkcs10 = malloc(sizeof(RegutilPKCS10)); pkcs10->keyUsage = pipe_readInt(stdin); pkcs10->keySize = pipe_readInt(stdin); pkcs10->subjectDN = pipe_readString(stdin); pkcs10->includeFullDN = pipe_readInt(stdin); pkcs10->next = input.pkcs10; input.pkcs10 = pkcs10; } // CMC input.cmc.oneTimePassword = pipe_readString(stdin); input.cmc.rfc2729cmcoid = pipe_readString(stdin); // Check for broken pipe if (feof(stdin)) goto createReq_end; // Check input if (!input.pkcs10) goto createReq_end; // Get name to display name = bankid_getRequestDisplayName(&input); if (!name) goto createReq_end; // Allocate a secure page for the password password = secmem_get_page(&password_maxsize); if (!password || !password_maxsize) goto createReq_end; platform_startChoosePassword(name, browserWindowId); platform_setPasswordPolicy(input.minPasswordLength, input.minPasswordNonDigits, input.minPasswordDigits); for (;;) { error = RUERR_UserCancel; // Ask for a password if (!platform_choosePassword(password, password_maxsize)) break; // Try to authenticate/sign // Generate key pair and construct the request TokenError tokenError; error = bankid_createRequest(&input, hostname, password, &request, &tokenError); guaranteed_memset(password, 0, password_maxsize); if (error == BIDERR_OK) break; platform_showError(tokenError); } platform_endChoosePassword(); // Send result createReq_end: secmem_free_page(password); pipe_sendInt(stdout, error); if (!request) pipe_sendString(stdout, ""); else { pipe_sendString(stdout, request); free(request); } pipe_flush(stdout); platform_leaveMainloop(); break; } case PC_StoreCertificates: { char *certs = pipe_readString(stdin); TokenError tokenError; BankIDError error = bankid_storeCertificates(certs, hostname, &tokenError); if (error != BIDERR_OK) { if (prefs_debug_dump) { certutil_dumpCertsP7(certs); } platform_showError(tokenError); } pipe_sendInt(stdout, error); pipe_flush(stdout); platform_leaveMainloop(); break; } default: { fprintf(stderr, BINNAME ": invalid command from pipe\n"); platform_leaveMainloop(); break; } } }