caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * The contents of this file are subject to the terms of the Common Development and
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * Distribution License (the License). You may not use this file except in compliance with the
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * License.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni *
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * specific language governing permission and limitations under the License.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni *
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * When distributing Covered Software, include this CDDL Header Notice in each file and include
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * Header, with the fields enclosed by brackets [] replaced by your own identifying
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * information: "Portions copyright [year] [name of copyright owner]".
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni *
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * Copyright 2016 ForgeRock AS.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#include <CommonCrypto/CommonHMAC.h>
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#import "FRAMechanism.h"
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni@interface FRAHotpOathMechanism : FRAMechanism
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#pragma mark -
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#pragma mark Properties
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * The length of the OATH code generated by the mechanism. Always 6 or 8; defaults to 6.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni@property (nonatomic, readonly) NSUInteger codeLength;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * The current login code for the OATH mechanism.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni@property (nonatomic, readonly) NSString *code;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * The secret key bytes used by the OATH mechanism.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni@property (nonatomic, readonly) NSData *secretKey;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * The algorithm used for generating the next hash code.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni@property (nonatomic, readonly) CCHmacAlgorithm algorithm;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * The HMAC counter which is used to generate the next hash code.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni@property (nonatomic, readonly) u_int64_t counter;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#pragma mark -
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#pragma mark Lifecyle
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * Initialize an HOTP OATH mechanism.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni *
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param database The database to which the mechanism can be persisted.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param identityModel The identity model which contains the list of identities.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param secretKey The secret key bytes used to generate the HMAC.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param algorithm The HMAC algorithm to use. Currently only MD5, SHA256, SHA512 and SHA1 are supported.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param codeLenght The length of the code.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param counter HOTP hash counter.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni *
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @return The initialized mechanism or nil if initialization failed.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni- (instancetype)initWithDatabase:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel secretKey:(NSData *)secretKey HMACAlgorithm:(CCHmacAlgorithm)algorithm codeLength:(NSUInteger)codeLenght counter:(u_int64_t)counter;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * Allocate and initialize an HOTP OATH mechanism.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni *
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param database The database to which the mechanism can be persisted.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param identityModel The identity model which contains the list of identities.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param secretKey The secret key bytes used to generate the HMAC.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param algorithm The HMAC algorithm to use. Currently only MD5, SHA256, SHA512 and SHA1 are supported.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param codeLength The length of the code.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @param counter HOTP hash counter.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni *
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * @return The initialized mechanism or nil if initialization failed.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni+ (instancetype)mechanismWithDatabase:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel secretKey:(NSData *)secretKey HMACAlgorithm:(CCHmacAlgorithm)algorithm codeLength:(NSUInteger)codeLength counter:(u_int64_t)counter;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#pragma mark -
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#pragma mark Instance Methods
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni/*!
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni * Generates the next code for this OATH mechanism.
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni *
7c51d54f23c5581d2cf894f9eafb9798e3febd22Diego Colantoni * @param error If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, you may pass in NULL.
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni * @return YES if a new code has been successfully generated, otherwise NO.
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni */
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni- (BOOL)generateNextCode:(NSError *__autoreleasing*)error;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni@end