Skip to content

Commit e81483f

Browse files
committed
Cleanup Lock API
Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent a12576e commit e81483f

8 files changed

Lines changed: 59 additions & 112 deletions

File tree

.github/workflows/build.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
echo 'path-exclude=/usr/share/doc-base/*' >> /etc/dpkg/dpkg.cfg.d/99-nodocs
2929
apt update -qq && apt install --no-install-recommends -y lsb-release build-essential devscripts debhelper lintian pkg-config ${UBUNTU_DEPS} doxygen swig openjdk-17-jdk-headless libpython3-dev python3-setuptools libboost-test-dev
3030
- name: Checkout
31-
uses: actions/checkout@v5
31+
uses: actions/checkout@v6
3232
- name: Setup changelog
3333
run: |
3434
export VERSION=$(grep project CMakeLists.txt | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
@@ -41,7 +41,7 @@ jobs:
4141
- name: Lintian
4242
run: lintian *.deb;
4343
- name: Archive artifacts
44-
uses: actions/upload-artifact@v5
44+
uses: actions/upload-artifact@v6
4545
with:
4646
name: ubuntu_${{ matrix.container }}_${{ matrix.arch }}
4747
path: libcdoc*.*
@@ -53,15 +53,15 @@ jobs:
5353
target: [androidarm, androidarm64, androidx86_64]
5454
steps:
5555
- name: Checkout
56-
uses: actions/checkout@v5
56+
uses: actions/checkout@v6
5757
- name: Expose Android NDK env
5858
shell: bash
5959
run: |
6060
echo "ANDROID_NDK_HOME=$ANDROID_NDK_LATEST_HOME" >> "$GITHUB_ENV"
6161
echo "ANDROID_NDK_ROOT=$ANDROID_NDK_LATEST_HOME" >> "$GITHUB_ENV"
6262
echo "VCPKG_ROOT=$VCPKG_INSTALLATION_ROOT" >> "$GITHUB_ENV"
6363
- name: Cache vcpkg
64-
uses: actions/cache@v4
64+
uses: actions/cache@v5
6565
with:
6666
path: ${{ github.workspace }}/vcpkg_cache
6767
key: vcpkg-${{ matrix.target }}-${{ hashFiles('vcpkg.json') }}
@@ -73,7 +73,7 @@ jobs:
7373
cmake --build --preset ${{ matrix.target }}
7474
cmake --build --preset ${{ matrix.target }} --target install/strip
7575
- name: Archive artifacts
76-
uses: actions/upload-artifact@v5
76+
uses: actions/upload-artifact@v6
7777
with:
7878
name: ${{ matrix.target }}
7979
path: |
@@ -88,14 +88,14 @@ jobs:
8888
DEST: ${{ github.workspace }}/${{ matrix.target }}
8989
steps:
9090
- name: Checkout
91-
uses: actions/checkout@v5
91+
uses: actions/checkout@v6
9292
- name: Install dependencies
9393
run: |
9494
brew update
9595
brew install --formula flatbuffers swig doxygen boost
9696
- name: Cache vcpkg
9797
if: matrix.target != 'macos'
98-
uses: actions/cache@v4
98+
uses: actions/cache@v5
9999
with:
100100
path: ${{ github.workspace }}/vcpkg_cache
101101
key: vcpkg-${{ matrix.target }}-${{ hashFiles('vcpkg.json') }}
@@ -112,7 +112,7 @@ jobs:
112112
- name: Install
113113
run: cmake --build --preset ${{ matrix.target }} --target install/strip
114114
- name: Archive artifacts
115-
uses: actions/upload-artifact@v5
115+
uses: actions/upload-artifact@v6
116116
with:
117117
name: ${{ matrix.target }}
118118
path: ${{ env.DEST }}
@@ -128,9 +128,9 @@ jobs:
128128
DEST: ${{ github.workspace }}/${{ matrix.image }}_${{ matrix.platform }}
129129
steps:
130130
- name: Checkout
131-
uses: actions/checkout@v5
131+
uses: actions/checkout@v6
132132
- name: Cache vcpkg
133-
uses: actions/cache@v4
133+
uses: actions/cache@v5
134134
with:
135135
path: ${{ github.workspace }}/vcpkg_cache
136136
key: vcpkg-${{ matrix.image }}-${{ matrix.platform }}-${{ hashFiles('vcpkg.json') }}
@@ -155,7 +155,7 @@ jobs:
155155
ctest -V -C RelWithDebInfo --test-dir build
156156
cmake --install build --config RelWithDebInfo --prefix ${{ env.DEST }}
157157
- name: Archive artifacts
158-
uses: actions/upload-artifact@v5
158+
uses: actions/upload-artifact@v6
159159
with:
160160
name: ${{ matrix.image }}_${{ matrix.platform }}
161161
path: ${{ env.DEST }}
@@ -167,7 +167,7 @@ jobs:
167167
contents: write
168168
steps:
169169
- name: Checkout
170-
uses: actions/checkout@v5
170+
uses: actions/checkout@v6
171171
- name: Install dependencies
172172
run: sudo apt update -qq && sudo apt install --no-install-recommends -y doxygen ${UBUNTU_DEPS}
173173
- name: Build docs
@@ -189,7 +189,7 @@ jobs:
189189
PROJECTNAME: ${{ github.repository }}
190190
steps:
191191
- name: Checkout
192-
uses: actions/checkout@v5
192+
uses: actions/checkout@v6
193193
- name: Install dependencies
194194
run: sudo apt update -qq && sudo apt install --no-install-recommends -y curl ca-certificates ${UBUNTU_DEPS}
195195
- name: Download Coverity Build Tool
@@ -224,7 +224,7 @@ jobs:
224224
security-events: write
225225
steps:
226226
- name: Checkout
227-
uses: actions/checkout@v5
227+
uses: actions/checkout@v6
228228
- name: Install dependencies
229229
run: sudo apt update -qq && sudo apt install --no-install-recommends -y ${UBUNTU_DEPS}
230230
- name: Initialize CodeQL

CMakePresets.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@
5555
"cacheVariables": {
5656
"CMAKE_SYSTEM_NAME": "iOS",
5757
"CMAKE_OSX_SYSROOT": "${presetName}",
58-
"CMAKE_OSX_DEPLOYMENT_TARGET": "15.0",
59-
"CMAKE_DISABLE_FIND_PACKAGE_SWIG": "YES",
60-
"CMAKE_CXX_FLAGS": "-D_LIBCPP_DISABLE_AVAILABILITY"
58+
"CMAKE_OSX_DEPLOYMENT_TARGET": "16.3",
59+
"CMAKE_DISABLE_FIND_PACKAGE_SWIG": "YES"
6160
}
6261
},
6362
{

cdoc/CDoc1Reader.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,13 @@ CDoc1Reader::CDoc1Reader(libcdoc::DataSource *src, bool delete_on_close)
327327
key.setBytes(Lock::Params::KEY_MATERIAL, reader.readBase64());
328328
// EncryptedData/KeyInfo/EncryptedKey/KeyInfo/X509Data/X509Certificate
329329
else if(reader.isElement("X509Certificate"))
330-
key.setCertificate(reader.readBase64());
330+
{
331+
auto cert = reader.readBase64();
332+
Certificate ssl(cert);
333+
key.setBytes(Lock::CERT, std::move(cert));
334+
key.setBytes(Lock::RCPT_KEY, ssl.getPublicKey());
335+
key.pk_type = (ssl.getAlgorithm() == libcdoc::Certificate::RSA) ? Lock::RSA : Lock::ECC;
336+
}
331337
// EncryptedData/KeyInfo/EncryptedKey/KeyInfo/CipherData/CipherValue
332338
else if(reader.isElement("CipherValue"))
333339
key.encrypted_fmk = reader.readBase64();

cdoc/CDoc2Reader.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,17 @@ CDoc2Reader::getLocks()
117117

118118
libcdoc::result_t
119119
CDoc2Reader::getLockForCert(const std::vector<uint8_t>& cert){
120-
libcdoc::Certificate cc(cert);
121-
std::vector<uint8_t> other_key = cc.getPublicKey();
120+
std::vector<uint8_t> other_key = libcdoc::Certificate(cert).getPublicKey();
121+
if (other_key.empty())
122+
return libcdoc::NOT_FOUND;
122123
LOG_DBG("Cert public key: {}", toHex(other_key));
123-
for (int lock_idx = 0; lock_idx < priv->locks.size(); lock_idx++) {
124-
const Lock &ll = priv->locks.at(lock_idx);
124+
int lock_idx = 0;
125+
for (const Lock &ll : priv->locks) {
125126
LOG_DBG("Lock {} type {}", lock_idx, (int) ll.type);
126-
if (ll.hasTheSameKey(other_key)) {
127+
if (ll.isPKI() && ll.getBytes(libcdoc::Lock::RCPT_KEY) == other_key) {
127128
return lock_idx;
128129
}
130+
++lock_idx;
129131
}
130132
setLastError("No lock found with certificate key");
131133
return libcdoc::NOT_FOUND;

cdoc/Lock.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,5 @@ Lock::setInt(Params key, int32_t val)
5353
params[key] = std::move(bytes);
5454
}
5555

56-
bool
57-
Lock::hasTheSameKey(const Lock& other) const
58-
{
59-
if (!isPKI()) return false;
60-
if (!other.isPKI()) return false;
61-
if (!params.contains(Params::RCPT_KEY)) return false;
62-
if (!other.params.contains(Params::RCPT_KEY)) return false;
63-
std::vector<uint8_t> pki = getBytes(Params::RCPT_KEY);
64-
if (pki.empty()) return false;
65-
std::vector<uint8_t> other_pki = other.getBytes(Params::RCPT_KEY);
66-
if (other_pki.empty()) return false;
67-
return pki == other_pki;
68-
}
69-
70-
bool
71-
Lock::hasTheSameKey(const std::vector<uint8_t>& public_key) const
72-
{
73-
if (!isPKI()) return false;
74-
if (!params.contains(Params::RCPT_KEY)) return false;
75-
if (public_key.empty()) return false;
76-
std::vector<uint8_t> pki = getBytes(Params::RCPT_KEY);
77-
LOG_DBG("Lock key: {}", toHex(pki));
78-
if (pki.empty()) return false;
79-
return pki == public_key;
80-
}
81-
82-
void
83-
Lock::setCertificate(const std::vector<uint8_t> &_cert)
84-
{
85-
setBytes(Params::CERT, _cert);
86-
Certificate ssl(_cert);
87-
std::vector<uint8_t> pkey = ssl.getPublicKey();
88-
Certificate::Algorithm algo = ssl.getAlgorithm();
89-
90-
setBytes(Params::RCPT_KEY, pkey);
91-
pk_type = (algo == libcdoc::Certificate::RSA) ? PKType::RSA : PKType::ECC;
92-
}
93-
9456
} // namespace libcdoc
9557

cdoc/Lock.h

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct CDOC_EXPORT Lock
4242
/**
4343
* @brief The lock type
4444
*/
45-
enum Type : unsigned char {
45+
enum Type : unsigned char {
4646
/**
4747
* @brief Valid capsule but not supported by this library version
4848
*
@@ -51,7 +51,7 @@ struct CDOC_EXPORT Lock
5151
/**
5252
* @brief Symmetric AES key
5353
*/
54-
SYMMETRIC_KEY,
54+
SYMMETRIC_KEY,
5555
/**
5656
* @brief PBKDF key (derived from password)
5757
*/
@@ -72,26 +72,26 @@ struct CDOC_EXPORT Lock
7272
* @brief Symmetric key distributed on several servers
7373
*/
7474
SHARE_SERVER
75-
};
75+
};
7676

7777
/**
7878
* @brief The public key type
7979
*/
80-
enum PKType : unsigned char {
80+
enum PKType : unsigned char {
8181
/**
8282
* Elliptic curve
8383
*/
8484
ECC,
8585
/**
8686
* RSA
8787
*/
88-
RSA
89-
};
88+
RSA
89+
};
9090

9191
/**
9292
* @brief Extra parameters depending on key type
9393
*/
94-
enum Params : unsigned int {
94+
enum Params : unsigned int {
9595
/**
9696
* @brief HKDF salt (SYMMETRIC_KEY, PASSWORD and SHARE_SERVER)
9797
*/
@@ -152,7 +152,7 @@ struct CDOC_EXPORT Lock
152152
* @brief CDoc1 specific
153153
*/
154154
PARTY_VINFO
155-
};
155+
};
156156

157157
/**
158158
* @brief get lock parameter value
@@ -176,20 +176,20 @@ struct CDOC_EXPORT Lock
176176
/**
177177
* @brief The lock type
178178
*/
179-
Type type = Type::UNKNOWN;
179+
Type type = Type::UNKNOWN;
180180
/**
181181
* @brief algorithm type for public key based locks
182182
*/
183-
PKType pk_type = PKType::ECC;
183+
PKType pk_type = PKType::ECC;
184184

185185
/**
186186
* @brief the lock label
187187
*/
188-
std::string label;
188+
std::string label;
189189
/**
190190
* @brief encrypted FMK (File Master Key)
191191
*/
192-
std::vector<uint8_t> encrypted_fmk;
192+
std::vector<uint8_t> encrypted_fmk;
193193

194194
/**
195195
* @brief check whether lock is valid
@@ -206,11 +206,6 @@ struct CDOC_EXPORT Lock
206206
* @return true if type is CDOC1, PUBLIC_KEY or SERVER
207207
*/
208208
constexpr bool isPKI() const noexcept { return (type == Type::CDOC1) || (type == Type::PUBLIC_KEY) || (type == Type::SERVER); }
209-
/**
210-
* @brief check whether lock is based on certificate
211-
* @return true if type is CDOC1
212-
*/
213-
constexpr bool isCertificate() const noexcept { return (type == Type::CDOC1); }
214209
/**
215210
* @brief check whether lock is CDoc1 version
216211
* @return true if type is CDOC1
@@ -222,57 +217,32 @@ struct CDOC_EXPORT Lock
222217
*/
223218
constexpr bool isRSA() const noexcept { return pk_type == PKType::RSA; }
224219

225-
/**
226-
* @brief check whether two locks have the same public key
227-
*
228-
* This convenience method checks whether both locks are public key based, and if they are,
229-
* whether the RCPT_KEY parameters are identical (i.e. both can be decrypted by the same private key)
230-
* @param other the other lock
231-
* @return true if both have the same public key
232-
*/
233-
bool hasTheSameKey(const Lock &other) const;
234-
/**
235-
* @brief check whether lock has the given public key
236-
*
237-
* This convenience method checks whether lock is public key based, and if it is,
238-
* whether the RCPT_KEY parameters is identical to ptovided key(i.e. it can be decrypted by the corresponding private key)
239-
* @param public_key the public key (short format)
240-
* @return true if lock has the same public key
241-
*/
242-
bool hasTheSameKey(const std::vector<uint8_t>& public_key) const;
243-
244-
Lock() noexcept = default;
245-
Lock(Type _type) noexcept : type(_type) {};
220+
Lock() noexcept = default;
221+
Lock(Type _type) noexcept : type(_type) {};
246222

247223
/**
248224
* @brief Set lock parameter value
249225
* @param param a parameter type
250226
* @param val the value
251227
*/
252-
void setBytes(Params param, const std::vector<uint8_t>& val) { params[param] = val; }
228+
void setBytes(Params param, std::vector<uint8_t> val) { params[param] = std::move(val); }
253229
/**
254230
* @brief Set lock parameter value from string
255231
* @param param a parameter type
256232
* @param val the value
257233
*/
258-
void setString(Params param, const std::string& val) { params[param] = std::vector<uint8_t>(val.cbegin(), val.cend()); }
234+
void setString(Params param, const std::string& val) { setBytes(param, {val.cbegin(), val.cend()}); }
259235
/**
260236
* @brief Set lock parameter value from integer
261237
* @param param a parameter type
262238
* @param val the value
263239
*/
264240
void setInt(Params param, int32_t val);
265241

266-
/**
267-
* @brief A convenience method to initialize CERTIFICATE, RCPT_KEY and PK_TYPE values from given certificate
268-
* @param cert the certificate (der-encoded)
269-
*/
270-
void setCertificate(const std::vector<uint8_t>& cert);
271-
272-
bool operator== (const Lock& other) const = default;
242+
bool operator== (const Lock& other) const noexcept = default;
273243

274244
private:
275-
std::map<Params,std::vector<uint8_t>> params;
245+
std::map<Params,std::vector<uint8_t>> params;
276246
};
277247

278248
} // namespace libcdoc

libcdoc.i

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@
176176
%ignore libcdoc::Lock::setBytes;
177177
%ignore libcdoc::Lock::setString;
178178
%ignore libcdoc::Lock::setInt;
179-
%ignore libcdoc::Lock::setCertificate;
180179
%extend libcdoc::Lock {
181180
Type getType() {
182181
return $self->type;

0 commit comments

Comments
 (0)